ria-com / nomeroff-net

Nomeroff Net. Automatic numberplate recognition system.
GNU General Public License v3.0
460 stars 160 forks source link

MASK_RCNN + cv2 #83

Closed prozaklob closed 4 years ago

prozaklob commented 4 years ago

Пытаюсь адаптировать проект под работу с видео. :) Разобрать на фреймы видео и в цикле перебрать все фреймы на предмет наличия номера. Вывести номер Сохранить кадр в картинку,если там найден номер.

Набросал корявенький скрипт:

# Import all necessary libraries.
import numpy as np
import matplotlib.image as mpimg
import tensorflow as tf
from matplotlib import pyplot
from matplotlib.patches import Rectangle
from matplotlib.patches import Circle
import cv2
import os
import time
import sys

# change this property
NOMEROFF_NET_DIR = os.path.abspath('../../')

# specify the path to Mask_RCNN if you placed it outside Nomeroff-net project
MASK_RCNN_DIR = os.path.join(NOMEROFF_NET_DIR, 'Mask_RCNN')
MASK_RCNN_LOG_DIR = os.path.join(NOMEROFF_NET_DIR, 'logs')

sys.path.append(NOMEROFF_NET_DIR)

# Import license plate recognition tools.
from NomeroffNet import  filters, RectDetector, TextDetector, OptionsDetector, Detector, textPostprocessing, textPostprocessingAsync

# Initialize npdetector with default configuration file.
nnet = Detector(MASK_RCNN_DIR, MASK_RCNN_LOG_DIR)
nnet.loadModel("latest")

rectDetector = RectDetector()

optionsDetector = OptionsDetector()
optionsDetector.load("latest")

# Initialize text detector.
textDetector = TextDetector.get_static_module("eu")()
textDetector.load("latest")

count = 0

cap = cv2.VideoCapture("dataset/1.mp4")
while True:
    #Capture frame-by-frame
    __, frame = cap.read()

    result = nnet.detect(frame)
    if result != []:
        for person in result:

            # Detect numberplate
            img = mpimg.imread(result)
            NP = nnet.detectFromFile([img])

            # Generate image mask.
            cv_img_masks = filters.cv_img_mask(NP)

            # Detect points.
            arrPoints = rectDetector.detect(cv_img_masks)
            zones = rectDetector.get_cv_zonesBGR(img, arrPoints)

            # find standart
            regionIds, stateIds, countLines = optionsDetector.predict(zones)
            regionNames = optionsDetector.getRegionLabels(regionIds)

            # find text with postprocessing by standart  
            textArr = textDetector.predict(zones)
            textArr = textPostprocessing(textArr, regionNames)
            print(textArr)

            count += 1

            timestr = time.strftime("%Y%m%d-%H%M%S")
            filename = ("dataset/car." + str(timestr) + '.' + str(count) + ".jpg")
            cv2.imwrite("dataset/car." + str(timestr) + '.' + str(count) + ".jpg", frame)
            print(filename)

    cv2.imshow('frame', frame)

    k = cv2.waitKey(100) & 0xff # Press 'ESC' for exiting video
    if k == 27:
        break
    elif count >= 1000: # Take 30 face sample and stop video
        break

# Do a bit of cleanup
print("\n [INFO] Exiting Program and cleanup stuff")
cam.release()
cv2.destroyAllWindows()

Вижу,что gpu напрягается,но на выходе вместо желаемого получаю ошибку:

self.MODEL = Model(input=net_inp, output=net_out)
Traceback (most recent call last):
  File "tet.py", line 44, in <module>
    result = nnet.detect(frame)
  File "\nomeroff-net-master\NomeroffNet\Detector.py", line 98, in detect
    return self.MODEL.detect(self.normalize(images), verbose=verbose)
  File "\mask_rcnn-2.1-py3.7.egg\mrcnn\model.py", line 2495, in detect
AssertionError: len(images) must be equal to BATCH_SIZE
prozaklob commented 4 years ago

Исправил.

Скрипт берет кадр из видео,детектит маску. Если маска поймана - пытаемся определить номер.

Выводим попутно на экран.

# Import all necessary libraries.
import numpy as np
import matplotlib.image as mpimg
import tensorflow as tf
from matplotlib import pyplot
from matplotlib.patches import Rectangle
from matplotlib.patches import Circle
import cv2
import os
import time
from datetime import datetime
import sys

# change this property
NOMEROFF_NET_DIR = os.path.abspath('../../')

# specify the path to Mask_RCNN if you placed it outside Nomeroff-net project
MASK_RCNN_DIR = os.path.join(NOMEROFF_NET_DIR, 'Mask_RCNN')
MASK_RCNN_LOG_DIR = os.path.join(NOMEROFF_NET_DIR, 'logs')

sys.path.append(NOMEROFF_NET_DIR)

# Import license plate recognition tools.
from NomeroffNet import  filters, RectDetector, TextDetector, OptionsDetector, Detector, textPostprocessing, textPostprocessingAsync

# Initialize npdetector with default configuration file.
nnet = Detector(MASK_RCNN_DIR, MASK_RCNN_LOG_DIR)
nnet.loadModel("latest")

rectDetector = RectDetector()

optionsDetector = OptionsDetector()
optionsDetector.load("latest")

# Initialize text detector.
textDetector = TextDetector.get_static_module("eu")()
textDetector.load("latest")

cap = cv2.VideoCapture("dataset/2.mp4")
count = 0
while True:
    #Capture frame-by-frame
    __, frame = cap.read()

    NP = nnet.detect([frame])

    if NP != []:
        for person in NP:

            count += 1

            if NP != []:

                # Generate image mask.
                cv_img_masks = filters.cv_img_mask(NP)

                # Detect points.
                arrPoints = rectDetector.detect(cv_img_masks)
                zones = rectDetector.get_cv_zonesBGR(frame, arrPoints)

                # find standart
                regionIds, stateIds, countLines = optionsDetector.predict(zones)
                regionNames = optionsDetector.getRegionLabels(regionIds)

                # find text with postprocessing by standart  
                textArr = textDetector.predict(zones)
                textArr = textPostprocessing(textArr, regionNames)
                #print(textArr)
                print(textArr)

                # if textArr != []:
                    # cv2.imwrite("dataset/carnum" + str(textArr) + "-"+  str(timestr) + ".jpeg", frame)
                    # os.remove(filename)

                # else:
                    # os.remove(filename)

            cv2.imshow('frame', frame)

            k = cv2.waitKey(100) & 0xff # Press 'ESC' for exiting video
            if k == 27:
                break
            elif count >= 1000: # Take 30 face sample and stop video
                break

# Do a bit of cleanup
print("\n [INFO] Exiting Program and cleanup stuff")
cam.release()
cv2.destroyAllWindows()