Spaces:
Paused
Paused
Update gradio.py
Browse files
gradio.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import sys
|
2 |
import asyncio
|
3 |
from aiohttp import web, WSMsgType
|
4 |
import json
|
@@ -14,26 +14,19 @@ 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)
|
@@ -52,7 +45,6 @@ async def setup():
|
|
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,7 +56,6 @@ async def setup():
|
|
64 |
})
|
65 |
|
66 |
return image
|
67 |
-
|
68 |
async def return_video(video):
|
69 |
print(video)
|
70 |
gr.Info("Extracting frames..")
|
@@ -72,12 +63,10 @@ async def setup():
|
|
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,10 +75,8 @@ async def setup():
|
|
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,6 +91,67 @@ async def setup():
|
|
104 |
inputs=video_input,
|
105 |
outputs=image_output,
|
106 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
interface.launch(share=True)
|
109 |
|
|
|
1 |
+
'''import sys
|
2 |
import asyncio
|
3 |
from aiohttp import web, WSMsgType
|
4 |
import json
|
|
|
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 |
|
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 |
})
|
57 |
|
58 |
return image
|
|
|
59 |
async def return_video(video):
|
60 |
print(video)
|
61 |
gr.Info("Extracting frames..")
|
|
|
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 |
}):
|
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 |
inputs=video_input,
|
92 |
outputs=image_output,
|
93 |
)
|
94 |
+
interface.launch(share=True)
|
95 |
+
if __name__ == "__main__":
|
96 |
+
asyncio.run(setup())'''
|
97 |
+
|
98 |
+
import sys
|
99 |
+
import asyncio
|
100 |
+
from aiohttp import web, WSMsgType
|
101 |
+
import json
|
102 |
+
from json import JSONEncoder
|
103 |
+
import numpy as np
|
104 |
+
import uuid
|
105 |
+
import logging
|
106 |
+
import os
|
107 |
+
import signal
|
108 |
+
from typing import Dict, Any, List, Optional
|
109 |
+
import base64
|
110 |
+
import io
|
111 |
+
from PIL import Image
|
112 |
+
import gradio as gr
|
113 |
+
import cv2
|
114 |
+
|
115 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
116 |
+
logger = logging.getLogger(__name__)
|
117 |
+
|
118 |
+
def SIGSEGV_signal_arises(signalNum, stack):
|
119 |
+
logger.critical(f"{signalNum} : SIGSEGV arises")
|
120 |
+
logger.critical(f"Stack trace: {stack}")
|
121 |
+
|
122 |
+
signal.signal(signal.SIGSEGV, SIGSEGV_signal_arises)
|
123 |
+
|
124 |
+
from loader import initialize_models
|
125 |
+
from engine import Engine, base64_data_uri_to_PIL_Image
|
126 |
+
from pathlib import Path
|
127 |
+
|
128 |
+
import cv2
|
129 |
+
|
130 |
+
# Global constants
|
131 |
+
DATA_ROOT = os.environ.get('DATA_ROOT', '/tmp/data')
|
132 |
+
MODELS_DIR = os.path.join(DATA_ROOT, "models")
|
133 |
+
|
134 |
+
async def setup():
|
135 |
+
live_portrait = await initialize_models()
|
136 |
+
engine = Engine(live_portrait=live_portrait)
|
137 |
+
|
138 |
+
async def return_video(video):
|
139 |
+
gr.Info("Processing video..")
|
140 |
+
output = engine.process_video(video, {
|
141 |
+
"aaa": -10,
|
142 |
+
"eee": -10,
|
143 |
+
"woo": -12
|
144 |
+
})
|
145 |
+
|
146 |
+
return output_file
|
147 |
+
|
148 |
+
interface = gr.Interface(
|
149 |
+
fn=return_video, # Your function to process video
|
150 |
+
inputs=gr.Video(label="Carregue seu vídeo"),
|
151 |
+
outputs=gr.Video(label="Imagem Processada"),
|
152 |
+
title="Retorno de Imagem",
|
153 |
+
description="📼 Conversor de Vídeo para Imagem"
|
154 |
+
)
|
155 |
|
156 |
interface.launch(share=True)
|
157 |
|