Spaces:
Paused
Paused
Create gradio.py
Browse files
gradio.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
import asyncio
|
3 |
+
from aiohttp import web, WSMsgType
|
4 |
+
import json
|
5 |
+
from json import JSONEncoder
|
6 |
+
import numpy as np
|
7 |
+
import uuid
|
8 |
+
import logging
|
9 |
+
import os
|
10 |
+
import signal
|
11 |
+
from typing import Dict, Any, List, Optional
|
12 |
+
import base64
|
13 |
+
import io
|
14 |
+
from PIL import Image
|
15 |
+
import gradio as gr
|
16 |
+
import cv2
|
17 |
+
|
18 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
19 |
+
logger = logging.getLogger(__name__)
|
20 |
+
|
21 |
+
def SIGSEGV_signal_arises(signalNum, stack):
|
22 |
+
logger.critical(f"{signalNum} : SIGSEGV arises")
|
23 |
+
logger.critical(f"Stack trace: {stack}")
|
24 |
+
|
25 |
+
signal.signal(signal.SIGSEGV, SIGSEGV_signal_arises)
|
26 |
+
|
27 |
+
from loader import initialize_models
|
28 |
+
from engine import Engine, base64_data_uri_to_PIL_Image
|
29 |
+
from pathlib import Path
|
30 |
+
|
31 |
+
import cv2
|
32 |
+
|
33 |
+
# Global constants
|
34 |
+
DATA_ROOT = os.environ.get('DATA_ROOT', '/tmp/data')
|
35 |
+
MODELS_DIR = os.path.join(DATA_ROOT, "models")
|
36 |
+
|
37 |
+
async def setup():
|
38 |
+
live_portrait = await initialize_models()
|
39 |
+
engine = Engine(live_portrait=live_portrait)
|
40 |
+
|
41 |
+
def get_all_frames(video_path):
|
42 |
+
cap = cv2.VideoCapture(video_path)
|
43 |
+
|
44 |
+
frames = []
|
45 |
+
while True:
|
46 |
+
ret, frame = cap.read()
|
47 |
+
|
48 |
+
if not ret:
|
49 |
+
break
|
50 |
+
|
51 |
+
frames.append(frame)
|
52 |
+
|
53 |
+
cap.release()
|
54 |
+
return frames
|
55 |
+
|
56 |
+
async def return_image(image):
|
57 |
+
binary_data = Path(image).read_bytes()
|
58 |
+
res = await engine.load_image(binary_data)
|
59 |
+
id = res['u']
|
60 |
+
_, image = await engine.transform_image(id, {
|
61 |
+
"aaa": -10,
|
62 |
+
"eee": -10,
|
63 |
+
"woo": -12
|
64 |
+
})
|
65 |
+
|
66 |
+
return image
|
67 |
+
|
68 |
+
async def return_video(video):
|
69 |
+
print(video)
|
70 |
+
gr.Info("Extracting frames..")
|
71 |
+
frames = get_all_frames(video)
|
72 |
+
gr.Info("Loading frames..")
|
73 |
+
res = await engine.load_frames(frames)
|
74 |
+
id = res['u']
|
75 |
+
|
76 |
+
height, width, _ = frames[0].shape
|
77 |
+
output_file = "output_video.mp4"
|
78 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
79 |
+
video_writer = cv2.VideoWriter(output_file, fourcc, 24.0, (width, height))
|
80 |
+
|
81 |
+
gr.Info("Processing..")
|
82 |
+
async for image in engine.transform_video(id, {
|
83 |
+
"aaa": -10,
|
84 |
+
"eee": -10,
|
85 |
+
"woo": -12
|
86 |
+
}):
|
87 |
+
bgr_frame = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
|
88 |
+
video_writer.write(cv2.cvtColor(bgr_frame, cv2.COLOR_BGR2RGB))
|
89 |
+
|
90 |
+
video_writer.release()
|
91 |
+
return output_file
|
92 |
+
|
93 |
+
with gr.Blocks(title="Retorno de Imagem") as interface:
|
94 |
+
gr.Markdown("## 📼 Conversor de Vídeo para Imagem")
|
95 |
+
|
96 |
+
with gr.Row(): # Organiza os componentes em uma linha
|
97 |
+
video_input = gr.Video(label="Carregue seu vídeo")
|
98 |
+
image_output = gr.Video(label="Imagem Processada",)
|
99 |
+
|
100 |
+
submit_btn = gr.Button("🔁 Processar", variant="primary") # Estilo primário
|
101 |
+
|
102 |
+
submit_btn.click(
|
103 |
+
fn=return_video, # Sua função de processamento
|
104 |
+
inputs=video_input,
|
105 |
+
outputs=image_output,
|
106 |
+
)
|
107 |
+
|
108 |
+
interface.launch(share=True)
|
109 |
+
|
110 |
+
|
111 |
+
if __name__ == "__main__":
|
112 |
+
asyncio.run(setup())
|