1

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()
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • 1
    Does this answer your question? [Face detection throws error: !empty() in function cv::CascadeClassifier::detectMultiScale](https://stackoverflow.com/questions/30857908/face-detection-throws-error-empty-in-function-cvcascadeclassifierdetectm) – Christoph Rackwitz Jun 05 '22 at 23:25
  • please take the [tour] and review [ask]. did you search for the error before posting? what specifically did you find? what debugging did you do of the code? – Christoph Rackwitz Jun 05 '22 at 23:25
  • 1
    Does cv2 can read Arduino? You can converted from c++ to opencv or python or any other language. One thing is that are you using laptop or pc? If yes. Then it should be cap=cv2.VideoCapture(1, cv2.CAP_DSHOW) – toyota Supra Jun 06 '22 at 10:40
  • @toyotaSupra I'm currently using a laptop for this and your fix sort of worked by prompting me with this error ```[ERROR:0@0.828] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap.cpp (299) cv::VideoCapture::open VIDEOIO(DSHOW): raised unknown C++ exception!``` I'm not really sure if I just have to point it somewhere else or not. – MasterChief4817 Jun 07 '22 at 06:06
  • @toyotaSupra When inputting ```cap=cv2.VideoCapture(1, cv2.CAP_DSHOW)``` I get this error "[ERROR:0@0.666] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap.cpp (299) cv::VideoCapture::open VIDEOIO(DSHOW): raised unknown C++ exception!" – MasterChief4817 Jun 10 '22 at 20:25
  • Are you using webcam or laptop comes with built-in camera? – toyota Supra Jun 11 '22 at 00:25
  • I am using a USB Webcam. It is the Logitech C920. – MasterChief4817 Jun 12 '22 at 03:29

0 Answers0