Spaces:
Runtime error
Runtime error
update
Browse files
app.py
CHANGED
@@ -1,99 +1,33 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
-
import time
|
4 |
-
import json
|
5 |
-
import yaml
|
6 |
-
import pandas as pd
|
7 |
-
from datetime import datetime
|
8 |
-
from moviepy.editor import VideoFileClip
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
video_duration = video_clip.duration
|
21 |
-
memory_usage = os.path.getsize(video_path) # in bytes
|
22 |
-
process_time = time.time() - start_time
|
23 |
-
upload_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
24 |
-
|
25 |
-
# Collect the log
|
26 |
-
log = {
|
27 |
-
"filename": video.name,
|
28 |
-
"collect_date_time": upload_time,
|
29 |
-
"video_duration": video_duration,
|
30 |
-
"process_time": process_time,
|
31 |
-
"memory_usage": memory_usage
|
32 |
-
}
|
33 |
-
|
34 |
-
# Save log in different formats
|
35 |
-
save_log(log)
|
36 |
-
|
37 |
-
return video_path, log
|
38 |
-
|
39 |
-
def save_log(log):
|
40 |
-
# Save to CSV
|
41 |
-
csv_file = "logs.csv"
|
42 |
-
df = pd.DataFrame([log])
|
43 |
-
if os.path.exists(csv_file):
|
44 |
-
df.to_csv(csv_file, mode='a', header=False, index=False)
|
45 |
-
else:
|
46 |
-
df.to_csv(csv_file, index=False)
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
if os.path.exists(json_file):
|
51 |
-
with open(json_file, 'r') as f:
|
52 |
-
logs = json.load(f)
|
53 |
-
logs.append(log)
|
54 |
-
with open(json_file, 'w') as f:
|
55 |
-
json.dump(logs, f, indent=4)
|
56 |
-
else:
|
57 |
-
with open(json_file, 'w') as f:
|
58 |
-
json.dump([log], f, indent=4)
|
59 |
|
60 |
-
|
61 |
-
yaml_file = "logs.yaml"
|
62 |
-
if os.path.exists(yaml_file):
|
63 |
-
with open(yaml_file, 'r') as f:
|
64 |
-
logs = yaml.safe_load(f)
|
65 |
-
logs.append(log)
|
66 |
-
with open(yaml_file, 'w') as f:
|
67 |
-
yaml.dump(logs, f)
|
68 |
-
else:
|
69 |
-
with open(yaml_file, 'w') as f:
|
70 |
-
yaml.dump([log], f)
|
71 |
-
|
72 |
-
def display_logs():
|
73 |
-
logs = []
|
74 |
-
if os.path.exists("logs.json"):
|
75 |
-
with open("logs.json", 'r') as f:
|
76 |
-
logs = json.load(f)
|
77 |
-
return logs
|
78 |
-
|
79 |
-
# Create Gradio interface
|
80 |
-
upload_video = gr.Interface(
|
81 |
-
fn=process_video,
|
82 |
-
inputs=gr.inputs.File(label="Upload Video"),
|
83 |
-
outputs=[gr.outputs.Video(label="Processed Video"), gr.outputs.JSON(label="Log Details")],
|
84 |
-
live=True
|
85 |
-
)
|
86 |
-
|
87 |
-
view_logs = gr.Interface(
|
88 |
-
fn=display_logs,
|
89 |
-
inputs=None,
|
90 |
-
outputs=gr.outputs.JSON(label="Logs History")
|
91 |
-
)
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
96 |
)
|
97 |
|
98 |
-
|
99 |
-
app.launch()
|
|
|
1 |
+
import cv2
|
2 |
import gradio as gr
|
3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
def convert_to_grayscale(video_file):
|
6 |
+
cap = cv2.VideoCapture(video_file)
|
7 |
+
output_file = "output.mp4"
|
8 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
9 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
10 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
11 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
12 |
+
out = cv2.VideoWriter(output_file, fourcc, fps, (width, height), isColor=False)
|
13 |
|
14 |
+
while(cap.isOpened()):
|
15 |
+
ret, frame = cap.read()
|
16 |
+
if not ret:
|
17 |
+
break
|
18 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
19 |
+
out.write(gray)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
cap.release()
|
22 |
+
out.release()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
return output_file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
demo = gr.Interface(
|
27 |
+
fn=convert_to_grayscale,
|
28 |
+
title="Video Upload and Display",
|
29 |
+
inputs=gr.Video(label="Upload Video", height=500, width=500),
|
30 |
+
outputs=gr.Video(label="Grayscale Video", height=500, width=500),
|
31 |
)
|
32 |
|
33 |
+
demo.launch()
|
|