add face tools to assets

This commit is contained in:
henryruhs
2026-08-07 22:22:03 +02:00
parent 9612d5391f
commit 37c3548466
+5 -16
View File
@@ -8,8 +8,9 @@ from starlette.datastructures import UploadFile
import facefusion.choices
from facefusion import ffmpeg, process_manager, state_manager
from facefusion.face_creator import get_many_faces
from facefusion.face_helper import warp_face_by_bounding_box
from facefusion.filesystem import create_directory, get_file_extension, get_file_format, is_audio, is_image, is_video
from facefusion.types import Face, ImageAsset, ImageMetadata, MediaType, VideoAsset, VisionFrame
from facefusion.types import ImageAsset, ImageMetadata, MediaType, VideoAsset, VisionFrame
from facefusion.vision import detect_image_resolution, fit_contain_frame, is_vision_frame, read_static_image, read_static_video_frame, unpack_resolution
@@ -123,28 +124,16 @@ def capture_asset_frames(asset : ImageAsset | VideoAsset, frame_numbers : List[s
return capture_vision_frames
def crop_face_frame(vision_frame : VisionFrame, face : Face) -> VisionFrame:
bounding_box = face.bounding_box.astype(int)
start_x = max(0, bounding_box[0])
start_y = max(0, bounding_box[1])
end_x = min(vision_frame.shape[1], bounding_box[2])
end_y = min(vision_frame.shape[0], bounding_box[3])
crop_vision_frame = vision_frame[start_y:end_y, start_x:end_x]
return crop_vision_frame
def capture_asset_faces(asset : ImageAsset | VideoAsset, frame_numbers : List[str], resolution : str) -> List[VisionFrame]:
capture_vision_frames = []
temp_vision_frames = read_asset_frames(asset, frame_numbers)
crop_size = unpack_resolution(resolution)
for temp_vision_frame in temp_vision_frames:
faces = get_many_faces([ temp_vision_frame ])
for face in faces:
crop_vision_frame = crop_face_frame(temp_vision_frame, face)
if crop_vision_frame.shape[0] and crop_vision_frame.shape[1]:
capture_vision_frame = fit_contain_frame(crop_vision_frame, unpack_resolution(resolution))
capture_vision_frames.append(capture_vision_frame)
capture_vision_frame, _ = warp_face_by_bounding_box(temp_vision_frame, face.bounding_box, crop_size)
capture_vision_frames.append(capture_vision_frame)
return capture_vision_frames