[Python] Why is it not detecting and reading the face from my captured image?

Discussão em 'Python' iniciado por Stack, Agosto 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm currently working on a face-comparison system that takes one loaded image and one captured image, extracts the faces from both, and compares them to see if they are the same person or not. I'm using OpenCV for it, and it's been successful at detecting and returning the face from the preloaded image. However, when I supplant the same code into an existing image capturing function, it fails to do so and says that there is no image to be compared.

    For the face detection on the preloaded image I have

    def get_Face(image_path):
    img = cv2.imread(image_path)
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
    color = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    faces = face_cascade.detectMultiScale(color, scaleFactor=1.1, minNeighbors=5)
    if len(faces) > 0:
    for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)

    cv2.imshow("Detected Face", img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    x, y, w, h = faces[-1]
    face_img = img[y:y+h, x:x+w]
    return face_img
    else:
    print("No face detected.")


    and for the face detection on the live capture image I have

    def get_Picture():
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
    cam = cv2.VideoCapture(0)
    ret, frame = cam.read()
    if ret:
    cv2.imshow("Captured", frame)
    cv2.waitKey(0)
    cv2.destroyWindow("Captured")
    cam.release()
    color = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    faces = face_cascade.detectMultiScale(color, scaleFactor=1.1, minNeighbors=5)
    if len(faces) > 0:
    for (x, y, w, h) in faces:
    cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)

    cv2.imshow("Detected Face", frame)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    x, y, w, h = faces[-1]
    face_img = frame[y:y+h, x:x+w]
    return face_img
    else:
    print("Failed to capture image")
    cam.release()


    The comparison function is as follows

    def compare_Images(image_path):
    id_photo = get_Face(image_path)
    snapped_photo = get_Picture()

    id_photo_gray = cv2.cvtColor(id_photo, cv2.COLOR_RGB2GRAY)
    snapped_photo_gray = cv2.cvtColor(snapped_photo, cv2.COLOR_RGB2GRAY)

    score, diff = structural_similarity(id_photo_gray, snapped_photo_gray, full=True)
    print("Similarity Score: {:.3f}%".format(score * 100))

    diff = (diff * 255).astype("uint8")

    thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
    contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    contours = contours[0] if len(contours) == 2 else contours[1]

    mask = np.zeros(id_photo.shape, dtype='uint8')
    filled = snapped_photo.copy()

    for c in contours:
    area = cv2.contourArea(c)
    if area > 100:
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(id_photo, (x, y), (x + w, y + h), (36,255,12), 2)
    cv2.rectangle(snapped_photo, (x, y), (x + w, y + h), (36,255,12), 2)
    cv2.drawContours(mask, [c], 0, (0,255,0), -1)
    cv2.drawContours(filled, [c], 0, (0,255,0), -1)

    cv2.imshow('id_photo', id_photo)
    cv2.imshow('snapped_photo', snapped_photo)
    cv2.imshow('diff', diff)
    cv2.imshow('mask', mask)
    cv2.imshow('filled', filled)
    cv2.waitKey()


    and the error I'm getting is

    error: OpenCV(4.11.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:199: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

    I've tried fiddling with the BGR2GRAY in the original code by making is RGB2GRAY since the images are now in RGB color format but it's saying that there isn't any value in the snapped_photo_gray variable

    Continue reading...

Compartilhe esta Página