1

別のスレッドでHAARカスケードを使用して車両を検出しようとしています。

これが私のコードの一部です:メインスレッド:

(isRead,cvImage)=cvCamSource.read()
lane1=Lane(cvCamSource,Lane.InflowLane)
print 'Blah Blah Blah!!'

実際のワーカースレッド:

class Lane(Thread):
    def __init__(self,camSource,laneType):
        Thread.__init__(self)
        self.cvCam=camSource
        self.setLaneMask()


    def run(self):
        self.detectVehiclesCount()


    #Just update the Count
    def detectVehiclesCount(self):
        cascade=cv2.CascadeClassifier(self.cascadeFile)
        frameCount=0
        sumOfVehicleCounts=0
        noOfFramesAveraged=1000
        while frameCount<=noOfFramesAveraged:
            print frameCount
            #Read Image
            (isRead,cvImage)=self.cvCam.read()
            if isRead==True:
                frameCount+=1
                grayscaleImage=cv2.cvtColor(cvImage,cv.CV_BGR2GRAY)
                #Apply Lane Mask
                laneImage=np.bitwise_and(grayscaleImage,self.laneMask)
                rects = cascade.detectMultiScale(laneImage, scaleFactor=1.2, minNeighbors=4, minSize=(30,30), maxSize=(200,200))
                sumOfVehicleCounts+=len(rects)
            else:
                raise self.NoInputFromWebcam, "Can't Read From Webcam Source!"

        self.countOfVehicles=int(sumOfVehicleCounts/noOfFramesAveraged)


    #Set the Lane Mask
    def setLaneMask(self):
        (isRead,cvImage)=self.cvCam.read()

        if isRead:
            grayscaleImage=cv2.cvtColor(cvImage,cv.CV_BGR2GRAY)
            self.laneMask=self.extract_lane(grayscaleImage)
##            print 'Mask Image Created Successfully!'
        else:
            raise self.NoInputFromWebcam, "Can't Read From Webcam Source!"


    """
    Input grayscale cvImage
    Output cvImage of Mask
    'Mouse Click' to mark points, 'Space' to Preview and 'Enter' to Finalize Mask
    """
    def extract_lane(self, grayscaleImage):
        overlayImage=np.copy(grayscaleImage)
        cv2.namedWindow('preview')
        cPts=[[]]
        cv2.setMouseCallback('preview',self.on_mouse,(cPts,overlayImage,grayscaleImage))
        opacity=0.4
        while True:
            displayImage=cv2.addWeighted(overlayImage,opacity,grayscaleImage,1-opacity,0)
            cv2.imshow('preview',displayImage)
            keyPressed=cv2.waitKey(5)
            if keyPressed==27:
                break
            elif keyPressed==32:
                cv2.drawContours(overlayImage,np.array(cPts),0,255)
                maskImage=np.zeros_like(grayscaleImage)
                cv2.drawContours(maskImage,np.array(cPts),0,255,-1)
                extractedImage=np.bitwise_and(grayscaleImage,maskImage)
                cv2.imshow('extractedImage',extractedImage)
            elif keyPressed==13:
                break
        cv2.destroyAllWindows()

        return maskImage


    #Mouse Event Listener
    """Left Click to Draw, Right Click to Reset"""
    def on_mouse(self, event, x, y, flags,(cPts,overlayImage,resetImage)):
        if event==cv.CV_EVENT_LBUTTONUP:
            cPts[0].append([x,y])
            cv2.circle(overlayImage,(x,y),5,(255),-1)
            if len(cPts[0])>1:
                oldPoint=cPts[0][len(cPts[0])-2]
                cv2.line(overlayImage,(oldPoint[0],oldPoint[1]),(x,y),255)
        elif event==cv.CV_EVENT_RBUTTONUP:
            cPts[0]=[]
            overlayImage[:]=resetImage[:]

Detectメソッドは、メインスレッドが終了するまでブロックし、その後「BlahBlahBlah」が出力されます。私は何が間違っているのですか?

4

0 に答える 0