face2face icon indicating copy to clipboard operation
face2face copied to clipboard

Bug in video swap

Open dxcanh opened this issue 1 year ago • 3 comments

I got a bug: Traceback (most recent call last): File "", line 198, in _run_module_as_main File "", line 88, in _run_code File "C:\daihoc\Lab\filter\face_swap\ApplySwap.py", line 34, in video_face_swap() File "C:\daihoc\Lab\filter\face_swap\ApplySwap.py", line 27, in video_face_swap swapped = f2f.swap_to_face_in_video(faces="caprio", video=vf) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\daihoc\Lab\filter\face_swap\core\mixins_video_swap.py", line 63, in swap_to_face_in_video new_video = VideoFile().from_video_stream( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\media_toolkit\utils\dependency_requirements.py", line 18, in wrapper return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\media_toolkit\core\video\video_file.py", line 181, in from_video_stream
temp_video_file_path = video_from_image_generator(video_gen_wrapper, frame_rate=frame_rate, save_path=None) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\media_toolkit\utils\dependency_requirements.py", line 18, in wrapper return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\media_toolkit\core\video\video_utils.py", line 138, in video_from_image_generator for i, img in image_generator: File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\tqdm\std.py", line 1181, in iter for obj in iterable: File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\media_toolkit\core\video\video_file.py", line 165, in _frame_gen for frame in video_audio_stream: File "C:\daihoc\Lab\filter\face_swap\core\mixins_image_swap.py", line 177, in swap_to_face_generator swapped = self._swap_faces(source_faces, target_faces, target_image, enhance_face_model) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\daihoc\Lab\filter\face_swap\core\mixins_image_swap.py", line 136, in _swap_faces result = self._face_swapper.get( ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\insightface\model_zoo\inswapper.py", line 50, in get latent = source_face.normed_embedding.reshape((1,-1)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'list' object has no attribute 'normed_embedding'

Can you fix help me :(

dxcanh avatar Sep 11 '24 18:09 dxcanh

@w4hns1nn please help me

dxcanh avatar Sep 11 '24 19:09 dxcanh

Hey dxvan.

looks like there's a face embedding missing in the stored faces. Troubleshoot:

  1. Did you add caprio before to the face embeddings with "f2f.add_face("caprio", "path/to/my_portrait_image.jpg", save=True)"?
  2. Was your face correctly added? Sometimes insightface has problems of detecting faces. You can check if the swap generally works by just swapping one image in the beginning.

Let me know if that solves your issue. Maybe you can attach more of your code? If there's a problem with the video itself consider to send it via wetransfer or on a similar way so that I can look into it.

Nonetheless, I'd like to dive deeper in here, because the error should be catched and a better error message should be presented to the user.

w4hns1nn avatar Sep 13 '24 06:09 w4hns1nn

Dear @w4hns1nn , I have same issues. I attached my code and console messages. When I used KH image file with swap_img_to_img function, it works. Additionally, when I do swap_img_to_img function with KH image file and the specific frame of the video, it works.

The below is my code.

f2f = Face2Face(device_id=0)
embedding = f2f.add_face("KH", r"path_to_source_image.jpg", save=True)
swapped = f2f.swap(media=r"path_to_target_video.mp4", faces="KH")
swapped.save('output.mp4')

I got these kind of messages until the end.

2it [00:00, 3.37it/s]Error in swapping frame 1 to KH: 'list' object has no attribute 'normed_embedding'. Skipping image Error in swapping frame 2 to KH: 'list' object has no attribute 'normed_embedding'. Skipping image 4it [00:01, 3.89it/s]Error in swapping frame 3 to KH: 'list' object has no attribute 'normed_embedding'. Skipping image Error in swapping frame 4 to KH: 'list' object has no attribute 'normed_embedding'. Skipping image 6it [00:01, 3.97it/s]Error in swapping frame 5 to KH: 'list' object has no attribute 'normed_embedding'. Skipping image Error in swapping frame 6 to KH: 'list' object has no attribute 'normed_embedding'. Skipping image Error in swapping frame 7 to KH: 'list' object has no attribute 'normed_embedding'. Skipping image Error in swapping frame 8 to KH: 'list' object has no attribute 'normed_embedding'. Skipping image

Kyeonghyeon-Park avatar Sep 24 '24 05:09 Kyeonghyeon-Park

Hi, according to the example :

output_folder = "output"
    # add ref face
    source_img = cv2.imread("test_media/test_face_1.jpg")
    f2f.add_face("caprio", source_img, save=True)
    # swap it
    fn = "test_media/test_video.mp4"
    try:
        vf = VideoFile().from_file(fn)
        swapped = f2f.swap_to_face_in_video(face_name="caprio", video=vf)
        # swapped = f2f.swap(media=vf, faces="caprio", enhance_face_model=None)
        swapped.save(f"{output_folder}/{os.path.basename(fn)}_swapped.mp4")

    except Exception as e:
        print(e)

but there is an error:

RuntimeWarning: Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work
  warn("Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work", RuntimeWarning)

Mori-hub avatar Feb 16 '25 14:02 Mori-hub

Hi,

ffprobe is part of ffmpeg which is also used by VidGear. The libs are used in the Media toolkit library. I haven't had time to look into it in more detail yet. Can you make sure ffmpeg is installed correctly?

w4hns1nn avatar Feb 16 '25 16:02 w4hns1nn