The python script doesn't stop

Viewed 144

I am using opencv and numpy for human body detection using HOGDescriptor. Here is the code, I am playing with:

import numpy as np
import cv2
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
cv2.startWindowThread()
cap = cv2.VideoCapture('C:/Users/****/Desktop/people_walk.mkv')
while(True):
    ret, frame = cap.read()
    frame = cv2.resize(frame, (640, 480))
    gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
    boxes, weights = hog.detectMultiScale(frame, winStride=(8,8) )
    boxes = np.array([[x, y, x + w, y + h] for (x, y, w, h) in boxes])
    for (xA, yA, xB, yB) in boxes:
        cv2.rectangle(frame, (xA, yA), (xB, yB),
                          (0, 255, 0), 2)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
cv2.waitKey(1)

So after pressing q the camera window should closed. In fact it does, but the script keeps on running and the whole execution doesn't stop even after pressing ctrl+c or exit(). Any help with this?

2 Answers

It's coming from the while exit condition.

Try this: Prioritize the bitwise operation and put 33 instead of 1 on the WaitingKey.

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

Seems like the problem was with the line

hog = cv2.HOGDescriptor()   
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

had to add the line in the waitkey() if condition

  hog.setSVMDetector(None)
Related