简体   繁体   中英

OpenCV with Python wont exit properly when using detectMultiScale on CascadeClassifier

I am experimenting with OpenCV and facial recognition. It has been working without any issuess until lately when it stopped exiting my script and I am forced to close the commandprompt. After some debugging I have located the culprit to be this line. The program exits properly if I remove this line but then I dont have the option to detect faces:(

faces = FACE_CLASSIFIER.detectMultiScale(gray, scaleFactor=SCALE_FACTOR, minNeighbors=MIN_NEIGHBORS)

I have also experimented using this code before and after the destroy windows command but it had no effect

for i in range(1, 5):
    cv2.waitKey(1)

I found this information in the opencv documentation

When the cascade is not needed anymore, release it using cvReleaseHaarClassifierCascade(&cascade).

https://docs.opencv.org/2.4/modules/objdetect/doc/cascade_classification.html

I think this might be the proper solution but I haven't been able to find a method that would allow me to do this.

Sampe code

I start with from commandline using

python face.py

face.py

import cv2

FACE_CLASSIFIER = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
SCALE_FACTOR = 1.5
MIN_NEIGHBORS = 5

video_capture = cv2.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # If this line is removed the program exits properly 
    faces = FACE_CLASSIFIER.detectMultiScale(gray, scaleFactor=SCALE_FACTOR, minNeighbors=MIN_NEIGHBORS)

    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.release()
cv2.destroyAllWindows()

System information

Microsoft Windows [Version 10.0.18363.836]

pip3 list
Package               Version
--------------------- --------
numpy                 1.18.5
opencv-contrib-python 3.4.9.33
pip                   20.1.1
setuptools            41.2.0

python --version
Python 3.7.7

It appears the problem is related to opencv-contrib-python package. I have tried the following packages and they all work.

pip3 install opencv-contrib-python==3.4.2.16
pip3 install opencv-contrib-python==3.4.2.17
pip3 install opencv-contrib-python==3.4.3.18

These versions all introduce this warning but it still works.

[ WARN:1] terminating async callback

pip3 install opencv-contrib-python==3.4.4.19
pip3 install opencv-contrib-python==3.4.5.20
pip3 install opencv-contrib-python==3.4.6.27
pip3 install opencv-contrib-python==3.4.7.28
pip3 install opencv-contrib-python==3.4.8.29
pip3 install opencv-contrib-python==4.0.0.21
pip3 install opencv-contrib-python==4.0.1.23
pip3 install opencv-contrib-python==4.0.1.24
pip3 install opencv-contrib-python==4.1.0.25

These version all introduce this warning but it still works.

[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (674) SourceReaderCB::~SourceReaderCB terminating async callback

pip3 install opencv-contrib-python==4.1.1.26
pip3 install opencv-contrib-python==4.1.2.30

And these versions fail to run the application with clean exit.

pip3 install opencv-contrib-python==3.4.9.31
pip3 install opencv-contrib-python==3.4.9.33
pip3 install opencv-contrib-python==4.2.0.32

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM