TAneKAnz's picture
history_table
19d70b2 verified
import cv2
import gradio as gr
import os
from datetime import datetime
import time
import psutil
def convert_to_grayscale(video_file, history):
start_time = time.time()
file_name = os.path.splitext(os.path.basename(video_file))[0]
output_file = f"{file_name}_output.mp4"
cap = cv2.VideoCapture(video_file)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
video_duration = frame_count / fps
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_file, fourcc, fps, (width, height), isColor=False)
while(cap.isOpened()):
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray_flipped = cv2.flip(gray, 1)
out.write(gray_flipped)
cap.release()
out.release()
end_time = time.time()
process_time = end_time - start_time
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
memory_usage = psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024 # in MB
history.append(f"#{len(history)+1} {file_name} {current_time} {video_duration:.2f} {process_time:.2f} {memory_usage:.2f}")
return output_file, history
with gr.Blocks() as demo:
history_state = gr.State([])
with gr.Tab("Convert Video"):
with gr.Row(""):
video_input = gr.Video(label="Upload Video", height=500, width=500)
video_output = gr.Video(label="Grayscale Video", height=500, width=500)
convert_button = gr.Button("Convert")
convert_button.click(
fn=convert_to_grayscale,
inputs=[video_input, history_state],
outputs=[video_output, history_state],
show_progress=True
)
with gr.Tab("History") as history_tab:
history_output = gr.HTML(label="History")
history_output.change(
fn=lambda history: "<table><tr><th>Entry</th><th>File Name</th><th>Collected On</th><th>Video Duration</th><th>Processing Time</th><th>Memory Usage</th></tr>" + "".join([f"<tr><td>{entry.split()[0]}</td><td>{entry.split()[1]}</td><td>{entry.split()[2]+' '+entry.split()[3]}</td><td>{entry.split()[4]} seconds</td><td>{entry.split()[5]} seconds</td><td>{entry.split()[6]} MB</td></tr>" for entry in history]) + "</table>",
inputs=history_state,
outputs=history_output,
queue=False,
show_progress=False
)
demo.launch()