Spaces:
Sleeping
Sleeping
Create video_enhancer.py
Browse files- video_enhancer.py +68 -0
video_enhancer.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
import io
|
5 |
+
import asyncio
|
6 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
7 |
+
from realesrgan import RealESRGANer
|
8 |
+
from huggingface_hub import hf_hub_download
|
9 |
+
from concurrent.futures import ThreadPoolExecutor
|
10 |
+
|
11 |
+
class VideoEnhancer:
|
12 |
+
def __init__(self, model_name="RealESRGAN_x4plus"):
|
13 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
14 |
+
self.model = self.load_model(model_name)
|
15 |
+
self.executor = ThreadPoolExecutor(max_workers=4)
|
16 |
+
|
17 |
+
def load_model(self, model_name):
|
18 |
+
if model_name == "RealESRGAN_x4plus":
|
19 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
20 |
+
model_path = hf_hub_download("schwgHao/RealESRGAN_x4plus", "RealESRGAN_x4plus.pth")
|
21 |
+
return RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=True)
|
22 |
+
else:
|
23 |
+
raise ValueError(f"Unsupported model: {model_name}")
|
24 |
+
|
25 |
+
async def enhance_frame(self, frame):
|
26 |
+
loop = asyncio.get_running_loop()
|
27 |
+
|
28 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
29 |
+
|
30 |
+
enhanced, _ = await loop.run_in_executor(self.executor, self.model.enhance, frame_rgb)
|
31 |
+
|
32 |
+
return cv2.cvtColor(enhanced, cv2.COLOR_RGB2BGR)
|
33 |
+
|
34 |
+
async def process_video(self, input_bytes, output_bytes):
|
35 |
+
cap = cv2.VideoCapture(input_bytes)
|
36 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
37 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
38 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
39 |
+
|
40 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
41 |
+
out = cv2.VideoWriter(output_bytes, fourcc, fps, (width * 4, height * 4))
|
42 |
+
|
43 |
+
while cap.isOpened():
|
44 |
+
ret, frame = cap.read()
|
45 |
+
if not ret:
|
46 |
+
break
|
47 |
+
enhanced_frame = await self.enhance_frame(frame)
|
48 |
+
out.write(enhanced_frame)
|
49 |
+
|
50 |
+
cap.release()
|
51 |
+
out.release()
|
52 |
+
|
53 |
+
async def stream_enhanced_video(self, video_file):
|
54 |
+
video_bytes = await video_file.read()
|
55 |
+
cap = cv2.VideoCapture(io.BytesIO(video_bytes).getvalue())
|
56 |
+
|
57 |
+
async def generate():
|
58 |
+
while cap.isOpened():
|
59 |
+
ret, frame = cap.read()
|
60 |
+
if not ret:
|
61 |
+
break
|
62 |
+
enhanced_frame = await self.enhance_frame(frame)
|
63 |
+
_, buffer = cv2.imencode('.jpg', enhanced_frame)
|
64 |
+
yield (b'--frame\r\n'
|
65 |
+
b'Content-Type: image/jpeg\r\n\r\n' + buffer.tobytes() + b'\r\n')
|
66 |
+
cap.release()
|
67 |
+
|
68 |
+
return StreamingResponse(generate(), media_type="multipart/x-mixed-replace; boundary=frame")
|