I am creating a face recognition attendance system in flask I am getting an error "Unsupported image type, must be 8bit gray or RGB image." In a function those generates face encodings from images for attendance tracking and matches them in real-time using a camera feed. It processes .jpg or .png images.
import cv2
import face_recognition
import pickle
def generate_frame():
# Check if encoding file exists
if not os.path.exists("EncodeFile.p"):
print("Generating encodings...")
img = cv2.imread("dummy.jpg") # Use a valid image file path
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
encodings = face_recognition.face_encodings(img_rgb)
if encodings:
with open("EncodeFile.p", "wb") as file:
pickle.dump([encodings, ["DummyID"]], file)
print("EncodeFile.p created!")
else:
print("No face found.")
return
# Load encodings
with open("EncodeFile.p", "rb") as file:
encoded_faces, student_ids = pickle.load(file)
# Start camera
capture = cv2.VideoCapture(0)
while True:
success, img = capture.read()
if not success: break
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
face_locations = face_recognition.face_locations(img_rgb)
face_encodings = face_recognition.face_encodings(img_rgb, face_locations)
for encoding in face_encodings:
matches = face_recognition.compare_faces(encoded_faces, encoding)
if True in matches:
print("Face recognized")
else:
print("Face not recognized")
cv2.imshow("Video", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
generate_frame()
This the function.