Keep in mind I'm very new to coding and find this very difficult, but my open cv code is running super slow, and i have checked task manager and the cpu is running at less than ten percent of it's max usage, so it's not lack of processing power. Haar_face.xml is just opencv's haar cascade frontal face default. the slow thing is one, it starts up really slowly and two, it returns about one frame every five seconds, which is far too slow and i'm not sure why. any help appreciated.
import cv2 as cv
while True:
wcapture = cv.VideoCapture(0)
ret, frame = wcapture.read()
imgGray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
cv.destroyAllWindows()
face_haar = cv.CascadeClassifier('haar_face.xml')
faces_rect = face_haar.detectMultiScale(imgGray, scaleFactor=1.1, minNeighbors=3)
print({len(faces_rect)})
for (x,y,w,h) in faces_rect:
cv.rectangle(frame, (x,y), (x w,y h), (0,255,0), thickness=2)
cv.imshow('Detected Faces', frame)
if cv.waitKey(1) & 0xFF==ord('d'):
break
wcapture.release()
cv.destroyAllWindows()
CodePudding user response:
I don't have opencv installed on the machine I'm currently on, but moving the creation of the cv.VideoCapture
out of the loop and not destroying all windows each time should speed up the execution:
import cv2 as cv
wcapture = cv.VideoCapture(0)
while True:
ret, frame = wcapture.read()
if ret:
imgGray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
face_haar = cv.CascadeClassifier('haar_face.xml')
faces_rect = face_haar.detectMultiScale(imgGray, scaleFactor=1.1, minNeighbors=3)
print({len(faces_rect)})
for (x,y,w,h) in faces_rect:
cv.rectangle(frame, (x,y), (x w,y h), (0,255,0), thickness=2)
cv.imshow('Detected Faces', frame)
if cv.waitKey(1) & 0xFF==ord('d'):
break
wcapture.release()
cv.destroyAllWindows()
CodePudding user response:
first of all remove print statement that slows down the execution of code alot. Also python is generally slower in execution if you can get this code in C or C that will make it faster.