video-enhance / app.py
inoculatemedia's picture
Update app.py
bdc5519 verified
# 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)