[Python] Haar Cascade classifier freezing code's execution

Discussão em 'Python' iniciado por Stack, Abril 24, 2025.

  1. Stack

    Stack Membro Participativo

    Trying to use Haar Cascade classifier, the program stops indefinitely using this code:

    import cv2

    haar_file = cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
    face_cascade = cv2.CascadeClassifier(haar_file)

    # Reduzindo resolução
    webcam = cv2.VideoCapture(0)
    webcam.set(cv2.CAP_PROP_FRAME_WIDTH, 320) # width
    webcam.set(cv2.CAP_PROP_FRAME_HEIGHT, 240) # height


    while True:
    retorno, frame = webcam.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    print("Grayscaled")

    faces = face_cascade.detectMultiScale(gray, 1.3, 3, flags= cv2.CASCADE_SCALE_IMAGE)
    print("Faces detected")

    for (x1, y1, x2, y2) in faces:
    moldura_face = gray[y1:y1+y2, x1:x1+x2]

    cv2.rectangle(gray, (x1, y1), (x2, y2), (0, 255, 255), 2)

    moldura_face = cv2.resize(moldura_face, (48, 48))

    # cv2.putText(im,prediction_label)
    cv2.putText(frame, '% s' % ('prediction_label'), (x1-10, y1-10),
    cv2.FONT_HERSHEY_COMPLEX_SMALL, 2, (0, 0, 255))

    cv2.imshow("Output", frame)
    cv2.waitKey(27)


    I tested the webcam with a simple opencv code and it works fine, even with a ball detector code using HoughCircles, and it works fine.

    But when executing the code above, it only gets to:

    [...]/testCascade.py
    4367ea-dec2-446e-99c4-7ea97825c9c3
    Grayscaled


    And never reaches "Faces detected"

    I don't understand why, I tried reducing the code as much as possible but I still can pin point the problem. Can anyone?

    Continue reading...

Compartilhe esta Página