Python: Anaconda3 Python 3.8.5 OpenCV: 4.5.1
I compiled OpenCV and OpenCV-Contrib with CMake so I could use the cuda
functions in python.
Now I am trying to detect faces with the cv2.cuda_CascadeClassifier().detectMultiScale()
function.
When I run it everything works fine until the line :gpu_result = model.classifier_cuda.detectMultiScale(gpu_frame)
also marked it with the **code**
.
Once this line gets executed the programm just stops.
I have put the output from the terminal from start to end below my code.
My Code:
import time
start_time = time.time()
import cv2
import os
clear = lambda: os.system('cls')
def printMessageWithTime(message):
print(f"[INFO] {message} - Time: {time.time() - start_time}s")
def most_frequent_element_in_list(list):
counter = 0
num = list[0]
for i in list:
curr_frequency = list.count(i)
if(curr_frequency>counter):
counter = curr_frequency
num = i
return num
printMessageWithTime("Programm started, packages loaded!")
print("OpenCV Version: " + cv2.__version__)
class Model():
def __init__(self, modelNumber):
self.trainers_path = "C:/Users/Student/Documents/models"
self.trainer_main_name = "_percent_trainer"
self.trainer_names = ["1","5","10"]
self.model_number = modelNumber
self.choosen_trainer = f"{self.trainers_path}/{self.trainer_names[self.model_number]}{self.trainer_main_name}.xml"
self.cascadePath = "C:/Users/Student/Documents/repos/ai_face_recognition/ai_face_recognition/testing/haarcascade_frontalface_default.xml" #cv2.data.haarcascades +
self.cudacascadePath = "C:/Users/Student/Desktop/new build/opencv-4.5.1/data/haarcascades_cuda/haarcascade_frontalface_default.xml"
self.faceCascade = cv2.CascadeClassifier(self.cascadePath)
self.classifier_cuda = cv2.cuda_CascadeClassifier(self.cudacascadePath)
self.recognizer = cv2.face.LBPHFaceRecognizer_create()
self.recognizer.read(self.choosen_trainer)
model = Model(0)
printMessageWithTime("Model data loaded!")
class Camera():
def __init__(self, resolution):
self.width = resolution[0]
self.height = resolution[1]
self.cam = cv2.VideoCapture(0)
self.cam.set(3,self.width)
self.cam.set(4,self.height)
self.minW = 0.25*self.cam.get(3)
self.minH = 0.25*self.cam.get(4)
Cam = Camera([640,480])
printMessageWithTime("Camera set up!")
detected_ids = []
font = cv2.FONT_HERSHEY_SCRIPT_SIMPLEX
while True:
start_time = time.time()
ret, img = Cam.cam.read()
gray_frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gpu_frame = cv2.cuda_GpuMat(gray_frame)
printMessageWithTime("Detecting")
** gpu_result = model.classifier_cuda.detectMultiScale(gpu_frame)**
printMessageWithTime("Detecting finished")
result = gpu_result.download() # UMat
id = 0
confidence = 0
for(x,y,w,h) in result:
cv2.rectangle(img, (x,y),(x+w,y+h), (0,255,0), 2)
id, confidence = model.recognizer.predict(gray_frame[y:y+h,x:x+w])
if confidence < 100:
id = str(id)
else:
id = "unknown"
confidence = " {0}%".format(round(100-confidence))
cv2.putText(img, str(id), (x+5,y-5),font,1,(255,255,255),2)
cv2.putText(img,str(confidence),(x+5,y+h-5),font,1,(255,255,0),1)
detected_ids.insert(0,id)
if len(detected_ids) > 40:
del detected_ids[0]
most_frequent_id = most_frequent_element_in_list(detected_ids)
end_time = time.time()
try:
fps = 1 / (end_time - start_time)
except:
print("Can not calculate fps, due to division by 0!")
print(f"[INFO] Time for detection: {end_time - start_time}, Detected Person: {most_frequent_id} - Confidence: {confidence}")
cv2.imshow('camera',img)
k = cv2.waitKey(10) & 0xff
if k == 27:
break
Cam.cam.release()
cv2.destroyAllWindows()
print("\n[INFO] Exiting program")
Terminal Output:
PS C:\Users\Student\Documents\repos\ai_face_recognition\ai_face_recognition> & C:/Users/Student/anaconda3/python.exe c:/Users/Student/Documents/repos/ai_face_recognition/ai_face_recognition/recognizer/recognizer_2.py
[INFO] Programm started, packages loaded! - Time: 0.4229726791381836s
OpenCV Version: 4.5.1
[INFO] Model data loaded! - Time: 11.675345182418823s
[INFO] Camera set up! - Time: 41.83551025390625s
[INFO] Detecting - Time: 1.9768860340118408s
PS C:\Users\Student\Documents\repos\ai_face_recognition\ai_face_recognition>
I tried different ways of creating the Frame for the detectMultiScale function as mentioned in this question: "Cuda CascadeClassifier detectMultiScale output is unreadable. Aswell as in this question: cv2.cuda_CascadeClassifier in python But the difference is between their and my problem, I do not get any error at all, and all the solutions described do not work.