|
|
import os |
|
|
import cv2 |
|
|
import numpy as np |
|
|
from PIL import Image, ImageEnhance |
|
|
from tqdm import tqdm |
|
|
import gradio as gr |
|
|
import tempfile |
|
|
|
|
|
def enhance_frame(frame, sharpness=4, contrast=1.1, blur=3): |
|
|
"""Enhance a video frame's sharpness, contrast, and blur.""" |
|
|
try: |
|
|
|
|
|
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
|
|
|
|
|
|
|
|
pil_img = Image.fromarray(frame) |
|
|
|
|
|
|
|
|
enhancer = ImageEnhance.Sharpness(pil_img) |
|
|
enhanced_frame = enhancer.enhance(sharpness) |
|
|
|
|
|
|
|
|
enhancer = ImageEnhance.Contrast(enhanced_frame) |
|
|
enhanced_frame = enhancer.enhance(contrast) |
|
|
|
|
|
|
|
|
enhanced_frame = np.array(enhanced_frame) |
|
|
|
|
|
|
|
|
enhanced_frame = cv2.GaussianBlur(enhanced_frame, (blur, blur), 0) |
|
|
|
|
|
|
|
|
enhanced_frame = cv2.cvtColor(enhanced_frame, cv2.COLOR_RGB2BGR) |
|
|
|
|
|
return enhanced_frame |
|
|
except Exception as e: |
|
|
raise ValueError(f"Error enhancing frame: {str(e)}") |
|
|
|
|
|
def enhance_video(input_path, output_path, sharpness=4, contrast=1.3, blur=3): |
|
|
"""Enhance a video's sharpness, contrast, and blur.""" |
|
|
try: |
|
|
|
|
|
cap = cv2.VideoCapture(input_path) |
|
|
|
|
|
if not cap.isOpened(): |
|
|
raise ValueError("Error: Could not open video file.") |
|
|
|
|
|
|
|
|
fps = cap.get(cv2.CAP_PROP_FPS) |
|
|
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) |
|
|
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
|
|
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
|
|
|
|
|
if total_frames == 0: |
|
|
raise ValueError("Error: Video file has no frames.") |
|
|
|
|
|
|
|
|
fourcc = cv2.VideoWriter_fourcc(*'mp4v') |
|
|
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height)) |
|
|
|
|
|
if not out.isOpened(): |
|
|
cap.release() |
|
|
raise ValueError("Error: Could not open video writer.") |
|
|
|
|
|
|
|
|
for _ in tqdm(range(total_frames), desc="Processing frames"): |
|
|
ret, frame = cap.read() |
|
|
if not ret: |
|
|
break |
|
|
|
|
|
|
|
|
enhanced_frame = enhance_frame(frame, sharpness, contrast, blur) |
|
|
|
|
|
|
|
|
out.write(enhanced_frame) |
|
|
|
|
|
|
|
|
cap.release() |
|
|
out.release() |
|
|
except Exception as e: |
|
|
if 'cap' in locals(): |
|
|
cap.release() |
|
|
if 'out' in locals(): |
|
|
out.release() |
|
|
raise ValueError(f"Error enhancing video: {str(e)}") |
|
|
|
|
|
def video_enhancer(input_video, sharpness=4, contrast=1.3, blur=3): |
|
|
"""Gradio interface function to enhance videos.""" |
|
|
try: |
|
|
|
|
|
if not os.path.exists(input_video): |
|
|
raise FileNotFoundError("Input video file not found.") |
|
|
|
|
|
|
|
|
temp_dir = tempfile.gettempdir() |
|
|
base_name = os.path.basename(input_video) |
|
|
name, ext = os.path.splitext(base_name) |
|
|
output_path = os.path.join(temp_dir, f"enhanced_{name}.mp4") |
|
|
|
|
|
|
|
|
enhance_video(input_video, output_path, sharpness, contrast, blur) |
|
|
|
|
|
return output_path |
|
|
except Exception as e: |
|
|
return f"Error: {str(e)}" |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=video_enhancer, |
|
|
inputs=[ |
|
|
gr.Video(label="Upload Video"), |
|
|
gr.Slider(minimum=1, maximum=10, value=4, label="Sharpness"), |
|
|
gr.Slider(minimum=0.5, maximum=2, step=0.1, value=1.1, label="Contrast"), |
|
|
gr.Slider(minimum=1, maximum=10, step=0.1, value=3, label="Blur") |
|
|
], |
|
|
outputs=gr.Video(label="Download Enhanced Video"), |
|
|
title="Video Enhancer", |
|
|
description="Enhance your videos by adjusting sharpness, contrast, and blur." |
|
|
) |
|
|
|
|
|
iface.launch() |
|
|
|