mirror of
https://github.com/facefusion/facefusion.git
synced 2026-07-14 05:57:23 +02:00
process image
This commit is contained in:
@@ -7,6 +7,7 @@ from facefusion.apis.endpoints.assets import delete_assets, get_asset, get_asset
|
||||
from facefusion.apis.endpoints.capabilities import get_capabilities
|
||||
from facefusion.apis.endpoints.metrics import get_metrics, websocket_metrics
|
||||
from facefusion.apis.endpoints.ping import websocket_ping
|
||||
from facefusion.apis.endpoints.process import websocket_process_image
|
||||
from facefusion.apis.endpoints.session import create_session, create_session_guard, destroy_session, get_session, refresh_session
|
||||
from facefusion.apis.endpoints.state import get_state, set_state
|
||||
|
||||
@@ -28,7 +29,8 @@ def create_api() -> Starlette:
|
||||
Route('/capabilities', get_capabilities, methods = [ 'GET' ]),
|
||||
Route('/metrics', get_metrics, methods = [ 'GET' ], middleware = [ session_guard ]),
|
||||
WebSocketRoute('/metrics', websocket_metrics, middleware = [ session_guard ]),
|
||||
WebSocketRoute('/ping', websocket_ping, middleware = [ session_guard ])
|
||||
WebSocketRoute('/ping', websocket_ping, middleware = [ session_guard ]),
|
||||
WebSocketRoute('/process/image', websocket_process_image, middleware = [ session_guard ])
|
||||
]
|
||||
|
||||
api = Starlette(routes = routes)
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
import os
|
||||
import uuid
|
||||
from time import time
|
||||
from typing import Optional
|
||||
|
||||
from starlette.websockets import WebSocket
|
||||
|
||||
from facefusion import ffmpeg, process_manager, session_context, session_manager, state_manager
|
||||
from facefusion.apis.api_helper import get_sec_websocket_protocol
|
||||
from facefusion.apis.endpoints.session import extract_access_token
|
||||
from facefusion.filesystem import create_directory, remove_file
|
||||
from facefusion.workflows import image_to_image
|
||||
|
||||
|
||||
def save_image_bytes(image_bytes : bytes, temp_path : str, file_extension : str) -> str:
|
||||
raw_path = os.path.join(temp_path, str(uuid.uuid4()) + file_extension)
|
||||
|
||||
with open(raw_path, 'wb') as raw_file:
|
||||
raw_file.write(image_bytes)
|
||||
|
||||
return raw_path
|
||||
|
||||
|
||||
def sanitize_upload(raw_path : str, temp_path : str, file_extension : str) -> Optional[str]:
|
||||
target_path = os.path.join(temp_path, str(uuid.uuid4()) + file_extension)
|
||||
|
||||
process_manager.start()
|
||||
sanitized = ffmpeg.sanitize_image(raw_path, target_path)
|
||||
process_manager.end()
|
||||
remove_file(raw_path)
|
||||
|
||||
if sanitized:
|
||||
return target_path
|
||||
return None
|
||||
|
||||
|
||||
def process_target(target_path : str, output_path : str) -> bool:
|
||||
state_manager.set_item('target_path', target_path)
|
||||
state_manager.set_item('output_path', output_path)
|
||||
state_manager.set_item('workflow', 'image-to-image')
|
||||
|
||||
error_code = image_to_image.process(time())
|
||||
remove_file(target_path)
|
||||
|
||||
return error_code == 0
|
||||
|
||||
|
||||
async def websocket_process_image(websocket : WebSocket) -> None:
|
||||
subprotocol = get_sec_websocket_protocol(websocket.scope)
|
||||
access_token = extract_access_token(websocket.scope)
|
||||
session_id = session_manager.find_session_id(access_token)
|
||||
|
||||
session_context.set_session_id(session_id)
|
||||
source_paths = state_manager.get_item('source_paths')
|
||||
|
||||
await websocket.accept(subprotocol = subprotocol)
|
||||
|
||||
if source_paths:
|
||||
temp_path = state_manager.get_temp_path()
|
||||
create_directory(temp_path)
|
||||
|
||||
try:
|
||||
while True:
|
||||
image_bytes = await websocket.receive_bytes()
|
||||
raw_path = save_image_bytes(image_bytes, temp_path, '.jpg')
|
||||
target_path = sanitize_upload(raw_path, temp_path, '.jpg')
|
||||
|
||||
if target_path:
|
||||
output_path = os.path.join(temp_path, str(uuid.uuid4()) + '.jpg')
|
||||
|
||||
if process_target(target_path, output_path):
|
||||
with open(output_path, 'rb') as output_file:
|
||||
await websocket.send_bytes(output_file.read())
|
||||
|
||||
remove_file(output_path)
|
||||
|
||||
except Exception:
|
||||
pass
|
||||
return
|
||||
|
||||
await websocket.close(code = 1008)
|
||||
Reference in New Issue
Block a user