TDN-M commited on
Commit
db239e6
·
verified ·
1 Parent(s): 228378a

Update app.py

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