liusida / ComfyUI-AutoCropFaces

Use RetinaFace to detect and automatically crop faces
MIT License
54 stars 10 forks source link

Potentially fixed scale exception. #8

Closed nomcycle closed 3 months ago

nomcycle commented 3 months ago

Thanks for the additional improvements, though after testing seems like I"m encountering some exceptions:

Error occurred when executing AutoCropFaces:

Sizes of tensors must match except in dimension 0. Expected size 512 but got size 511 for tensor number 1 in the list.

File "/home/sea/ComfyUI/execution.py", line 151, in recursive_execute
output_data, output_ui = get_output_data(obj, input_data_all)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/sea/ComfyUI/execution.py", line 81, in get_output_data
return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/sea/ComfyUI/execution.py", line 74, in map_node_over_list
results.append(getattr(obj, func)(**slice_dict(input_data_all, i)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/sea/ComfyUI/custom_nodes/ComfyUI-AutoCropFaces/__init__.py", line 155, in auto_crop_faces
out = torch.cat((out, face_image), dim=0)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is because widths and heights were swapped here which I fixed:

        # Determine the index of the face with the maximum width
        max_width_index = max(range(len(selected_faces)), key=lambda i: selected_faces[i].shape[2])

        # Determine the maximum width
        max_width = selected_faces[max_width_index].shape[2]
        max_height = selected_faces[max_width_index].shape[1]
        shape = (max_height, max_width)

It's only for comfy.utils.common_upscale that has it's widths/heights swapped.

Also, regarding this line:

            if shape != image.shape[1:3]: # Check all images against the largest image and scale it to that size.

Shouldn't this be this or am I misunderstanding:

            if shape != face_image.shape[1:3]: # Determine whether cropped face image size matches largest cropped face image. 
liusida commented 3 months ago

Yeah, thanks! I didn't test for different ratios, so the mistake in determining width and height~

liusida commented 3 months ago

And I've just realized that I've also made a mistake when using aspect_ratio, since aspect_ratio should be width/height, but I thought it was height/width. Just fixed that.