Error in face detection code

please help me with error

the code is given:

Read a Video Stream from Camera(Frame by Frame)

import cv2

cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(“harcascade_frontalface_alt.xml”)

while True:
ret,frame = cap.read()
gray_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

if ret == False:
	continue

faces=face_cascade.detectMultiScale(gray_frame,1.3,5)

for (x,y,w,h) in faces:
	cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)

cv2.imshow("Video Frame",frame)
cv2.imshow("Gray Frame",gray_frame)
for (x,y,w,h) in faces:
	cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)

#Wait for user input - q, then you will stop the loop
key_pressed = cv2.waitKey(1) & 0xFF
if key_pressed == ord('q'):
	break

cap.release()
cv2.destroyALlWindows()

“”"
scaleFactor – Parameter specifying how much the image size is reduced at each image scale.

Basically the scale factor is used to create your scale pyramid. More explanation can be found here. In short, as described here, your model has a fixed size defined during training, which is visible in the xml. This means that this size of face is detected in the image if present. However, by rescaling the input image, you can resize a larger face to a smaller one, making it detectable by the algorithm.

1.05 is a good possible value for this, which means you use a small step for resizing, i.e. reduce size by 5%, you increase the chance of a matching size with the model for detection is found. This also means that the algorithm works slower since it is more thorough. You may increase it to as much as 1.4 for faster detection, with the risk of missing some faces altogether.

minNeighbors – Parameter specifying how many neighbors each candidate rectangle should have to retain it.

This parameter will affect the quality of the detected faces. Higher value results in less detections but with higher quality. 3~6 is a good value for it.

“”"

I happens sometime when you copy paste the code. The reason is there might be spaces mixed in with your tabs.
Try doing a search & replace to replace all tabs with a few spaces.
or eliminate all spaces and use complete tabs or use all tabs and no spaces for the indentation(python syntax format) of your code.

This might also help:

ahh that was solved but there is a new problem now

Pls make sure there is enough light for webcam to recognize ur face and if even then it doesn’t work then Pls send ur complete code .py file.

Hey Aastha,
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.