File size: 1,242 Bytes
c40e9d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# This is a Gradio app that applies an unsharp mask to a video using OpenCV and outputs a new video.
import gradio as gr
import cv2
import numpy as np

# Define a function to apply an unsharp mask to a frame
def unsharp_mask(frame, kernel_size=(5, 5), sigma=1.0, strength=1.0):
    blurred = cv2.GaussianBlur(frame, kernel_size, sigma)
    sharpened = cv2.addWeighted(frame, 1.0 + strength, blurred, -strength, 0)
    return sharpened

# Define a function to process the video and apply the unsharp mask to each frame
def process_video(video_path):
    cap = cv2.VideoCapture(video_path)
    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))
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    out = cv2.VideoWriter('output.mp4', fourcc, fps, (width, height))

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        sharpened_frame = unsharp_mask(frame)
        out.write(sharpened_frame)

    cap.release()
    out.release()
    return 'output.mp4'

demo = gr.Interface(fn=process_video, inputs="video", outputs="video")

# Launch the interface
if __name__ == "__main__":
    demo.launch(show_error=True)