I'm currently attempting to run the following program which I found here. The program gets my webcam footage, track the position of a detected face, and makes an arduino move servo motors to track the face. I'm using pycharm Community Edition 2022.1.1 with Python 3.8 and with packages setuptools (V 41.2.0), pyserial (V 3.5), pip (V 222.1.1), opencv-python (V 4.5.5.64), and numpy (V 1.22.4) installed.
Upon run it gives me this error
C:\Users\MicrosoftIsStupid\AppData\Local\Programs\Python\Python38\python.exe C:/Users/MicrosoftIsStupid/PycharmProjects/pythonProject1/main.py
Traceback (most recent call last):
File "C:/Users/MicrosoftIsStupid/PycharmProjects/pythonProject1/main.py", line 18, in <module>
face = face_cascade.detectMultiScale(gray, 1.1 , 6)
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
[ WARN:0@1.596] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (539) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
Process finished with exit code 1
The current code I'm using is below
#Face tracker using OpenCV and Arduino
#by Shubham Santosh
import cv2
import serial,time
face_cascade= cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap=cv2.VideoCapture(1)
#fourcc= cv2.VideoWriter_fourcc(*'XVID')
ArduinoSerial=serial.Serial('COM1',9600,timeout=0.1)
#out= cv2.VideoWriter('face detection4.avi',fourcc,20.0,(640,480))
time.sleep(1)
while cap.isOpened():
ret, frame= cap.read()
frame=cv2.flip(frame,1) #mirror the image
#print(frame.shape)
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
face = face_cascade.detectMultiScale(gray, 1.1 , 6)
for x,y,w,h in face:
#sending coordinates to Arduino
string='X{0:d}Y{1:d}'.format((x+w//2),(y+h//2))
print(string)
ArduinoSerial.write(string.encode('utf-8'))
#plot the center of the face
cv2.circle(frame,(x+w//2,y+h//2),2,(0,255,0),2)
#plot the roi
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),3)
#plot the squared region in the center of the screen
cv2.rectangle(frame,(640//2-30,480//2-30),
(640//2+30,480//2+30),
(255,255,255),3)
#out.write(frame)
cv2.imshow('img',frame)
#cv2.imwrite('output_img.jpg',frame)
'''for testing purpose
read= str(ArduinoSerial.readline(ArduinoSerial.inWaiting()))
time.sleep(0.05)
print('data from arduino:'+read)
'''
# press q to Quit
if cv2.waitKey(10)&0xFF== ord('q'):
break
cap.release()
cv2.destroyAllWindows()