Ask Your Question
0

Traceback (most recent call last): File "./Documents/S.py", line 36, in <module> roi_gray, NameError: name 'roi_gray' is not defined

asked 2019-12-13 08:01:55 -0600

updated 2019-12-13 08:04:00 -0600

berak gravatar image

I have this error when i run this code:

import cv2

cascPath = "/usr/local/lib/python2.7/site-packages/h.xml"
eyePath = "/usr/local/lib/python2.7/site-packages/e.xml"
smilePath = "/usr/local/lib/python2.7/site-packages/le.xml"

faceCascade = cv2.CascadeClassifier(cascPath)
eyeCascade = cv2.CascadeClassifier(eyePath)
smileCascade = cv2.CascadeClassifier(smilePath)

font = cv2.FONT_HERSHEY_SIMPLEX
video_capture = cv2.VideoCapture(0)

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

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

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(200, 200),
        flags=cv2.CASCADE_SCALE_IMAGE
    )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 3)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = frame[y:y+h, x:x+w]
        cv2.putText(frame,'Face',(x, y), font, 2,(255,0,0),5)

    smile = smileCascade.detectMultiScale(
        roi_gray,
        scaleFactor= 1.16,
        minNeighbors=35,
        minSize=(25, 25),
        flags=cv2.CASCADE_SCALE_IMAGE
    )

    for (sx, sy, sw, sh) in smile:
        cv2.rectangle(roi_color, (sh, sy), (sx+sw, sy+sh), (255, 0, 0), 2)
        cv2.putText(frame,'Smile',(x + sx,y + sy), 1, 1, (0, 255, 0), 1)

    eyes = eyeCascade.detectMultiScale(roi_gray)
    for (ex,ey,ew,eh) in eyes:
        cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
        cv2.putText(frame,'Eye',(x + ex,y + ey), 1, 1, (0, 255, 0), 1)

    cv2.putText(frame,'Number of Faces : ' + str(len(faces)),(40, 40), font, 1,(255,0,0),2)      
    # Display the resulting frame
    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
      break

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2019-12-13 14:42:32 -0600

supra56 gravatar image

updated 2019-12-13 14:45:56 -0600

@CAPSLOCK. Your code isn't working properly. You're missing something. Do not set values too high.

#!usr/bin/python37

#OpenCV 4.1.2, Raspberry p[i 3/4. Buster ver10
#Date: 13th December, 2019

import cv2 as cv

cascade_face = cv.CascadeClassifier('haarcascade_frontalface_default.xml') 
cascade_eye = cv.CascadeClassifier('haarcascade_eye.xml') 
cascade_smile = cv.CascadeClassifier('haarcascade_smile.xml')

cap = cv.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX

def detected(img, gray):
    #Face
    face = cascade_face.detectMultiScale(gray, 1.3, 5)
    for x, y, w, h in face:
        cv.rectangle(img, (x, y), (x+we, y+h),(255, 130, 0), 2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[ye:y+h, x_:x+w]

        #For Eyes
        eye = cascade_eye.detectMultiScale(roi_gray, 1.2, 18)
        cv.putText(img, 'Face{}'.format(x, y), font, 2, (255,0,0),5)
        for (ex, ey, ew, eh) in eye:
            cv.rectangle(roi_color,(ex, ey),
                        (ex+ew,ey+eh),(0, 180, 60), 2)

        #Smile
        smile = cascade_smile.detectMultiScale(roi_gray, 1.7, 20)
        for (sx, sy, sw, sh) in smile: 
            cv.rectangle(roi_color,(sx, sy),
                        (sx+sw, sy+sh), (255, 0, 130), 2)

    return img  

while True:
    img = cap.read() 
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) 
    result = detectied(gray, img) 
    cv.imshow('Detected', result) 
    if cv.waitKey(1) & 0xFF == ord('q'):
        break 

cap.release() 
cv.destroyAllWindows()
edit flag offensive delete link more

Comments

Btw, I didn't add extra putText for eye and smile.

supra56 gravatar imagesupra56 ( 2019-12-13 14:46:32 -0600 )edit
0

answered 2019-12-13 08:11:45 -0600

mvuori gravatar image

You need to tell _where_ in the code you get that. But let me guess. faceCascade.detectMultiScale fails to detect anything, thus the next for loop is skippef, tge variable remains undefined as parameter for smileCascade.detectMultiScale

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-12-13 08:01:55 -0600

Seen: 1,468 times

Last updated: Dec 13 '19