0

I am writing code for my deep learning project and when getting error while running facial detection.

I am building facial recognition system and I am getting error

NameError: name 'x' is not defined

while running the code, but I am not able to find root source of these problem.

import numpy as np
import cv2

CascadeClassifier = cv2.CascadeClassifier(r'C:\Users\ESEC\Desktop\opencv\aditya\cascades\haarcascades\haarcascade_frontalface_alt2.xml')

cap = cv2.VideoCapture(0)


while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()



    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = CascadeClassifier.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)

    for (x, y, w, h) in faces:
        print(x,y,w,h)
        roi_gray = gray[y:y+h, x:x+w]

        roi_color = gray[y:y+h, x:x+w]

        img_item = 'image.png'
        cv2.imwrite(img_item, roi_gray)
    # Our operations on the frame come here

    color = (255,0,0) #BGR 0=255

    stroke = 2


    end_cord_x = x + w

    end_cord_y = y + h

    cv2.rectangle(frame, (x, y), (end_cord_x, end_cord_y), color, stroke)

    # Display the resulting frame
    cv2.imshow('frame',frame)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break

     # When everything done, release the capture
     cap.release()
     cv2.destroyAllWindows()

It should be running smoothly and should be showing rectangle around the face but I am getting this following error.

File "camera-test.py", line 34, in <module>
end_cord_x = x + w
NameError: name 'x' is not defined
IMParasharG
  • 1,869
  • 1
  • 15
  • 26
  • 1
    what is the problem exactly? you never define x, as the error says. At one point, x is a variable in the for loop, but youre trying to reference it outside of that. – Banana Jul 18 '19 at 11:54
  • I want to get rectangle around my face but i am getting error that Name 'x' is not defined –  Jul 18 '19 at 13:21
  • Of course you get that error, because it IS not defined. Unfortunately I dont understand what x is supposed to be, maybe someone else can help. – Banana Jul 18 '19 at 13:52
  • `x` is only defined at that place if `faces` wasn't empty. Because then the ``for`` loop doesn't run and the loop variables, among them `x`, are not defined. – BlackJack Jul 18 '19 at 15:59

0 Answers0