opencv-python \ opencv \ modules \ imgproc \ src \ color. cpp: 182: error: (-215: Assertion failed)! _sr c .empty () в функции 'cv :: cvtColor', только cam легкий открытый - PullRequest
2 голосов
/ 25 мая 2020

У меня ошибка ниже, когда я запускаю свой код. Оба erorr отображаются во время выполнения

    Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python35\lib\idlelib\run.py", line 119, in main
        seq, request = rpc.request_queue.get(block=True, timeout=0.05)
      File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python35\lib\queue.py", line 172, in get
        raise Empty
    queue.Empty

    Traceback (most recent call last):
      File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 1550, in __call__
        return self.func(*args)
      File "C:\Users\Lenovo\Desktop\Face recognise attandace system Succes.py", line 89, in TakeImages
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor

Вот мой код. Когда я запускал это, у меня было так много ошибок, о которых я упоминал выше. Когда я пытаюсь обработать основное исключение, возникает другое исключение. Я также упомянул их трассировку стека.

def TakeImages():         
    Id =(txt.get())  
    name =(txt2.get()) 
    if(is_number(Id) and name.isalpha()):
        cam = cv2.VideoCapture(1)
        harcascadePath ="haarcascade_frontalface_default.xml"
        detector = cv2.CascadeClassifier(harcascadePath)  
        sampleNum = 0 
        while(True):
            ret,img = cam.read()
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  
            faces = detector.detectMultiScale(gray, 1.3,5) 
            for (x, y, w, h) in faces:
                cv2.rectangle(img, (x, y), ( x + w, y + h), (255, 0, 0), 2)
                sampleNum=sampleNum + 1
                cv2.imwrite("TrainingImages"+name +"."+Id +'.'+ str(sampleNum) + ".jpg", gray[y:y + h, x:x + w]) 
                cv2.imshow('frame', img) 
            if cv2.waitKey(100) & 0xFF == ord('q'):
                            break
            elif sampleNum>60:
               break
        cam.release() 
        cv2.destroyAllWindows()
        res = "Images Saved for ID: " + Id +" Name: "+ name   
        row = [Id, name]  
        with open('studentDetails\StudentDetails.csv','a+') as csvFile:
            writer = csv.writer(csvFile) 
            writer.writerow(row)
        csvFile.close() 
        message.configure(text = res)
    else:
        if(is_number(Id)):
                res = "Enter Alphabetical Name"
                message.configure(text = res) 
        if(name.isalpha()): 
                res = "Enter Numeric Id"
                message.configure(text = res)
...