Ask Your Question
0

im am getting error in opencv

asked 2017-08-22 03:32:48 -0500

Aneel gravatar image

while creating facerecognition i got following error after creating detector file, i am completl beginer to pthon nad open cv

Error

    Traceback (most recent call last):
  File "C:\Users\Aneel\Desktop\project\facerecognitation\detector.py", line 14, in <module>
    gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
error: ..\..\..\..\opencv\modules\imgproc\src\color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor

my actual code

   import cv2
import numpy as np

recognizer = cv2.createLBPHFaceRecognizer()
recognizer.load('trainner/trainner.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);


cam = cv2.VideoCapture(0)
font = cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 1, 1)
while True:
    ret, im =cam.read()
    gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    faces=faceCascade.detectMultiScale(gray, 1.2,5)
    for(x,y,w,h) in faces:
        cv2.rectangle(im,(x,y),(x+w,y+h),(225,0,0),2)
        Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
        if(conf<50):
            if(Id==1):
                Id="Anirban"
            elif(Id==2):
                Id="Sam"
        else:
            Id="Unknown"
        cv2.cv.PutText(cv2.cv.fromarray(im),str(Id), (x,y+h),font, 255)
    cv2.imshow('im',im) 
    if cv2.waitKey(10) & 0xFF==ord('q'):
        break
cam.release()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-08-22 03:46:31 -0500

berak gravatar image

your image is empty. probably the VideoCapture did not open correctly.

please check:

cam = cv2.VideoCapture(0)
if not cam.isOpened(): raise Exception("could not open camera")

and later:

    ret, im =cam.read()
    if not ret: break # no image

then, please avoid using deprecated cv2.cv things, they're entirely gone in recent opencv, use cv2.putText() and cv2.FONT_HERSHEY_SIMPLEX instead.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-08-22 03:30:51 -0500

Seen: 106 times

Last updated: Aug 22 '17