我正在尝试对视频文件进行人脸检测。我正在使用openCV HAAR人脸检测器。我还通过合适的因子调整图像大小以实现更快的处理。我也使用了多个线程,但线程是顺序的。输出视频滞后很多。 我希望输出与输入帧一样快。有关如何提高o / p视频中帧速率的任何建议。 线程也可以并行使用吗?如果是,怎么样?
面部检测模块写在faceDetector.py文件中
样品给出25 fps。
该程序适用于webCam。
我的代码:
import cv2
from threading import Thread
from faceDetector import Face
cap = cv2.VideoCapture("sample2.mp4")
class T1(Thread):
def __init__(self, frame):
Thread.__init__(self)
self.frame = frame
self.imgResized = None
def run(self):
self.imgResized, self.faces = FaceDetection(self.frame)
class T2(Thread):
def __init__(self, img, faces):
Thread.__init__(self)
self.img = img
self.faces = faces
def run(self):
Faceobj.RectAroundFace(self.faces, self.img)
# cv2.imshow("faces in video", self.img)
class T3(Thread):
def __init__(self, img):
Thread.__init__(self)
self.img = img
def run(self):
cv2.imshow("faces in video", self.img)
def FaceDetection(img):
imgPreProcessed, imgResized = Faceobj.preprocessing(img)
faces = Faceobj.detectFace(imgPreProcessed)
return imgResized, faces
Faceobj = Face()
while (cap.isOpened):
ret, frame = cap.read()
# print cap.get(cv2.cv.CV_CAP_PROP_FPS)
# cap.set(cv2.cv.CV_CAP_PROP_FPS, 60)
if not ret :
break
thread_1 = T1(frame)
thread_1.start()
thread_1.join()
thread_2 = T2(thread_1.imgResized, thread_1.faces)
thread_2.start()
thread_3 = T3(thread_2.img)
thread_3.start()
thread_3.join()
if (cv2.waitKey(1) & 0xFF == 27):
break