Опитвам се да създам детектор на движение с помощта на openCV и python, но прозорецът на дисплея не реагира, когато прекратя програмата

Ето кода за същото, вижте го. В този код по-долу създавам детектор на движение и с това ще записвам времето на появата и изчезването на различните обекти, за които използвам рамка с данни.

Проблемът с това е, че програмата се изпълнява, но когато изходът се показва под формата на прозорец, той замръзва, когато се опитам да прекъсна.

import cv2, pandas
from datetime import datetime

first_frame = None
status_list = [None,None]
times = []
df = pandas.DataFrame(columns=["Start", "End"]) #Dataframe to store the time values during which object detection and movement appears.


video = cv2.VideoCapture(0)#VideoCapture object is used to record video using web cam

while True:
    #check is a bool data type, returns true if VideoCapture object is read
    check,frame = video.read()
    status = 0
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)  # For converting the frame color to gray scale
    gray = cv2.GaussianBlur(gray,(21,21),0)  # For converting the gray scale frame to GaussianBlur

    if first_frame is None:
        first_frame = gray   # used to store the first image/frame of the video
        continue
    delta_frame = cv2.absdiff(first_frame,gray)#calculates the difference between first and other frames
    thresh_delta = cv2.threshold(delta_frame,30,255,cv2.THRESH_BINARY)[1]
    thresh_delta = cv2.dilate(thresh_delta,None,iterations=0) #Provides threshold value, so if the difference is <30 it will turn to black otherwise if >30 pixels will turn to white
    (_,cnts,_) = cv2.findContours(thresh_delta.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) #Define the contour area,i.e. adding borders

    #Removing noises and shadows, any part which is greater than 1000 pixels will be converted to white
    for contour in cnts:
        if cv2.contourArea(contour) < 1000:
            continue
        status = 1 #change in status when the object is detected
        #Creating a rectangular box around the object in frame
        (x,y,w,h) = cv2.boundingRect(contour)
        cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),3)

    #list of status for every frame
    status_list.append(status)

    status_list=status_list[-2:]

    #Record datetime in a list when change occurs
    if status_list[-1]==1 and status_list[-2]==0:
        times.append(datetime.now())
    if status_list[-1]==0 and status_list[-2]==1:
        times.append(datetime.now())

    cv2.imshow('frame',frame)
    #cv2.imshow('Capturing',gray)
    #cv2.imshow('delta',delta_frame)
    #cv2.imshow('thresh',thresh_delta)

    key = cv2.waitKey(1) #changing the frame after 1 millisecond

    #Used for terminating the loop once 'q' is pressed
    if key == ord('q'):
        break

print(status_list)
print(times)

for i in range(0,len(times),2):
    df = df.append({"Start":times[i],"End":times[i+1]},ignore_index=True)

df.to_csv("Times.csv")

video.release()
cv2.destroyAllWindows #will be closing all the windows

person Palash Sharma    schedule 10.11.2018    source източник
comment
добре дошли в SO. използвате ли mac, windows, linux? cv2 версия? може да е полезно.   -  person user1269942    schedule 10.11.2018
comment
Използвам windows и 3.4.1 версия на cv2   -  person Palash Sharma    schedule 10.11.2018


Отговори (2)


Опитайте тази:

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

За кратко обяснение какво е "& 0xFF", вижте: За какво е 0xFF в cv2. waitKey(1)?

По същество това е битова маска, която приема частта от стойността на 'waitKey' (32 бита), която може да се сравни с ASCII (8 бита).

person user1269942    schedule 10.11.2018
comment
Опитах този кодов фрагмент, но не проработи. Сега не мога да прекратя програмата с натискане на 'q'. - person Palash Sharma; 10.11.2018

Можете да опитате това.

k = cv2.waitKey(1) & 0xFF
if k == 27 :        
     break

където k може да бъде всеки ASCII код на клавиатурата Помощ за ASCII код

В този случай 27 е бутон „Esc“.

Проблемът, който имате обаче, е, че замръзва, опитвали ли сте да натискате непрекъснато някой бутон освен q? Имах подобен проблем, когато тествах кода на урока на OpenCV тук

но го разбрах, като си поиграх и след това осъзнах, че трябва да задържа който и да е бутон освен бутона за изход.

person penguin    schedule 17.02.2019