Witam wszystkich.
Od jakiegoś czasu pracuję nad programem który będzie wykrywać tablice rejestracyjne samochodów z wideo przekazywanego na żywo. Całość podzieliłem na dwie części. Pierwsza to wykrywanie samochodów poruszających się w zasięgu kamery, jeśli program wykryje pojazd zapisuje pojedynczą klatkę z wideo w formacie .jpg. Druga część polega na wykrywaniu i odczytywaniu samych tablic rejestracyjnych z samego obrazu. Kolejnym krokiem jest połączenie tych dwóch algorytmów tak aby mogły funkcjonować w jednym programie, tak aby zapisywane klatki wideo w czasie rzeczywistym były analizowane pod kątem wykrycia tablicy rejestracyjnej. I tutaj pojawia się moje pytanie, co zrobić aby połączyć oba algorytmy tak aby całość funkcjonowała, ponieważ przy dotychczasowych próbach program kończył działanie po odpaleniu pierwszego zdjęcia Co?

Kod który wykrywa pojazdy:

Kod

import cv2
import imutils
from PIL import Image
import numpy as np
import os
 
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
backsub = cv2.createBackgroundSubtractorMOG2()
cam = "rtsp://192.168.3.148"
 
capture = cv2.VideoCapture(cam)
 
i = 0
minArea=120
count = 0 
 
while True:
    ret, frame = capture.read()
    #frame = imutils.resize(frame, width=1000)
 
    fgmask = backsub.apply(frame, None, 0.2)
    erode=cv2.erode(fgmask,None,iterations=1)     #erosion to erase unwanted small contours
    moments=cv2.moments(erode,True)              
    area=moments['m00']   
 
    if moments['m00'] >=minArea:
 
        #cv2.line(frame,(150,300),(1200,100),(255,255,0),10)
 
 
        x=int(moments['m10']/moments['m00'])
        y=int (moments['m01']/moments['m00'])
        if x>150 and x<1200 and y>100 and y<300:       
            i=i+1
            print(i)
 
            cv2.imwrite("frame%d.jpg" % count, frame)      
            count += 1
 
 
        cv2.putText(frame,'Liczba: %r' %i, (10,30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
 
        cv2.imshow("Track", frame)
        cv2.imshow("background sub", fgmask)
 
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
 
capture.release()
cv2.destroyAllWindows()

Kod wykrywający tablice rejestracyjne (Źródło//github.com/MicrocontrollersAndMore/OpenCV_3_License_Plate_Recognition_Python + moje modyfikacje):

import cv2
import numpy as np
import os
import glob
import DetectChars
import DetectPlates
import PossiblePlate
 
SCALAR_BLACK = (0.0, 0.0, 0.0)
SCALAR_WHITE = (255.0, 255.0, 255.0)
SCALAR_YELLOW = (0.0, 255.0, 255.0)
SCALAR_GREEN = (0.0, 255.0, 0.0)
SCALAR_RED = (0.0, 0.0, 255.0)
 
showSteps = False
 
def main():
 
    blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN()       
 
    if blnKNNTrainingSuccessful == False:                              
        print "\nerror: KNN traning was not successful\n"              
        return
 
    imgOriginalScene  = cv2.imread('frame32.jpg')        
 
    if imgOriginalScene is None:                        
        print "\nerror: image not read from file \n\n"     
        os.system("pause")                                
        return                                            
 
    listOfPossiblePlates = DetectPlates.detectPlatesInScene(imgOriginalScene)  
 
    listOfPossiblePlates = DetectChars.detectCharsInPlates(listOfPossiblePlates)    
 
    cv2.imshow("imgOriginalScene", imgOriginalScene) 
 
    if len(listOfPossiblePlates) == 0:                  
        print "\nno license plates were detected\n"        
    else:                                                            
 
        listOfPossiblePlates.sort(key = lambda possiblePlate: len(possiblePlate.strChars), reverse = True)
 
 
        licPlate = listOfPossiblePlates[0]
 
        #cv2.imshow("imgPlate", licPlate.imgPlate)  
        cv2.imshow("imgThresh", licPlate.imgThresh)
 
        if len(licPlate.strChars) == 0:                    
            print "\nBrak znalezionych symboli\n\n"     
            return                                         
        # end if
 
        drawRedRectangleAroundPlate(imgOriginalScene, licPlate)             
 
        print "\nlicense plate read from image = " + licPlate.strChars + "\n"     
        print "----------------------------------------"
 
        writeLicensePlateCharsOnImage(imgOriginalScene, licPlate)    
 
        #cv2.imshow("Detekcja", imgOriginalScene)          
        cv2.imwrite("Tablica rejestracyjna.jpg",licPlate.imgPlate)
        cv2.imwrite("Detekcja.jpg", imgOriginalScene)     
 
 
    cv2.waitKey(0)				
 
    return
# end main
 
 
def drawRedRectangleAroundPlate(imgOriginalScene, licPlate):
 
    p2fRectPoints = cv2.boxPoints(licPlate.rrLocationOfPlateInScene)            # get 4 vertices of rotated rect
 
    cv2.line(imgOriginalScene, tuple(p2fRectPoints[0]), tuple(p2fRectPoints[1]), SCALAR_RED, 2)         # draw 4 red lines
    cv2.line(imgOriginalScene, tuple(p2fRectPoints[1]), tuple(p2fRectPoints[2]), SCALAR_RED, 2)
    cv2.line(imgOriginalScene, tuple(p2fRectPoints[2]), tuple(p2fRectPoints[3]), SCALAR_RED, 2)
    cv2.line(imgOriginalScene, tuple(p2fRectPoints[3]), tuple(p2fRectPoints[0]), SCALAR_RED, 2)
 
 
 
def writeLicensePlateCharsOnImage(imgOriginalScene, licPlate):
    ptCenterOfTextAreaX = 0                             # this will be the center of the area the text will be written to
    ptCenterOfTextAreaY = 0
 
    ptLowerLeftTextOriginX = 0                          # this will be the bottom left of the area that the text will be written to
    ptLowerLeftTextOriginY = 0
 
    sceneHeight, sceneWidth, sceneNumChannels = imgOriginalScene.shape
    plateHeight, plateWidth, plateNumChannels = licPlate.imgPlate.shape
 
    intFontFace = cv2.FONT_HERSHEY_SIMPLEX                      # choose a plain jane font
    fltFontScale = float(plateHeight) / 30.0                    # base font scale on height of plate area
    intFontThickness = int(round(fltFontScale * 1.5))           # base font thickness on font scale
 
    textSize, baseline = cv2.getTextSize(licPlate.strChars, intFontFace, fltFontScale, intFontThickness)        # call getTextSize
 
            # unpack roatated rect into center point, width and height, and angle
    ( (intPlateCenterX, intPlateCenterY), (intPlateWidth, intPlateHeight), fltCorrectionAngleInDeg ) = licPlate.rrLocationOfPlateInScene
 
    intPlateCenterX = int(intPlateCenterX)              # make sure center is an integer
    intPlateCenterY = int(intPlateCenterY)
 
    ptCenterOfTextAreaX = int(intPlateCenterX)         # the horizontal location of the text area is the same as the plate
 
    if intPlateCenterY < (sceneHeight * 0.75):                                                  # if the license plate is in the upper 3/4 of the image
        ptCenterOfTextAreaY = int(round(intPlateCenterY)) + int(round(plateHeight * 1.6))      # write the chars in below the plate
    else:                                                                                       # else if the license plate is in the lower 1/4 of the image
        ptCenterOfTextAreaY = int(round(intPlateCenterY)) - int(round(plateHeight * 1.6))      # write the chars in above the plate
    # end if
 
    textSizeWidth, textSizeHeight = textSize                # unpack text size width and height
 
    ptLowerLeftTextOriginX = int(ptCenterOfTextAreaX - (textSizeWidth / 2))           # calculate the lower left origin of the text area
    ptLowerLeftTextOriginY = int(ptCenterOfTextAreaY + (textSizeHeight / 2))          # based on the text area center, width, and height
 
            # write the text on the image
    cv2.putText(imgOriginalScene, licPlate.strChars, (ptLowerLeftTextOriginX, ptLowerLeftTextOriginY), intFontFace, fltFontScale, SCALAR_YELLOW, intFontThickness)
    np.savetxt('Numery.txt', ["Numer rejestracyjny: %s" %licPlate.strChars], fmt='%s')
 
if __name__ == "__main__":
    main()

Co do drugiego algorytmu pojawia się jeszcze jeden problem ponieważ nie mogę go ustawić tak aby otwierał wszystkie pliki(zdjęcia) z danego folderu, działa jedynie przy wskazaniu docelowego pojedynczego pliku, próbowałem za pomocą argparase argument, jednak po uruchomieniu programu nic się wówczas nie dzieje.

Czy ktoś bardziej doświadczony ode mnie mógłby podsunąć poradę/ rozwiązanie Uśmiech ?