Search code examples
python-3.xlinuxvisual-studio-codemanjaro

EOF error in VS-code (not Working in terminal also)


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


Solution

  • 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