|
| 1 | + |
| 2 | +import time |
| 3 | +from datetime import datetime |
| 4 | + |
| 5 | +import cv2 |
| 6 | +import pandas |
| 7 | + |
| 8 | +video=cv2.VideoCapture(0) |
| 9 | +first_frame=None |
| 10 | +status_list=[None,None] |
| 11 | +time=[] |
| 12 | +df=pandas.DataFrame(columns=["Start","End"]) |
| 13 | +cnts= [[0,0], [255,0], [255,255], [0,255]] |
| 14 | +while True: |
| 15 | + check, frame = video.read() |
| 16 | + status=0 |
| 17 | + gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) |
| 18 | + gray=cv2.GaussianBlur(gray,(21,21),0) |
| 19 | + if first_frame is None: |
| 20 | + first_frame=gray |
| 21 | + continue |
| 22 | + delta_frame=cv2.absdiff(first_frame,gray) |
| 23 | + thresh_data=cv2.threshold(delta_frame,30,255,cv2.THRESH_BINARY)[1] |
| 24 | + thresh_delta=cv2.dilate(thresh_data,None,iterations=5) |
| 25 | + (cnts,_) =cv2.findContours(thresh_data.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) |
| 26 | + for contour in cnts: |
| 27 | + if cv2.contourArea(contour)<10000: |
| 28 | + continue |
| 29 | + status=1 |
| 30 | + (x,y,w,h)=cv2.boundingRect(contour) |
| 31 | + cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),3) |
| 32 | + status_list.append(status) |
| 33 | + if status_list[-1]==1 and status_list[-2]==0: |
| 34 | + time.append(datetime.now()) |
| 35 | + if status_list[-1]==0 and status_list[-2]==1: |
| 36 | + time.append(datetime.now()) |
| 37 | + cv2.imshow("Capturing", gray) |
| 38 | + cv2.imshow("Delta Frame",delta_frame) |
| 39 | + cv2.imshow("Threshold Frame",thresh_data) |
| 40 | + cv2.imshow("Colour Frame",frame) |
| 41 | + |
| 42 | + |
| 43 | + key=cv2.waitKey(1) |
| 44 | + if key==ord('q'): |
| 45 | + if status==1: |
| 46 | + time.append(datetime.now()) |
| 47 | + break |
| 48 | + |
| 49 | + |
| 50 | +print(time) |
| 51 | +for i in range(0,len(time),2): |
| 52 | + df=df.append({"Start":time[i],"End":time[i+1]},ignore_index=True) |
| 53 | +df.to_csv("Times.csv") |
| 54 | + |
| 55 | +video.release() |
| 56 | +cv2.destroyAllWindows |
| 57 | + |
0 commit comments