-
Notifications
You must be signed in to change notification settings - Fork 2
/
target3-p2.py
81 lines (60 loc) · 1.99 KB
/
target3-p2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import cv2 as cv
import numpy as np
import time
def rescale(frame,scale=0.75):
width=int(frame.shape[1]*scale)
height=int(frame.shape[0]*scale)
dimension=(width,height)
return cv.resize(frame,dimension,interpolation=cv.INTER_AREA)
def finderr(dif):
err=np.sum(dif**2)
mse=err/(float)(dif.shape[1] * dif.shape[0])
return mse
def main():
vidfilename="TestVid1.mp4"
count =0
filename="IMG"
filecount=1
path="extract"
scale=float(input("Enter the rescale multiplier\n"))
frameskip=(int)(input("Enter how many frames to skip after each pick\n"))
frameskip+=1
capture = cv.VideoCapture(vidfilename)
error=0
length=int(capture.get(cv.CAP_PROP_FRAME_COUNT))
print(length)
speed=2
speed=(float)(input("Enter speed multiplier for live feed\n"))
timegap=(int)(20.0/speed)
fps = capture.get(cv.CAP_PROP_FPS)
while(True):
isTrue, frame=capture.read()
count+=1
if(isTrue==False):
break
if(count%frameskip==0):
frame=rescale(frame,scale)
if(count==frameskip):
prevframe=frame
filename=path+"\\IMG"+str(filecount)+".png"
filename1=path+"\\DIF"+str(filecount)+".png"
filecount+=1
cv.imshow('Current video',frame)
#cv.imshow('previous video',prevframe)
prevgray=cv.cvtColor(prevframe,cv.COLOR_BGR2GRAY)
gray=cv.cvtColor(frame,cv.COLOR_BGR2GRAY)
dif=cv.subtract(prevgray,gray)
error=finderr(dif)
if(error>3):
print("\n",error)
cv.imwrite(filename1,dif)
cv.imwrite(filename,frame)
cv.imshow('difference video',dif)
prevframe=frame
if( cv.waitKey(timegap) & 0xFF==ord('d')):
break;
#cv.imwrite(filename,frame)
capture.release()
cv.destroyAllWindows()
cv.waitKey(0)
main()