gpt4 book ai didi

python - 使用python和Opencv进行多处理或多线程以检测人脸

转载 作者:行者123 更新时间:2023-12-02 17:35:08 27 4
gpt4 key购买 nike

最近,我使用OPENCV + PYTHON开发树莓派3(OS raspbian),以在实时摄像机上检测面部。而且我发现在树莓派pi3上检测到的人脸使用opencv的速度非常慢,大约为4-5 FPS / s。所以我想知道我可以使用MultiThread或MultiProcessing来加快FPS的速度吗,如果可以,我该怎么做?

请帮助我,任何想法将不胜感激。谢谢

最佳答案

更新。我使用单独的线程从相机捕获图像,与串行比较,我看不到FPS的任何加速。请参阅我的代码。

import threading
import time
import cv2
import numpy as np
class myThread (threading.Thread):
def __init__(self, src):
print("thread -------------init-------------")
threading.Thread.__init__(self)
self.cap = cv2.VideoCapture(src)
self.stop = False
def run(self):
while(self.stop == False):
self.ret, self.frame = self.cap.read()

def Stop(self):
self.cap.release()
self.stop = True

def read(self):
return self.ret, self.frame

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer/trainer.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);

thread = myThread(0)
thread.start()
time.sleep(1)

start = time.time()
frames = 0
font = cv2.FONT_HERSHEY_SIMPLEX
cap = cv2.VideoCapture(0)
while(True):
ret, frame = thread.read()
frame = cv2.resize(frame, (640, 480))
frames += 1
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, 1.2, 5)
for(x,y,w,h) in faces:
# Create rectangle around the face
cv2.rectangle(frame, (x-20,y-20), (x+w+20,y+h+20), (0,255,0), 4)
Id, con = recognizer.predict(gray[y:y+h,x:x+w])
print(Id, con)

# Check the ID if exist
if(con < 60):
if(Id == 1):
Id = "HUY"
if(Id == 2):
Id = "HOA"
#If not exist, then it is Unknown
else:
#print(Id)
Id = "Unknow"

# Put text describe who is in the picture
cv2.rectangle(frame, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1)
cv2.putText(frame, str(Id), (x,y-40), font, 2, (255,255,255), 3)

if cv2.waitKey(10) & 0xFF == ord('q'):
thread.Stop()
break
cv2.imshow("frame", frame)
end = time.time()
second = end - start
print("second:", + second)
print(frames/second)
cv2.destroyAllWindows()

关于python - 使用python和Opencv进行多处理或多线程以检测人脸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51779893/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com