1

I use vs-code on Manjaro ann I've had numerous issues with it explained in this question following VS- CODE errors on manjaro, auto quit, can't open folder

I have the following code

import cv2
from random import randrange
#load data
trained_face_data = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

#Choose image
webcam = cv2.VideoCapture(0)

while True:
    successful_frame_read, frame = webcam.read()

    #convert to greyscale
    greyscaled_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)


    #detect faces
    face_coordinates = trained_face_data.detectMultiScale(greyscaled_img)

    #Draw a rectangle around the Face
    for (x, y, w, h) in face_coordinates:
        cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 0, 10)
    
    #Display the image with the faces spotted
    cv2.imshow('Face detector', frame)
    key = cv2.waitKey(1)

    #stop if Q is pressed
    if key==81 or key==113:
    break

webcam.release()
print("code completed")

In the beginning, I had a syntax error at

  File "Face_detector.py", line 24
    cv2.imshow('Face detector', frame)
    ^
SyntaxError: invalid syntax

I commented out the line but got the same error in the next line

  File "Face_detector.py", line 25
    key = cv2.waitKey(1)
    ^
SyntaxError: invalid syntax

So I went ahead and commented out every sing line till the end and now I got an EOF error

 File "Face_detector.py", line 33

                            ^
SyntaxError: unexpected EOF while parsing

I tried running the script from the terminal but still has the exact same error

Here is an image of running the uncommented code from the terminal

Here is an image of running the final commented out code from the terminal

1 Answers1

0

You are missing a parentheses at this line:

#Draw a rectangle around the Face
for (x, y, w, h) in face_coordinates:
    cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 0, 10) # <-- Missing parentheses here
sanitizedUser
  • 1,723
  • 3
  • 18
  • 33
  • Okay, So I fixed it But now I have this error. I believe there's something wrong with the open cv implementation. I'd appreciate it if you'd like to help me – maskedpirate Aug 23 '20 at 14:17
  • Traceback (most recent call last): File "/home/smokedpirate/Documents/1. Python_learning/3_recognition/Realtime_face_detector/Face_detector.py", line 17, in face_coordinates = trained_face_data.detectMultiScale(greyscaled_img) cv2.error: OpenCV(4.4.0) /tmp/pip-req-build-6179nsls/opencv/modules/objdetect/src/cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'detectMultiScale' – maskedpirate Aug 23 '20 at 14:19
  • I suggest looking at some of the answers [here](https://stackoverflow.com/questions/30508922/error-215-empty-in-function-detectmultiscale). – sanitizedUser Aug 23 '20 at 19:21