Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,51 +37,6 @@ def image_fn(
|
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
-
def video_fn(model_path, video_file, conf_thres, iou_thres, start_sec, duration):
|
| 41 |
-
model = yolov7.load(model_path, device="cpu", hf_model=True, trace=False)
|
| 42 |
-
start_timestamp = time.strftime("%H:%M:%S", time.gmtime(start_sec))
|
| 43 |
-
end_timestamp = time.strftime("%H:%M:%S", time.gmtime(start_sec + duration))
|
| 44 |
-
|
| 45 |
-
suffix = Path(video_file).suffix
|
| 46 |
-
|
| 47 |
-
clip_temp_file = tempfile.NamedTemporaryFile(suffix=suffix)
|
| 48 |
-
subprocess.call(
|
| 49 |
-
f"ffmpeg -y -ss {start_timestamp} -i {video_file} -to {end_timestamp} -c copy {clip_temp_file.name}".split()
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
# Reader of clip file
|
| 53 |
-
cap = cv2.VideoCapture(clip_temp_file.name)
|
| 54 |
-
|
| 55 |
-
# This is an intermediary temp file where we'll write the video to
|
| 56 |
-
# Unfortunately, gradio doesn't play too nice with videos rn so we have to do some hackiness
|
| 57 |
-
# with ffmpeg at the end of the function here.
|
| 58 |
-
with tempfile.NamedTemporaryFile(suffix=".mp4") as temp_file:
|
| 59 |
-
out = cv2.VideoWriter(temp_file.name, cv2.VideoWriter_fourcc(*"MP4V"), 30, (1280, 720))
|
| 60 |
-
|
| 61 |
-
num_frames = 0
|
| 62 |
-
max_frames = duration * 30
|
| 63 |
-
while cap.isOpened():
|
| 64 |
-
try:
|
| 65 |
-
ret, frame = cap.read()
|
| 66 |
-
if not ret:
|
| 67 |
-
break
|
| 68 |
-
except Exception as e:
|
| 69 |
-
print(e)
|
| 70 |
-
continue
|
| 71 |
-
print("FRAME DTYPE", type(frame))
|
| 72 |
-
out.write(model([frame], conf_thres, iou_thres))
|
| 73 |
-
num_frames += 1
|
| 74 |
-
print("Processed {} frames".format(num_frames))
|
| 75 |
-
if num_frames == max_frames:
|
| 76 |
-
break
|
| 77 |
-
|
| 78 |
-
out.release()
|
| 79 |
-
|
| 80 |
-
# Aforementioned hackiness
|
| 81 |
-
out_file = tempfile.NamedTemporaryFile(suffix="out.mp4", delete=False)
|
| 82 |
-
subprocess.run(f"ffmpeg -y -loglevel quiet -stats -i {temp_file.name} -c:v libx264 {out_file.name}".split())
|
| 83 |
-
|
| 84 |
-
return out_file.name
|
| 85 |
|
| 86 |
image_interface = gr.Interface(
|
| 87 |
fn=image_fn,
|
|
@@ -106,31 +61,9 @@ image_interface = gr.Interface(
|
|
| 106 |
)
|
| 107 |
|
| 108 |
|
| 109 |
-
video_interface = gr.Interface(
|
| 110 |
-
fn=video_fn,
|
| 111 |
-
inputs=[
|
| 112 |
-
gr.inputs.Video(source = "upload", type = "mp4", label = "Input Video"),
|
| 113 |
-
gr.inputs.Dropdown(
|
| 114 |
-
choices=[
|
| 115 |
-
"Aalaa/Yolov7_Visual_Pollution_Detection",
|
| 116 |
-
],
|
| 117 |
-
default="Aalaa/Yolov7_Visual_Pollution_Detection",
|
| 118 |
-
label="Model",
|
| 119 |
-
),
|
| 120 |
-
],
|
| 121 |
-
outputs=gr.outputs.Video(type = "mp4", label = "Output Video"),
|
| 122 |
-
# examples=[
|
| 123 |
-
# ["video.mp4", 0.25, 0.45, 0, 2],
|
| 124 |
-
|
| 125 |
-
# ],
|
| 126 |
-
title="Smart Environmental Eye (SEE)",
|
| 127 |
-
cache_examples=True,
|
| 128 |
-
theme='huggingface',
|
| 129 |
-
|
| 130 |
-
)
|
| 131 |
|
| 132 |
if __name__ == "__main__":
|
| 133 |
gr.TabbedInterface(
|
| 134 |
-
[image_interface
|
| 135 |
-
["Run on Images"
|
| 136 |
).launch()
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
image_interface = gr.Interface(
|
| 42 |
fn=image_fn,
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
if __name__ == "__main__":
|
| 66 |
gr.TabbedInterface(
|
| 67 |
+
[image_interface],
|
| 68 |
+
["Run on Images"],
|
| 69 |
).launch()
|