bcci commited on
Commit
202725c
·
verified ·
1 Parent(s): 8167f90

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import numpy as np
4
+ from PIL import Image, ImageEnhance
5
+ from tqdm import tqdm
6
+ import gradio as gr
7
+ import tempfile
8
+
9
+ def enhance_frame(frame, sharpness=4, contrast=1.1, blur=3):
10
+ """Enhance a video frame's sharpness, contrast, and blur."""
11
+ try:
12
+ # Convert the frame to RGB (if it's in BGR format)
13
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
14
+
15
+ # Convert the frame to PIL Image
16
+ pil_img = Image.fromarray(frame)
17
+
18
+ # Enhance the sharpness
19
+ enhancer = ImageEnhance.Sharpness(pil_img)
20
+ enhanced_frame = enhancer.enhance(sharpness)
21
+
22
+ # Enhance the contrast
23
+ enhancer = ImageEnhance.Contrast(enhanced_frame)
24
+ enhanced_frame = enhancer.enhance(contrast)
25
+
26
+ # Convert back to numpy array
27
+ enhanced_frame = np.array(enhanced_frame)
28
+
29
+ # Apply Gaussian blur
30
+ enhanced_frame = cv2.GaussianBlur(enhanced_frame, (blur, blur), 0)
31
+
32
+ # Convert back to BGR format for OpenCV
33
+ enhanced_frame = cv2.cvtColor(enhanced_frame, cv2.COLOR_RGB2BGR)
34
+
35
+ return enhanced_frame
36
+ except Exception as e:
37
+ raise ValueError(f"Error enhancing frame: {str(e)}")
38
+
39
+ def enhance_video(input_path, output_path, sharpness=4, contrast=1.3, blur=3):
40
+ """Enhance a video's sharpness, contrast, and blur."""
41
+ try:
42
+ # Open the video file
43
+ cap = cv2.VideoCapture(input_path)
44
+
45
+ if not cap.isOpened():
46
+ raise ValueError("Error: Could not open video file.")
47
+
48
+ # Get video properties
49
+ fps = cap.get(cv2.CAP_PROP_FPS)
50
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
51
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
52
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
53
+
54
+ if total_frames == 0:
55
+ raise ValueError("Error: Video file has no frames.")
56
+
57
+ # Define the codec and create VideoWriter object
58
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Using mp4v codec for mp4 files
59
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
60
+
61
+ if not out.isOpened():
62
+ cap.release()
63
+ raise ValueError("Error: Could not open video writer.")
64
+
65
+ # Process each frame with tqdm progress bar
66
+ for _ in tqdm(range(total_frames), desc="Processing frames"):
67
+ ret, frame = cap.read()
68
+ if not ret:
69
+ break
70
+
71
+ # Enhance the frame
72
+ enhanced_frame = enhance_frame(frame, sharpness, contrast, blur)
73
+
74
+ # Write the enhanced frame to the output video
75
+ out.write(enhanced_frame)
76
+
77
+ # Release everything when done
78
+ cap.release()
79
+ out.release()
80
+ except Exception as e:
81
+ if 'cap' in locals():
82
+ cap.release()
83
+ if 'out' in locals():
84
+ out.release()
85
+ raise ValueError(f"Error enhancing video: {str(e)}")
86
+
87
+ def video_enhancer(input_video, sharpness=4, contrast=1.3, blur=3):
88
+ """Gradio interface function to enhance videos."""
89
+ try:
90
+ # Check if input file exists
91
+ if not os.path.exists(input_video):
92
+ raise FileNotFoundError("Input video file not found.")
93
+
94
+ # Generate a temporary output path
95
+ temp_dir = tempfile.gettempdir()
96
+ base_name = os.path.basename(input_video)
97
+ name, ext = os.path.splitext(base_name)
98
+ output_path = os.path.join(temp_dir, f"enhanced_{name}.mp4")
99
+
100
+ # Process the video
101
+ enhance_video(input_video, output_path, sharpness, contrast, blur)
102
+
103
+ return output_path
104
+ except Exception as e:
105
+ return f"Error: {str(e)}"
106
+
107
+ # Create the Gradio interface
108
+ iface = gr.Interface(
109
+ fn=video_enhancer,
110
+ inputs=[
111
+ gr.Video(label="Upload Video"),
112
+ gr.Slider(minimum=1, maximum=10, value=4, label="Sharpness"),
113
+ gr.Slider(minimum=0.5, maximum=2, step=0.1, value=1.1, label="Contrast"),
114
+ gr.Slider(minimum=1, maximum=10, step=0.1, value=3, label="Blur")
115
+ ],
116
+ outputs=gr.Video(label="Download Enhanced Video"),
117
+ title="Video Enhancer",
118
+ description="Enhance your videos by adjusting sharpness, contrast, and blur."
119
+ )
120
+
121
+ iface.launch()