TDN-M commited on
Commit
7413d5a
·
verified ·
1 Parent(s): 41d4788

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import cv2
4
+ from basicsr.archs.rrdbnet_arch import RRDBNet
5
+ from realesrgan import RealESRGANer
6
+ import shutil
7
+
8
+ # Thư mục tạm thời để lưu trữ file tải lên
9
+ UPLOAD_FOLDER = "uploads"
10
+ OUTPUT_FOLDER = "outputs"
11
+ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
12
+ os.makedirs(OUTPUT_FOLDER, exist_ok=True)
13
+
14
+ # Khởi tạo mô hình Real-ESRGAN
15
+ model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
16
+ upsampler = RealESRGANer(
17
+ scale=4,
18
+ model_path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth",
19
+ model=model,
20
+ tile=0,
21
+ tile_pad=10,
22
+ pre_pad=0,
23
+ half=True
24
+ )
25
+
26
+ def enhance_video(input_video):
27
+ """
28
+ Hàm xử lý video đầu vào và trả về video đã tăng cường.
29
+ """
30
+ # Lưu file tải lên vào thư mục uploads
31
+ input_path = os.path.join(UPLOAD_FOLDER, "input.mp4")
32
+ output_path = os.path.join(OUTPUT_FOLDER, "enhanced_output.mp4")
33
+ input_video.save(input_path)
34
+
35
+ # Tách video thành khung hình
36
+ frames_folder = os.path.join(UPLOAD_FOLDER, "frames")
37
+ os.makedirs(frames_folder, exist_ok=True)
38
+ extract_frames(input_path, frames_folder)
39
+
40
+ # Xử lý từng khung hình với Real-ESRGAN
41
+ enhanced_frames_folder = os.path.join(OUTPUT_FOLDER, "enhanced_frames")
42
+ os.makedirs(enhanced_frames_folder, exist_ok=True)
43
+ enhance_frames(frames_folder, enhanced_frames_folder)
44
+
45
+ # Ghép các khung hình đã tăng cường thành video
46
+ combine_frames_to_video(enhanced_frames_folder, output_path)
47
+
48
+ # Xóa các thư mục tạm thời
49
+ shutil.rmtree(frames_folder)
50
+ shutil.rmtree(enhanced_frames_folder)
51
+
52
+ return output_path
53
+
54
+ def extract_frames(video_path, output_folder):
55
+ """
56
+ Tách video thành các khung hình.
57
+ """
58
+ cap = cv2.VideoCapture(video_path)
59
+ frame_count = 0
60
+ while True:
61
+ ret, frame = cap.read()
62
+ if not ret:
63
+ break
64
+ frame_path = os.path.join(output_folder, f"frame_{frame_count:04d}.png")
65
+ cv2.imwrite(frame_path, frame)
66
+ frame_count += 1
67
+ cap.release()
68
+
69
+ def enhance_frames(input_folder, output_folder):
70
+ """
71
+ Tăng cường từng khung hình bằng Real-ESRGAN.
72
+ """
73
+ for frame_name in os.listdir(input_folder):
74
+ frame_path = os.path.join(input_folder, frame_name)
75
+ img = cv2.imread(frame_path, cv2.IMREAD_UNCHANGED)
76
+ enhanced_img, _ = upsampler.enhance(img, outscale=4)
77
+ output_path = os.path.join(output_folder, frame_name)
78
+ cv2.imwrite(output_path, enhanced_img)
79
+
80
+ def combine_frames_to_video(frames_folder, output_path):
81
+ """
82
+ Ghép các khung hình thành video.
83
+ """
84
+ frame_rate = 30 # Số khung hình/giây
85
+ first_frame = os.path.join(frames_folder, os.listdir(frames_folder)[0])
86
+ height, width, _ = cv2.imread(first_frame).shape
87
+
88
+ fourcc = cv2.VideoWriter_fourcc(*"mp4v")
89
+ video_writer = cv2.VideoWriter(output_path, fourcc, frame_rate, (width, height))
90
+
91
+ for frame_name in sorted(os.listdir(frames_folder)):
92
+ frame_path = os.path.join(frames_folder, frame_name)
93
+ frame = cv2.imread(frame_path)
94
+ video_writer.write(frame)
95
+
96
+ video_writer.release()
97
+
98
+ # Tạo giao diện Gradio
99
+ with gr.Blocks() as demo:
100
+ gr.Markdown("# Video Enhancement with Real-ESRGAN")
101
+ gr.Markdown("Upload a video and get an enhanced version with improved quality.")
102
+
103
+ with gr.Row():
104
+ input_video = gr.File(label="Upload Video")
105
+ output_video = gr.Video(label="Enhanced Video")
106
+
107
+ submit_button = gr.Button("Enhance Video")
108
+ submit_button.click(enhance_video, inputs=input_video, outputs=output_video)
109
+
110
+ # Khởi chạy ứng dụng
111
+ demo.launch()