Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import sys
|
| 2 |
import asyncio
|
| 3 |
from aiohttp import web, WSMsgType
|
| 4 |
import json
|
|
@@ -12,8 +12,8 @@ from typing import Dict, Any, List, Optional
|
|
| 12 |
import base64
|
| 13 |
import io
|
| 14 |
from PIL import Image
|
| 15 |
-
|
| 16 |
-
import
|
| 17 |
|
| 18 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 19 |
logger = logging.getLogger(__name__)
|
|
@@ -26,108 +26,151 @@ 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 |
|
| 30 |
# Global constants
|
| 31 |
DATA_ROOT = os.environ.get('DATA_ROOT', '/tmp/data')
|
| 32 |
MODELS_DIR = os.path.join(DATA_ROOT, "models")
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
async
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
-
app['engine'] = engine
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
|
| 118 |
|
| 119 |
-
return app
|
| 120 |
-
except Exception as e:
|
| 121 |
-
logger.error(f"🚨 Error during application initialization: {str(e)}")
|
| 122 |
-
logger.exception("Full traceback:")
|
| 123 |
-
raise
|
| 124 |
|
| 125 |
if __name__ == "__main__":
|
| 126 |
-
|
| 127 |
-
logger.info("Starting FacePoke application")
|
| 128 |
-
app = asyncio.run(initialize_app())
|
| 129 |
-
logger.info("Application initialized, starting web server")
|
| 130 |
-
web.run_app(app, host="0.0.0.0", port=8080)
|
| 131 |
-
except Exception as e:
|
| 132 |
-
logger.critical(f"🚨 FATAL: Failed to start the app: {str(e)}")
|
| 133 |
-
logger.exception("Full traceback:")
|
|
|
|
| 1 |
+
'''import sys
|
| 2 |
import asyncio
|
| 3 |
from aiohttp import web, WSMsgType
|
| 4 |
import json
|
|
|
|
| 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__)
|
|
|
|
| 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())'''
|
| 113 |
|
| 114 |
+
import sys
|
| 115 |
+
import asyncio
|
| 116 |
+
from aiohttp import web, WSMsgType
|
| 117 |
+
import json
|
| 118 |
+
from json import JSONEncoder
|
| 119 |
+
import numpy as np
|
| 120 |
+
import uuid
|
| 121 |
+
import logging
|
| 122 |
+
import os
|
| 123 |
+
import signal
|
| 124 |
+
from typing import Dict, Any, List, Optional
|
| 125 |
+
import base64
|
| 126 |
+
import io
|
| 127 |
+
from PIL import Image
|
| 128 |
+
import gradio as gr
|
| 129 |
+
import cv2
|
| 130 |
+
|
| 131 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 132 |
+
logger = logging.getLogger(__name__)
|
| 133 |
+
|
| 134 |
+
def SIGSEGV_signal_arises(signalNum, stack):
|
| 135 |
+
logger.critical(f"{signalNum} : SIGSEGV arises")
|
| 136 |
+
logger.critical(f"Stack trace: {stack}")
|
| 137 |
+
|
| 138 |
+
signal.signal(signal.SIGSEGV, SIGSEGV_signal_arises)
|
| 139 |
+
|
| 140 |
+
from loader import initialize_models
|
| 141 |
+
from engine import Engine, base64_data_uri_to_PIL_Image
|
| 142 |
+
from pathlib import Path
|
| 143 |
+
|
| 144 |
+
import cv2
|
| 145 |
+
|
| 146 |
+
# Global constants
|
| 147 |
+
DATA_ROOT = os.environ.get('DATA_ROOT', '/tmp/data')
|
| 148 |
+
MODELS_DIR = os.path.join(DATA_ROOT, "models")
|
| 149 |
+
|
| 150 |
+
async def setup():
|
| 151 |
+
live_portrait = await initialize_models()
|
| 152 |
+
engine = Engine(live_portrait=live_portrait)
|
| 153 |
|
| 154 |
+
async def return_video(video):
|
| 155 |
+
gr.Info("Processing video..")
|
| 156 |
+
output = engine.process_video(video, {
|
| 157 |
+
"aaa": -10,
|
| 158 |
+
"eee": -10,
|
| 159 |
+
"woo": -12
|
| 160 |
+
})
|
| 161 |
|
| 162 |
+
return output_file
|
|
|
|
| 163 |
|
| 164 |
+
interface = gr.Interface(
|
| 165 |
+
fn=return_video, # Your function to process video
|
| 166 |
+
inputs=gr.Video(label="Carregue seu vídeo"),
|
| 167 |
+
outputs=gr.Video(label="Imagem Processada"),
|
| 168 |
+
title="Retorno de Imagem",
|
| 169 |
+
description="📼 Conversor de Vídeo para Imagem"
|
| 170 |
+
)
|
| 171 |
|
| 172 |
+
interface.launch(share=True)
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
if __name__ == "__main__":
|
| 176 |
+
asyncio.run(setup())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|