|
| 1 | +import cv2 as cv |
| 2 | +from datetime import datetime |
| 3 | +import pandas |
| 4 | +first_frame=None #stores the first frame... |
| 5 | +status_list=[None,None] #stroing the status if objects |
| 6 | +times=[] #storing the datetime for objects |
| 7 | +df=pandas.DataFrame(columns=["Start","End"]) |
| 8 | +video=cv.VideoCapture(0) |
| 9 | +while True: |
| 10 | + #two attributes check->:boolean that tells video is recording..while frame :-> ndarray |
| 11 | + check,frame=video.read() |
| 12 | + #this status=0 means there is no motion right now.. |
| 13 | + status=0 |
| 14 | + #print(check) |
| 15 | + #print(frame) |
| 16 | + #converting RGB video into GrayScale |
| 17 | + gray=cv.cvtColor(frame,cv.COLOR_BGR2GRAY) |
| 18 | + #blurring the video so that motion is easily measured. |
| 19 | + gray=cv.GaussianBlur(gray,(21,21),0) |
| 20 | + #checking and storing the backgound,so the background finds out the difference with other frames of video. |
| 21 | + if first_frame is None: |
| 22 | + first_frame=gray |
| 23 | + continue #now if we continue then there will be no first frame,because it is pre generated. |
| 24 | + |
| 25 | + delta_frame=cv.absdiff(first_frame,gray) #this finding the diff between the first frame(background) and other frames of video. |
| 26 | + thresh_frame=cv.threshold(delta_frame,30,255,cv.THRESH_BINARY)[1] #create threshold frame |
| 27 | + thresh_frame=cv.dilate(thresh_frame,None,iterations=2) #dilate means making tthe edges smoother |
| 28 | + |
| 29 | + #finding the contours which helps to find out objects in a frame. |
| 30 | + (_,cnts,_)=cv.findContours(thresh_frame.copy(),cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE) |
| 31 | + for contour in cnts: |
| 32 | + #if area of object is less then 1000 then we will continue otherwise there is an object. |
| 33 | + if cv.contourArea(contour)<1000: |
| 34 | + continue |
| 35 | + status=1 #this implies there is an object in a frame |
| 36 | + (x,y,w,h)=cv.boundingRect(contour) # finding the corners of object in a frame. |
| 37 | + cv.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),3) # making out rectangle for every objects |
| 38 | + #appending all the incoming and outgoing of the object in an frame |
| 39 | + status_list.append(status) |
| 40 | + |
| 41 | + #checking the datetime of apearing object |
| 42 | + if status_list[-1]==1 and status_list[-2]==0: |
| 43 | + times.append(datetime.now()) |
| 44 | + #checking the datetime of disapearing object |
| 45 | + if status_list[-1]==0 and status_list[-2]==1: |
| 46 | + times.append(datetime.now()) |
| 47 | + |
| 48 | + #showing the frames/windows |
| 49 | + cv.imshow("Gray Frame",gray) |
| 50 | + cv.imshow("Delta Frame",delta_frame) |
| 51 | + cv.imshow("THRESH_BINARY",thresh_frame) |
| 52 | + cv.imshow("RectFrame",frame) |
| 53 | + #1 is refreshing the frame in every 1 milisec: |
| 54 | + key=cv.waitKey(1) |
| 55 | + #displaying window until you press q |
| 56 | + if key ==ord('q'): |
| 57 | + #if thier is appearing object but that time is window is quited,then it will add the datetime for disappearing object as time of window exited. |
| 58 | + if status==1: |
| 59 | + times.append(datetime.now()) |
| 60 | + break |
| 61 | +print(status_list) |
| 62 | +print(times) |
| 63 | +#putting all the datetime of objects in csv file with the help of pandas. |
| 64 | +for i in range(0,len(times),2): |
| 65 | + df=df.append({"Start":times[i],"END":times[i+1]},ignore_index=True) |
| 66 | +df.to_csv("Times.csv") #saving file as "Times.csv" |
| 67 | + |
| 68 | +#releasing window.. |
| 69 | +video.release() |
| 70 | +#destroying all windows.. |
| 71 | +cv.destroyAllWindows() |
0 commit comments