ageitgey / face_recognition

The world's simplest facial recognition api for Python and the command line
MIT License
52.85k stars 13.43k forks source link

File "C:\Users\hp\PycharmProjects\face-reco-attendence\.venv\Lib\site-packages\face_recognition\api.py", line 105, in _raw_face_locations return face_detector(img, number_of_times_to_upsample) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: Unsupported image type, must be 8bit gray or RGB image. #1578

Open Ananya221203 opened 1 month ago

Ananya221203 commented 1 month ago

Description

Describe what you were trying to get done. Tell us what happened, what went wrong, and what you expected to happen. IMPORTANT: If your issue is related to a specific picture, include it so others can reproduce the issue.

What I Did

import os import cv2 import face_recognition

Function to load images from a folder and encode them

def load_and_encode_images(folderPath): PathList = os.listdir(folderPath) print("Found images:", PathList)

imgList = []
studentIds = []

for path in PathList:
    img_path = os.path.join(folderPath, path)
    img = cv2.imread(img_path)

    if img is None:
        print(f"Error loading image: {img_path}")
        continue

    # Convert image to RGB format
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    imgList.append(img_rgb)
    studentIds.append(os.path.splitext(path)[0])

encodeList = []
for img in imgList:
    try:
        # Ensure image is 8-bit RGB or grayscale
        if img.shape[2] != 3:
            print(f"Skipping image {img}: not 8-bit RGB")
            continue

        encode = face_recognition.face_encodings(img)[0]
        encodeList.append(encode)
    except IndexError:
        print(f"No face found in image: {img}")
        # You can choose to skip this image or handle the error as needed

return encodeList, studentIds

Example usage

folderPath = 'Images' print("Encoding started .......") encodeListKnown, studentIds = load_and_encode_images(folderPath) print("Encoding complete")

Print the results

print("Encoded faces:", encodeListKnown) print("Student IDs:", studentIds)

Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
modamaan commented 2 weeks ago

change this: rgb_small_frame = small_frame[:, :, ::-1] to rgb_small_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)

numpy==1.26.3 opencv-python==4.9.0.80 @Ananya221203