-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslides.py
75 lines (64 loc) · 2.11 KB
/
slides.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
import numpy as np
import progressbar
import cv2
import time
import argparse
import sys
import os
#using argprase to do saving,giving animal and habitat model and to give arena pic
parser = argparse.ArgumentParser( usage="""python myscript.py {vid}""",description='''Description.''',epilog="""Epilog.""")
parser.add_argument("vid", help="input path")
parser.add_argument('-f',help="folder path for saving")
parser.add_argument('-s',help="no of frames to be skipped")
parser.add_argument('-p',help="fintune the threshold percentage(0-1 float values)")
args = parser.parse_args()
if(args.vid==None or args.f==None):
print("ERROR:no input/output path specified")
exit()
input_location=args.vid
output_location=args.f
perc=0.15
ch=5
if(args.s!=None):
ch=int(args.skip)
if(args.p!=None):
perc=float(args.skip)
current_directory = os.getcwd()
final_directory = os.path.join(current_directory,output_location)
if not os.path.exists(final_directory):
os.makedirs(final_directory)
cap = cv2.VideoCapture(input_location)
if cap.isOpened()==False:
print("ERROR:Failed to open video file")
exit()
os.chdir(final_directory)
count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
n=cap.get(cv2.CAP_PROP_FRAME_WIDTH)*cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
old_frame = None
f=0
i=0
sl_count=1
bar = progressbar.ProgressBar(maxval=count, \
widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
bar.start()
while i<count:
ret, frame = cap.read()
f=f+1
if ret == True and f==ch:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if old_frame is not None:
diff_frame = cv2.absdiff(frame, old_frame)
diff_per=float(cv2.countNonZero(cv2.cvtColor(diff_frame, cv2.COLOR_BGR2GRAY))/n)
frame_blank=float(cv2.countNonZero(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY))/n)
if diff_per>perc and frame_blank>0.40:
name=('slide' + "% d"+'.jpg') % sl_count
cv2.imwrite(name,frame)
sl_count=sl_count+1
old_frame = frame
else:
old_frame = frame
f=0
bar.update(i+1)
i=i+1
bar.finish()
cap.release()