-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnth_main.py
85 lines (63 loc) · 2.29 KB
/
nth_main.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
82
83
84
85
import argparse
import cv2
from operator import itemgetter
import pandas as pd
import os
import time
from utilities import *
# collect system arguments
parser = argparse.ArgumentParser()
parser.add_argument("--directory", "-d", default="videos", help="directory of the videos")
parser.add_argument("--extension", "-ex", help="Video file extension")
parser.add_argument("--nth", help="Interval between frames", type=int, default=25)
args = parser.parse_args()
directory = args.directory
video_extension = args.extension
nth_interval = int(args.nth)
files_directory, videos_names = find_videos(directory, video_extension)
def select_frames (vid, vid_name, nth_interval):
print(f"Saving frames from {vid_name}")
start = time.time()
# read the video
vidcap = cv2.VideoCapture(vid)
#create folder for the selected frames
if not os.path.exists(f"{vid}_bl{nth_interval}"):
os.mkdir(f"{vid}_bl{nth_interval}")
# get the video dimensions
if vidcap.isOpened():
frame_count = vidcap.get(7)
print (f"intial frame count: {int(frame_count)}")
# initiate counter
count = 0
selected_frames_count = 0
# parse the video
while True:
# read the video
success, image = vidcap.read()
if vidcap.get(1) == 0:
cv2.imwrite(f"{vid}_bl{nth_interval}/{str(vidcap.get(1))}{vid_name}_bl{nth_interval}.tif",image)
# use the next frames based on the condition below
if vidcap.get(1) % nth_interval == 0:
cv2.imwrite(f"{vid}_bl{nth_interval}/{str(vidcap.get(1))}{vid_name}_bl{nth_interval}.tif",image)
selected_frames_count += 1
# check if video is fully read
if vidcap.get(1) >= frame_count:
break
vidcap.release()
end = time.time() - start
print ('Processing time: ', end)
print ('Final frame count: ', selected_frames_count)
return (end, selected_frames_count)
time_list = []
final_frame_count_list = []
for count in range(len(files_directory)):
process_time, frame_count = select_frames (files_directory[count], videos_names[count], nth_interval)
time_list.append(process_time)
final_frame_count_list.append(frame_count)
df = pd.DataFrame({
"video name" : videos_names,
"processing_time" : time_list,
"final_frame_count" : final_frame_count_list,
})
df.to_csv(f"{directory}/baseline_{nth_interval}.csv")
print ("Done!")