-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalltogether.py
221 lines (196 loc) · 8.65 KB
/
alltogether.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from retinaface import RetinaFace
import cv2
from scenedetect.scene_manager import SceneManager
from scenedetect.video_manager import VideoManager
from scenedetect.stats_manager import StatsManager
from scenedetect.detectors.content_detector import ContentDetector
from os import listdir, remove, path
import os
from os.path import isfile, join
import shutil
#defien importatnt parameters
DETECTION_THRESHOLD = 0.6 # you can change this
VIDEO_DIR = 'trimmed/'
VIDEO_OUT_DIR = 'output/'
#retina face initializataion
model = RetinaFace('model-mnet/mnet.25', 0, 0, 'net3')
#get files in the list
onlyfiles = [f for f in listdir(VIDEO_DIR) if isfile(join(VIDEO_DIR, f))]
the_current_vid = 0
number_of_output_videos=0
while len(onlyfiles) > the_current_vid :
VIDEO_PATH = onlyfiles[the_current_vid]
cap = cv2.VideoCapture(VIDEO_PATH)
#get video details
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # float -> int
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # float -> int
frame_number=0
out = cv2.VideoWriter( 'output/output'+str(number_of_output_videos)+'.avi',
cv2.VideoWriter_fourcc('M','J','P','G'), int(fps),
(width,height) )
while cap.isOpened():
# flag=0
ret, frame = cap.read()
frame_number+=1
if ret == True:
if frame_number%(fps/2) == 1 : # this is the step that we check the faces in it
detections,point = model.detect(frame)
# dinish_of_short_interval=i
if len(detections) == 1 :
if detections[0][4]>DETECTION_THRESHOLD:
out.write(frame)
# flag=1
else:#if flag == 1 :
number_of_output_videos+=1
out.release()
out.release()
out = cv2.VideoWriter( 'output/output'+str(number_of_output_videos)+'.avi',
cv2.VideoWriter_fourcc('M','J','P','G'), int(fps),
(width,height) )
# flag=0
# start_of_short_interval=i
pass
else:
break
out.release()
the_current_vid += 1
cap.release()
print('the number of vids = ' + str(the_current_vid))
######################################### scene detector ###############################################
print("######### phase scene detector #########")
DETECTION_THRESHOLD = 0.7
f=0
onlyfiles=[]
onlyfiles = [f for f in listdir(VIDEO_OUT_DIR) if isfile(join(VIDEO_OUT_DIR, f))]
# this will remove unnecesery files (video files that generated that their size is under 15 Kb)
for i in range(len(onlyfiles)):
file = VIDEO_OUT_DIR + ''.join(onlyfiles[i:i+1])
if os.path.getsize(file) < 15 * 1024:
os.remove(file)
onlyfiles=[]
onlyfiles = [f for f in listdir(VIDEO_OUT_DIR) if isfile(join(VIDEO_OUT_DIR, f))]
the_current_vid = 0
while len(onlyfiles) > the_current_vid:
address = VIDEO_OUT_DIR + ''.join(onlyfiles[the_current_vid : the_current_vid+1])
print(address)
video_manager = VideoManager([address])
stats_manager = StatsManager()
scene_manager = SceneManager(stats_manager)
scene_manager.add_detector(ContentDetector())
base_timecode = video_manager.get_base_timecode()
video_manager.start()
# Perform scene detection on video_manager.
scene_manager.detect_scenes(video_manager)
# Obtain list of detected scenes.
scene_list = scene_manager.get_scene_list(base_timecode)
# Each scene is a tuple of (start, end) FrameTimecodes.
print('List of scenes obtained:')
sc_list = []
scene_number_in_vid=0
for i, scene in enumerate(scene_list):
sc_list.append(scene[0].get_frames())
sc_list.append(scene[1].get_frames())
scene_number_in_vid=i+1
print(
'Scene %2d: Start Frame %d, End Frame %d' % (
i+1,
scene[0].get_frames(), scene[1].get_frames(),))
sc_list.remove(0)
###########################################################
print("######### phase scene splitting #########")
print(sc_list)
splitted_vid=0
if scene_number_in_vid > 1 :
while scene_number_in_vid > splitted_vid:
# VIDEO_PATH = VIDEO_OUT_DIR + ''.join(onlyfiles[the_current_vid : the_current_vid+1])
cap = cv2.VideoCapture(address)
out = cv2.VideoWriter('scenesplitted/scene_splitted_'+str(the_current_vid)+'_'+str(splitted_vid)+'.avi',
cv2.VideoWriter_fourcc('M','J','P','G'),
fps, ((width),(height)) )
this_frame=0
while cap.isOpened():
retr, framec = cap.read()
this_frame += 1
if retr == True:
out.write(framec)
if this_frame in sc_list:
splitted_vid += 1
out.release()
out = cv2.VideoWriter(
'scenesplitted/scene_splitted_'+str(the_current_vid)+'_'+str(splitted_vid)+'.avi',
cv2.VideoWriter_fourcc('M','J','P','G'),
fps,
(width,height)
)
else:
# print(i)
break
cap.release()
out.release()
splitted_vid += 1
else :
VIDEO_PATH = VIDEO_OUT_DIR + ''.join(onlyfiles[the_current_vid : the_current_vid+1])
shutil.copy(VIDEO_PATH , "scenesplitted/")
the_current_vid += 1
video_manager.release()
######################### a python list 2 txt file and reverse script ################################
# TODO writing list of scenes into txt files
# with open('listfile.txt', 'w') as filehandle:
# for listitem in sc_list:
# filehandle.write('%s\n' % listitem)
# # open file and read the content in a list
# with open('listfile.txt', 'r') as filehandle:
# for line in filehandle:
# # remove linebreak which is the last character of the string
# currentPlace = line[:-1]
# # add item to the list
# sc_list.append(currentPlace)
####################################### scene splitting ############################################
# print("######### phase scene splitting #########")
# the_current_vid=0
# while len(onlyfiles) > the_current_vid:
# VIDEO_PATH = onlyfiles[the_current_vid]
# cap = cv2.VideoCapture(VIDEO_PATH)
# out = cv2.VideoWriter('scenesplitted/scene_splitted_'+str(the_current_vid)+'.avi',
# cv2.VideoWriter_fourcc('M','J','P','G'),
# fps, ((width),(height)) )
# i=0
# while cap.isOpened():
# ret, frame = cap.read()
# if ret == True:
# out.write(frame)
# if i in sc_list:
# the_current_vid += 1
# out.release()
# out = cv2.VideoWriter('scenesplitted/scene_splitted_'+str(the_current_vid)+'.avi',
# cv2.VideoWriter_fourcc('M','J','P','G'),
# fps, ((width),(height)) )
# i+=1
# else:
# # print(i)
# break
# cap.release()
# out.release()
############## a splitting video script ########################
# import cv2
# if __name__ == '__main__':
# vidPath = '/path/foo/video.mp4'
# shotsPath = '/path/foo/video/%d.avi' # output path (must be avi, otherwize choose other codecs)
# segRange = [(0,40),(50,100),(200,400)] # a list of starting/ending frame indices pairs
# cap = cv2.VideoCapture(vidPath)
# fps = int(cap.get(cv2.CAP_PROP_FPS))
# size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# fourcc = int(cv2.VideoWriter_fourcc('M','J','P','G')) # XVID codecs
# for idx,(begFidx,endFidx) in enumerate(segRange):
# writer = cv2.VideoWriter(shotsPath%idx,fourcc,fps,size)
# cap.set(cv2.CAP_PROP_POS_FRAMES,begFidx)
# ret = True # has frame returned
# while(cap.isOpened() and ret and writer.isOpened()):
# ret, frame = cap.read()
# frame_number = cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
# if frame_number < endFidx:
# writer.write(frame)
# else:
# break
# writer.release()