OpenCV Python在线程中运行HAAR级联检测

时间:2013-03-22 03:58:44

标签: python opencv python-multithreading

我试图在一个单独的线程中使用HAAR Cascade检测车辆。

这是我的一些代码: 主线:

(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[:]

检测方法会阻塞主线程直到完成,然后“Blah Blah Blah”打印。 我做错了什么?

0 个答案:

没有答案