运行2个面部检测流程并仅存储第一个结果

时间:2015-08-17 21:15:14

标签: python opencv

我有一个程序用opencv处理视频以检测面部及其在框架中的位置,在opencv中你运行两个haar级联,一个用于面部前面,一个用于面部轮廓以确保你已经得到了一切,我想使用多处理在不同的核心上运行每个测试,并使用先找到的结果,杀死其他进程以节省时间。

我不确定如何只得到第一个结果并杀死其他进程,这样我就可以处理结果并移动到下一帧。

这是我提出的,主要是借用opencv示例:

import cv2
#import numpy as np
import time
import multiprocessing as mp

cascPath = '/usr/share/opencv/haarcascades/'
faceCascade = cv2.CascadeClassifier(cascPath+"haarcascade_frontalface_default.xml")
profileCascade = cv2.CascadeClassifier(cascPath+"haarcascade_profileface.xml")



def detect(cascade, gray, queue):
    faces = cascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(60, 60),
        flags=cv2.cv.CV_HAAR_FIND_BIGGEST_OBJECT # This is for the speed increase, pruning is also an option
    )
    if (len(faces>0)):
        queue.put(faces)
    else:
        queue.put(False)


if __name__ == "__main__":
    video_capture = cv2.VideoCapture(0)
    while True:
        ret, frame = video_capture.read()
        t = time.clock() # Timing tests

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        q = mp.Queue()
        p = mp.Process(target=detect, args=(faceCascade,gray,q,)).start()
        p2 = mp.Process(target=detect, args=(profileCascade,gray,q,)).start()

        faces = q.get()

        p.terminate()
        p2.terminate()
        # Draw a rectangle around the faces
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 1)

        # Display the resulting frame
        print (int(1/(time.clock()-t))) # More timing tests
        cv2.imshow('Video', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # When everything is done, release the capture
    video_capture.release()
    cv2.destroyAllWindows()

进程开始但我无法说服他们将任何内容放入队列中,然后所有内容都被queue.get()

阻止

0 个答案:

没有答案