-1

I try to make an attendance system via face recognition using computer vision technologies.

Python Version: 3.8.10

The code is given below,

import cv2
import numpy as np
import face_recognition
import os

path = 'attendanceImages'
images = []
classNames = []
myList = os.listdir(path)
print(myList)
for  cl in myList:
    curImg = cv2.imread(f"{path} / {cl}")
    images.append(curImg)
    classNames.append(os.path.splitext(cl)[0])
print(classNames)

def findEncodings(images):
    encodeList = []
    for img in images:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        encode = face_recognition.face_encodings(img)[0]
        encodeList.append(encode)
    return  encodeList

encodeListKnown = findEncodings(images)
print(len(encodeListKnown))  ##--> It would be printed the length of dataset 

Note:- After print(len(encodeListKnown)) print it would be show the length but for the error it's failed

The faced error is given below,

Traceback (most recent call last):
  File "/home/imdadul/PycharmProjects/faceVerification/AttendanceViaFace.py", line 25, in <module>
    encodeListKnown = findEncodings(images)
  File "/home/imdadul/PycharmProjects/faceVerification/AttendanceViaFace.py", line 20, in findEncodings
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.5.2) /tmp/pip-req-build-sl2aelck/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

['sohelRana.jpg', 'apurbo.jpg', 'RamCharan.jpg', 'ananyaPanday.jpg', 'imdadulHaque.jpg']
['sohelRana', 'apurbo', 'RamCharan', 'ananyaPanday', 'imdadulHaque']

Process finished with exit code 1

For checking Version of installed packages is given attached file, please concern there. enter image description here

Imdadul Haque
  • 1,635
  • 5
  • 23
  • 44
  • 1
    Did you check the images where loaded correctly? Your path construction for imread does not look correct. Try: `Path(os.path.abspath(path))/cl` (with `Path` from https://docs.python.org/3/library/pathlib.html?highlight=path#module-pathlib) – Tom Oct 08 '21 at 11:25
  • @Tom Thanks for your suggestions, it's solved! – Imdadul Haque Oct 08 '21 at 11:36
  • 1
    path issue, once again. please search for the error messages you get, before you ask a question. you would have found plenty of existing questions about this, and some even have answers. – Christoph Rackwitz Oct 08 '21 at 16:36
  • 1
    Does this answer your question? [imread returns None, violating assertion !\_src.empty() in function 'cvtColor' error](https://stackoverflow.com/questions/52676020/imread-returns-none-violating-assertion-src-empty-in-function-cvtcolor-er) – Christoph Rackwitz Oct 08 '21 at 16:37

1 Answers1

1

The problem is solved!

I made error in curImg = cv2.imread(f"{path}/{cl}"), this line. I should remove the spaces before and after from /. And that's it. Actually, it was not found the correct location and now it's solved!

Imdadul Haque
  • 1,635
  • 5
  • 23
  • 44