Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 this issue Jul 30, 2024 · 1 comment

Comments

@Ananya221203
Copy link

  • face_recognition version:
  • Python version:
  • Operating System:

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants