Spaces:
Paused
Paused
Update gradio.py
Browse files
gradio.py
CHANGED
@@ -14,19 +14,26 @@ import io
|
|
14 |
from PIL import Image
|
15 |
import gradio as gr
|
16 |
import cv2
|
|
|
17 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
18 |
logger = logging.getLogger(__name__)
|
|
|
19 |
def SIGSEGV_signal_arises(signalNum, stack):
|
20 |
logger.critical(f"{signalNum} : SIGSEGV arises")
|
21 |
logger.critical(f"Stack trace: {stack}")
|
|
|
22 |
signal.signal(signal.SIGSEGV, SIGSEGV_signal_arises)
|
|
|
23 |
from loader import initialize_models
|
24 |
from engine import Engine, base64_data_uri_to_PIL_Image
|
25 |
from pathlib import Path
|
|
|
26 |
import cv2
|
|
|
27 |
# Global constants
|
28 |
DATA_ROOT = os.environ.get('DATA_ROOT', '/tmp/data')
|
29 |
MODELS_DIR = os.path.join(DATA_ROOT, "models")
|
|
|
30 |
async def setup():
|
31 |
live_portrait = await initialize_models()
|
32 |
engine = Engine(live_portrait=live_portrait)
|
@@ -45,6 +52,7 @@ async def setup():
|
|
45 |
|
46 |
cap.release()
|
47 |
return frames
|
|
|
48 |
async def return_image(image):
|
49 |
binary_data = Path(image).read_bytes()
|
50 |
res = await engine.load_image(binary_data)
|
@@ -56,6 +64,7 @@ async def setup():
|
|
56 |
})
|
57 |
|
58 |
return image
|
|
|
59 |
async def return_video(video):
|
60 |
print(video)
|
61 |
gr.Info("Extracting frames..")
|
@@ -63,10 +72,12 @@ async def setup():
|
|
63 |
gr.Info("Loading frames..")
|
64 |
res = await engine.load_frames(frames)
|
65 |
id = res['u']
|
|
|
66 |
height, width, _ = frames[0].shape
|
67 |
output_file = "output_video.mp4"
|
68 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
69 |
video_writer = cv2.VideoWriter(output_file, fourcc, 24.0, (width, height))
|
|
|
70 |
gr.Info("Processing..")
|
71 |
async for image in engine.transform_video(id, {
|
72 |
"aaa": -10,
|
@@ -75,8 +86,10 @@ async def setup():
|
|
75 |
}):
|
76 |
bgr_frame = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
|
77 |
video_writer.write(cv2.cvtColor(bgr_frame, cv2.COLOR_BGR2RGB))
|
|
|
78 |
video_writer.release()
|
79 |
return output_file
|
|
|
80 |
with gr.Blocks(title="Retorno de Imagem") as interface:
|
81 |
gr.Markdown("## 📼 Conversor de Vídeo para Imagem")
|
82 |
|
@@ -91,7 +104,10 @@ async def setup():
|
|
91 |
inputs=video_input,
|
92 |
outputs=image_output,
|
93 |
)
|
|
|
94 |
interface.launch(share=True)
|
|
|
|
|
95 |
if __name__ == "__main__":
|
96 |
asyncio.run(setup())'''
|
97 |
|
@@ -145,13 +161,20 @@ async def setup():
|
|
145 |
|
146 |
return output_file
|
147 |
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
|
156 |
interface.launch(share=True)
|
157 |
|
|
|
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)
|
|
|
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)
|
|
|
64 |
})
|
65 |
|
66 |
return image
|
67 |
+
|
68 |
async def return_video(video):
|
69 |
print(video)
|
70 |
gr.Info("Extracting frames..")
|
|
|
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,
|
|
|
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 |
|
|
|
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())'''
|
113 |
|
|
|
161 |
|
162 |
return output_file
|
163 |
|
164 |
+
with gr.Blocks(title="Retorno de Imagem") as interface:
|
165 |
+
gr.Markdown("## 📼 Conversor de Vídeo para Imagem")
|
166 |
+
|
167 |
+
with gr.Row(): # Organiza os componentes em uma linha
|
168 |
+
video_input = gr.Video(label="Carregue seu vídeo")
|
169 |
+
image_output = gr.Video(label="Imagem Processada",)
|
170 |
+
|
171 |
+
submit_btn = gr.Button("🔁 Processar", variant="primary") # Estilo primário
|
172 |
+
|
173 |
+
submit_btn.click(
|
174 |
+
fn=return_video, # Sua função de processamento
|
175 |
+
inputs=video_input,
|
176 |
+
outputs=image_output,
|
177 |
+
)
|
178 |
|
179 |
interface.launch(share=True)
|
180 |
|