-1

I tried removing gray[y:y+h,x:x+w] in imwrite line and the code works amazing but as soon as I add this [y:y+h,x:x+w] it says: y is not defined. I just don't understand how can this error message!!!

from picamera.array import PiRGBArray

from picamera import PiCamera

import cv2

import time

Id=raw_input('enter your id')

sampleNum=0

camera = PiCamera()

camera.resolution = (320, 240)

camera.framerate = 30

rawCapture = PiRGBArray(camera, size=(320, 240))

display_window = cv2.namedWindow("Faces")

face_cascade = cv2.CascadeClassifier('/home/pi/FR22/haarcascade_frontalface_default.xml')

time.sleep(1)

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):

image = frame.array

#FACE DETECTION STUFF
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 5)
for (x,y,w,h) in faces:
    cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
sampleNum=sampleNum+1
#DISPLAY TO WINDOW
cv2.imwrite("dataSet/User."+Id +'.'+ str(sampleNum) + ".jpg", gray[y:y+h,x:x+w])
cv2.imshow("Faces", image)
key = cv2.waitKey(1)

rawCapture.truncate(0)

if key == 27:
    camera.close()
    cv2.destroyAllWindows()
elif sampleNum>20:
    break
  • 2
    Welcome to Raspberry Pi! Please take the tour and visit the helpcenter to see how things work here. Any good reasons why you reverted my edit to improve your code formatting? – Ghanima Apr 9 '17 at 11:42
3

y is out of scope after leaving the for-loop which is why you get the not defined error with your imwrite. Check your intented program flow and indentation.

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

# x, y, w, h are out of scope here    

sampleNum=sampleNum+1
#DISPLAY TO WINDOW
cv2.imwrite("dataSet/User."+Id +'.'+ str(sampleNum) + ".jpg", gray[y:y+h,x:x+w])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.