William278989 commited on
Commit
f75befb
·
verified ·
1 Parent(s): db226ec

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import numpy as np
4
+ import onnxruntime as ort
5
+ import gradio as gr
6
+ import subprocess
7
+ import shutil
8
+ from huggingface_hub import hf_hub_download
9
+
10
+ # --- AUTH & MODEL SETUP ---
11
+ HF_TOKEN = os.getenv("HF_TOKEN")
12
+
13
+ def get_onnx_model():
14
+ try:
15
+ model_path = hf_hub_download(
16
+ repo_id="KingPro100/real-esrgan-onxx",
17
+ filename="Real-ESRGAN-x4plus.onnx",
18
+ token=HF_TOKEN
19
+ )
20
+ return model_path
21
+ except Exception as e:
22
+ # Fallback to Xenova if the path is tricky
23
+ return hf_hub_download(repo_id="Xenova/realesrgan-x4plus", filename="onnx/model.onnx")
24
+
25
+ MODEL_FILE = get_onnx_model()
26
+
27
+ # CPU Stability Settings
28
+ sess_options = ort.SessionOptions()
29
+ sess_options.intra_op_num_threads = 2
30
+ session = ort.InferenceSession(MODEL_FILE, sess_options, providers=['CPUExecutionProvider'])
31
+
32
+ def upscale_frame(frame):
33
+ img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
34
+ img = img.astype(np.float32) / 255.0
35
+ img = np.transpose(img, (2, 0, 1))
36
+ img = np.expand_dims(img, axis=0)
37
+
38
+ inputs = {session.get_inputs()[0].name: img}
39
+ output = session.run(None, inputs)[0]
40
+
41
+ output = np.squeeze(output)
42
+ output = np.clip(output, 0, 1)
43
+ output = np.transpose(output, (1, 2, 0))
44
+ output = cv2.cvtColor((output * 255.0).astype(np.uint8), cv2.COLOR_RGB2BGR)
45
+ return output
46
+
47
+ def process_video(input_path, do_sharpen, progress=gr.Progress()):
48
+ if not input_path: return None
49
+
50
+ cap = cv2.VideoCapture(input_path)
51
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
52
+
53
+ # 1. Extract Audio
54
+ audio_path = "temp_audio.mp3"
55
+ subprocess.run(f"ffmpeg -i {input_path} -vn -acodec libmp3lame {audio_path} -y", shell=True)
56
+
57
+ # 2. Setup Frames Dir
58
+ frames_dir = "temp_frames"
59
+ if os.path.exists(frames_dir): shutil.rmtree(frames_dir)
60
+ os.makedirs(frames_dir)
61
+
62
+ count = 0
63
+ while True:
64
+ ret, frame = cap.read()
65
+ if not ret: break
66
+
67
+ try:
68
+ upscaled = upscale_frame(frame)
69
+ cv2.imwrite(f"{frames_dir}/frame_{count:05d}.png", upscaled)
70
+ except Exception as e:
71
+ # Fallback for individual frame failure
72
+ h, w = frame.shape[:2]
73
+ upscaled = cv2.resize(frame, (w*4, h*4), interpolation=cv2.INTER_LANCZOS4)
74
+ cv2.imwrite(f"{frames_dir}/frame_{count:05d}.png", upscaled)
75
+
76
+ count += 1
77
+ if count % 5 == 0:
78
+ progress(count/total_frames, desc=f"4x Scaling: {count}/{total_frames}")
79
+
80
+ cap.release()
81
+
82
+ # 3. Final Reassembly with Optional Sharpening
83
+ output_video = "upscaled_output.mp4"
84
+
85
+ # FFmpeg 'unsharp' filter for clarity
86
+ # Settings: luma_matrix_width:luma_matrix_height:luma_amount
87
+ sharpen_filter = "-vf \"unsharp=5:5:1.0\"" if do_sharpen else ""
88
+
89
+ ffmpeg_cmd = (
90
+ f"ffmpeg -framerate 24 -i {frames_dir}/frame_%05d.png -i {audio_path} "
91
+ f"{sharpen_filter} "
92
+ f"-c:v libx264 -preset superfast -pix_fmt yuv420p -c:a aac -shortest {output_video} -y"
93
+ )
94
+ subprocess.run(ffmpeg_cmd, shell=True)
95
+
96
+ # Cleanup
97
+ shutil.rmtree(frames_dir)
98
+ if os.path.exists(audio_path): os.remove(audio_path)
99
+
100
+ return output_video
101
+
102
+ # --- Updated UI with Toggle ---
103
+ demo = gr.Interface(
104
+ fn=process_video,
105
+ inputs=[
106
+ gr.Video(label="Upload Video"),
107
+ gr.Checkbox(label="Enable Post-Upscale Sharpening", value=False, info="Check this if the AI output looks too soft or blurry.")
108
+ ],
109
+ outputs=gr.Video(label="Upscaled Result"),
110
+ title="Real-ESRGAN 4x CPU (with Clarity Toggle)",
111
+ description="Processes video to 4x resolution. Use the toggle to add extra sharpness if needed."
112
+ )
113
+
114
+ if __name__ == "__main__":
115
+ demo.launch()