File size: 2,591 Bytes
82e48c9
cdb3457
 
bb90a83
 
 
cdb3457
bb90a83
 
 
 
d941b57
82e48c9
 
 
 
bb90a83
 
 
82e48c9
 
d941b57
82e48c9
 
 
 
 
d941b57
 
 
82e48c9
 
d941b57
bb90a83
 
 
 
 
19d70b2
bb90a83
 
cdb3457
bb90a83
 
 
 
 
 
 
 
 
 
 
 
1cabe85
82ee94e
bb90a83
 
 
 
d941b57
c415cfb
19d70b2
c415cfb
 
1cabe85
82ee94e
c415cfb
cdb3457
bb90a83
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
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()