Commit ·
ae49a1b
1
Parent(s): 6014d3d
optional end time
Browse files- app.py +14 -6
- hls_download.py +3 -2
app.py
CHANGED
|
@@ -123,7 +123,7 @@ def detect_beeps(video_path, event_length=30):
|
|
| 123 |
|
| 124 |
|
| 125 |
def inference(stream_url, start_time, end_time, beep_detection_on, event_length, count_only_api, api_key,
|
| 126 |
-
img_size=256, seq_len=64, stride_length=32, stride_pad=3, batch_size=4,
|
| 127 |
miss_threshold=0.8, marks_threshold=0.5, median_pred_filter=True, center_crop=True, both_feet=True,
|
| 128 |
api_call=False,
|
| 129 |
progress=gr.Progress()):
|
|
@@ -149,24 +149,32 @@ def inference(stream_url, start_time, end_time, beep_detection_on, event_length,
|
|
| 149 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 150 |
seconds = length / fps
|
| 151 |
all_frames = []
|
| 152 |
-
frame_i =
|
| 153 |
-
resize_size = max(frame_width, frame_height)
|
|
|
|
| 154 |
while cap.isOpened():
|
|
|
|
|
|
|
| 155 |
ret, frame = cap.read()
|
| 156 |
if ret is False:
|
| 157 |
frame = all_frames[-1] # padding will be with last frame
|
| 158 |
break
|
|
|
|
|
|
|
|
|
|
| 159 |
frame = cv2.cvtColor(np.uint8(frame), cv2.COLOR_BGR2RGB)
|
| 160 |
# add square padding with opencv
|
| 161 |
#frame = square_pad_opencv(frame)
|
| 162 |
-
|
|
|
|
|
|
|
| 163 |
frame_center_x = frame.shape[1] // 2
|
| 164 |
frame_center_y = frame.shape[0] // 2
|
| 165 |
crop_x = frame_center_x - img_size // 2
|
| 166 |
crop_y = frame_center_y - img_size // 2
|
| 167 |
frame = frame[crop_y:crop_y+img_size, crop_x:crop_x+img_size]
|
| 168 |
all_frames.append(frame)
|
| 169 |
-
|
| 170 |
cap.release()
|
| 171 |
|
| 172 |
length = len(all_frames)
|
|
@@ -430,7 +438,7 @@ with gr.Blocks() as demo:
|
|
| 430 |
api_dummy_button.click(api_inference, [in_stream_url, in_stream_start, in_stream_end, beep_detection_on, event_length, count_only, api_token],
|
| 431 |
outputs=[period_length], api_name='inference')
|
| 432 |
examples = [
|
| 433 |
-
['https://hiemdall-dev2.azurewebsites.net/api/
|
| 434 |
['https://hiemdall-dev2.azurewebsites.net/api/playlist/rec_UGEhqlMh/vod', '00:00:18', '00:00:55', True, 30],
|
| 435 |
#['https://hiemdall-dev2.azurewebsites.net/api/playlist/rec_rd2FAyUo/vod', '01:24:22', '01:25:35', True, 60]
|
| 436 |
#['https://hiemdall-dev2.azurewebsites.net/api/playlist/rec_PY5Ukaua/vod, '00:52:53', '00:55:00', True, 120]
|
|
|
|
| 123 |
|
| 124 |
|
| 125 |
def inference(stream_url, start_time, end_time, beep_detection_on, event_length, count_only_api, api_key,
|
| 126 |
+
img_size=256, seq_len=64, stride_length=32, stride_pad=3, batch_size=4, resize_factor=0.5, min_size=300, force_30_fps=True,
|
| 127 |
miss_threshold=0.8, marks_threshold=0.5, median_pred_filter=True, center_crop=True, both_feet=True,
|
| 128 |
api_call=False,
|
| 129 |
progress=gr.Progress()):
|
|
|
|
| 149 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 150 |
seconds = length / fps
|
| 151 |
all_frames = []
|
| 152 |
+
frame_i = 0
|
| 153 |
+
#resize_size = int(max(frame_width, frame_height) * 0.4)
|
| 154 |
+
resize_amount = max((img_size + 64) / frame_width, (img_size + 64) / frame_height)
|
| 155 |
while cap.isOpened():
|
| 156 |
+
frame_i += 1
|
| 157 |
+
|
| 158 |
ret, frame = cap.read()
|
| 159 |
if ret is False:
|
| 160 |
frame = all_frames[-1] # padding will be with last frame
|
| 161 |
break
|
| 162 |
+
# if force_30_fps and fps != 30 and frame_i % 4 != 0:
|
| 163 |
+
# continue
|
| 164 |
+
|
| 165 |
frame = cv2.cvtColor(np.uint8(frame), cv2.COLOR_BGR2RGB)
|
| 166 |
# add square padding with opencv
|
| 167 |
#frame = square_pad_opencv(frame)
|
| 168 |
+
frame_center_x = frame.shape[1] // 2
|
| 169 |
+
frame_center_y = frame.shape[0] // 2
|
| 170 |
+
frame = cv2.resize(frame, (0, 0), fx=resize_amount, fy=resize_amount, interpolation=cv2.INTER_CUBIC)
|
| 171 |
frame_center_x = frame.shape[1] // 2
|
| 172 |
frame_center_y = frame.shape[0] // 2
|
| 173 |
crop_x = frame_center_x - img_size // 2
|
| 174 |
crop_y = frame_center_y - img_size // 2
|
| 175 |
frame = frame[crop_y:crop_y+img_size, crop_x:crop_x+img_size]
|
| 176 |
all_frames.append(frame)
|
| 177 |
+
|
| 178 |
cap.release()
|
| 179 |
|
| 180 |
length = len(all_frames)
|
|
|
|
| 438 |
api_dummy_button.click(api_inference, [in_stream_url, in_stream_start, in_stream_end, beep_detection_on, event_length, count_only, api_token],
|
| 439 |
outputs=[period_length], api_name='inference')
|
| 440 |
examples = [
|
| 441 |
+
['https://hiemdall-dev2.azurewebsites.net/api/clip/clp_vrpWTyjM/mp4', '00:00:00', '00:01:10', True, 60],
|
| 442 |
['https://hiemdall-dev2.azurewebsites.net/api/playlist/rec_UGEhqlMh/vod', '00:00:18', '00:00:55', True, 30],
|
| 443 |
#['https://hiemdall-dev2.azurewebsites.net/api/playlist/rec_rd2FAyUo/vod', '01:24:22', '01:25:35', True, 60]
|
| 444 |
#['https://hiemdall-dev2.azurewebsites.net/api/playlist/rec_PY5Ukaua/vod, '00:52:53', '00:55:00', True, 120]
|
hls_download.py
CHANGED
|
@@ -4,6 +4,8 @@ import os
|
|
| 4 |
def download_clips(stream_url, out_dir, start_time, end_time, resize=True):
|
| 5 |
output_file = os.path.join(out_dir, f"train_{len(os.listdir(out_dir))}.mp4")
|
| 6 |
tmp_file = os.path.join(out_dir, f"train_{len(os.listdir(out_dir))}_tmp.mp4")
|
|
|
|
|
|
|
| 7 |
yt_dlp_cmd = [
|
| 8 |
'yt-dlp',
|
| 9 |
'--download-sections', f'*{start_time}-{end_time}',
|
|
@@ -25,7 +27,6 @@ def download_clips(stream_url, out_dir, start_time, end_time, resize=True):
|
|
| 25 |
'ffmpeg',
|
| 26 |
'-i', tmp_file,
|
| 27 |
'-c:v', 'libx264',
|
| 28 |
-
'-preset', 'veryfast',
|
| 29 |
'-crf', '23',
|
| 30 |
'-r', '30',
|
| 31 |
'-maxrate', '2M',
|
|
@@ -47,4 +48,4 @@ def download_clips(stream_url, out_dir, start_time, end_time, resize=True):
|
|
| 47 |
os.rename(tmp_file, output_file)
|
| 48 |
print(f"Converted {output_file}")
|
| 49 |
|
| 50 |
-
return output_file
|
|
|
|
| 4 |
def download_clips(stream_url, out_dir, start_time, end_time, resize=True):
|
| 5 |
output_file = os.path.join(out_dir, f"train_{len(os.listdir(out_dir))}.mp4")
|
| 6 |
tmp_file = os.path.join(out_dir, f"train_{len(os.listdir(out_dir))}_tmp.mp4")
|
| 7 |
+
if end_time is None or end_time == '':
|
| 8 |
+
end_time = 'inf'
|
| 9 |
yt_dlp_cmd = [
|
| 10 |
'yt-dlp',
|
| 11 |
'--download-sections', f'*{start_time}-{end_time}',
|
|
|
|
| 27 |
'ffmpeg',
|
| 28 |
'-i', tmp_file,
|
| 29 |
'-c:v', 'libx264',
|
|
|
|
| 30 |
'-crf', '23',
|
| 31 |
'-r', '30',
|
| 32 |
'-maxrate', '2M',
|
|
|
|
| 48 |
os.rename(tmp_file, output_file)
|
| 49 |
print(f"Converted {output_file}")
|
| 50 |
|
| 51 |
+
return output_file
|