Spaces:
Runtime error
Runtime error
File size: 7,432 Bytes
7576d10 d2aa7b7 f7db529 7576d10 ff9df39 cc37e2b ff9df39 cd0d6f2 eddda5a 7576d10 b73d81d cd0d6f2 5bbee66 6e8c2ef 4d701c0 eddda5a e213266 7576d10 8353801 cd0d6f2 8353801 02cdb95 8353801 5636b5c 8353801 cd0d6f2 5636b5c 8353801 f3a075d 02cdb95 f3a075d cd0d6f2 f3a075d 5636b5c 7576d10 f3a075d 7576d10 f3a075d 7576d10 f3a075d cd0d6f2 f3a075d cd0d6f2 7576d10 f3a075d 7576d10 5636b5c f3a075d 5636b5c f3a075d 5636b5c f3a075d 5636b5c 7576d10 f3a075d 5636b5c f3a075d 7576d10 f3a075d 7576d10 5636b5c 7576d10 cd0d6f2 02cdb95 7576d10 f3a075d 780307f f3a075d 7576d10 f3a075d cd0d6f2 02cdb95 cd0d6f2 02cdb95 7576d10 5636b5c cd0d6f2 02cdb95 7576d10 cd0d6f2 |
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 222 223 224 225 226 227 228 229 |
import gradio as gr
import os
import subprocess
from huggingface_hub import snapshot_download
REPO_ID='SharkSpace/videos_examples'
snapshot_download(repo_id=REPO_ID, token= os.environ.get('SHARK_MODEL'),repo_type='dataset',local_dir='videos_example')
if os.getenv('SYSTEM') == 'spaces':
subprocess.call('pip install -U openmim'.split())
subprocess.call('pip install python-dotenv'.split())
subprocess.call('pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu113'.split())
subprocess.call('mim install mmcv>=2.0.0'.split())
subprocess.call('mim install mmengine'.split())
subprocess.call('mim install mmdet'.split())
subprocess.call('pip install opencv-python-headless==4.5.5.64'.split())
subprocess.call('pip install git+https://github.com/cocodataset/panopticapi.git'.split())
import cv2
import dotenv
dotenv.load_dotenv()
import numpy as np
import gradio as gr
import glob
from inference import inference_frame
from inference import inference_frame_par_ready
from inference import process_frame
import os
import pathlib
import multiprocessing as mp
from time import time
def analyze_video(x, skip_frames = 5, frame_rate_out = 8):
print(x)
#Define path to saved images
path = 'tmp/test/'
os.makedirs(path, exist_ok=True)
# Define name of current video as number of videos in path
n_videos_in_path = len(os.listdir(path))
path = f'{path}{n_videos_in_path}'
os.makedirs(path, exist_ok=True)
# Define name of output video
outname = f'{path}_processed.mp4'
if os.path.exists(outname):
print('video already processed')
return outname
cap = cv2.VideoCapture(x)
counter = 0
while(cap.isOpened()):
frames = []
start = time()
for i in range(16):
start = time()
ret, frame = cap.read()
frames.append(frame)
if ret == False:
break
print(f'read time: {time()-start}')
#if ret==True:
#if counter % skip_frames == 0:
name = os.path.join(path,f'{counter:05d}.png')
# Get timing for inference
start = time()
frames = inference_frame(frames)
print(f'inference time: {time()-start}')
# write the flipped frame
start = time()
for frame in frames:
name = os.path.join(path,f'{counter:05d}.png')
cv2.imwrite(name, frame)
counter +=1
print(f'write time: {time()-start}')
# else:
# print(counter)
# counter +=1
# else:
# break
# Release everything if job is finished
cap.release()
# Create video from predicted images
print(path)
os.system(f'''ffmpeg -framerate {frame_rate_out} -pattern_type glob -i '{path}/*.png' -c:v libx264 -pix_fmt yuv420p {outname} -y''')
return outname
def analyze_video_parallel(x, skip_frames = 5,
frame_rate_out = 8, batch_size = 16):
print(x)
#Define path to saved images
path = '/tmp/test/'
os.makedirs(path, exist_ok=True)
# Define name of current video as number of videos in path
n_videos_in_path = len(os.listdir(path))
path = f'{path}{n_videos_in_path}'
os.makedirs(path, exist_ok=True)
# Define name of output video
outname = f'{path}_processed.mp4'
if os.path.exists(outname):
print('video already processed')
return outname
cap = cv2.VideoCapture(x)
counter = 0
pred_results_all = []
frames_all = []
while(cap.isOpened()):
frames = []
#start = time()
while len(frames) < batch_size:
#start = time()
ret, frame = cap.read()
if ret == False:
break
elif counter % skip_frames == 0:
frames.append(frame)
counter += 1
#print(f'read time: {time()-start}')
frames_all.extend(frames)
# Get timing for inference
start = time()
print('len frames passed: ', len(frames))
if len(frames) > 0:
pred_results = inference_frame_par_ready(frames)
print(f'inference time: {time()-start}')
pred_results_all.extend(pred_results)
# break while loop when return of the image reader is False
if ret == False:
break
print('exited prediction loop')
# Release everything if job is finished
cap.release()
start = time()
pool = mp.Pool(mp.cpu_count()-2)
pool_out = pool.map(process_frame,
list(zip(pred_results_all,
frames_all,
[i for i in range(len(pred_results_all))])))
pool.close()
print(f'pool time: {time()-start}')
start = time()
counter = 0
for pool_out_tmp in pool_out:
name = os.path.join(path,f'{counter:05d}.png')
cv2.imwrite(name, pool_out_tmp)
counter +=1
print(f'write time: {time()-start}')
# Create video from predicted images
print(path)
os.system(f'''ffmpeg -framerate {frame_rate_out} -pattern_type glob -i '{path}/*.png' -c:v libx264 -pix_fmt yuv420p {outname} -y''')
return outname
def set_example_image(example: list) -> dict:
return gr.Video.update(value=example[0])
def show_video(example: list) -> dict:
return gr.Video.update(value=example[0])
with gr.Blocks(title='Shark Patrol',theme=gr.themes.Soft(),live=True,) as demo:
gr.Markdown("Alpha Demo of the Sharkpatrol Oceanlife Detector.")
with gr.Tab("Preloaded Examples"):
with gr.Row():
video_example = gr.Video(source='upload',include_audio=False,stream=True)
with gr.Row():
paths = sorted(pathlib.Path('videos_example/').rglob('*rgb.mp4'))
example_preds = gr.Dataset(components=[video_example],
samples=[[path.as_posix()]
for path in paths])
example_preds.click(fn=show_video,
inputs=example_preds,
outputs=video_example)
with gr.Tab("Test your own Video"):
with gr.Row():
video_input = gr.Video(source='upload',include_audio=False)
#video_input.style(witdh='50%',height='50%')
video_output = gr.Video()
#video_output.style(witdh='50%',height='50%')
video_button = gr.Button("Analyze your Video")
with gr.Row():
paths = sorted(pathlib.Path('videos_example/').rglob('*.mp4'))
example_images = gr.Dataset(components=[video_input],
samples=[[path.as_posix()]
for path in paths if 'videos_side_by_side' not in str(path)])
video_button.click(analyze_video_parallel, inputs=video_input, outputs=video_output)
example_images.click(fn=set_example_image,
inputs=example_images,
outputs=video_input)
demo.queue()
#if os.getenv('SYSTEM') == 'spaces':
demo.launch(width='40%',auth=(os.environ.get('SHARK_USERNAME'), os.environ.get('SHARK_PASSWORD')))
|