Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lack of verbosity during AttributeErrors #1029

Closed
0phoi5 opened this issue Feb 20, 2024 · 4 comments
Closed

Lack of verbosity during AttributeErrors #1029

0phoi5 opened this issue Feb 20, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@0phoi5
Copy link

0phoi5 commented Feb 20, 2024

Hi,
Firstly, thank you for the great work. This code is working excellently for me 99% of the time.
Occasionally, I will come across an image that is unreadable, for whatever reason, and get an AttributeError. In the below example, it's down to "NoneType object has no attribute shape."
This Issue has been raised specifically for verbosity purposes. Could we please have the image name added to the output when an error is thrown? At the moment, I am having to manually try and trace down what image(s) have a problem, when it really aught to be in the output.
Thank you for your time.

Finding representations:  42%|████████████████████████████████▉                                             | 420/993 [12:42<17:20,  1.82s/it]
Traceback (most recent call last):
  File "/home/ophois/.local/bin/deepface", line 8, in <module>
    sys.exit(cli())
  File "/home/ophois/.local/lib/python3.10/site-packages/deepface/DeepFace.py", line 390, in cli
    fire.Fire()
  File "/home/ophois/.local/lib/python3.10/site-packages/fire/core.py", line 141, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
  File "/home/ophois/.local/lib/python3.10/site-packages/fire/core.py", line 475, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
  File "/home/ophois/.local/lib/python3.10/site-packages/fire/core.py", line 691, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
  File "/home/ophois/.local/lib/python3.10/site-packages/deepface/DeepFace.py", line 229, in find
    return recognition.find(
  File "/home/ophois/.local/lib/python3.10/site-packages/deepface/modules/recognition.py", line 167, in find
    representations = __find_bulk_embeddings(
  File "/home/ophois/.local/lib/python3.10/site-packages/deepface/modules/recognition.py", line 326, in __find_bulk_embeddings
    img_objs = functions.extract_faces(
  File "/home/ophois/.local/lib/python3.10/site-packages/deepface/commons/functions.py", line 173, in extract_faces
    img_region = [0, 0, img.shape[1], img.shape[0]]
AttributeError: 'NoneType' object has no attribute 'shape'
@0phoi5
Copy link
Author

0phoi5 commented Feb 20, 2024

The relevant section of code that needs amendment is in ~/.local/lib/python3.10/site-packages/deepface/commons/functions.py;

# img might be path, base64 or numpy array. Convert it to numpy whatever it is.
    img, img_name = load_image(img)
    img_region = [0, 0, img.shape[1], img.shape[0]]

    if detector_backend == "skip":
        face_objs = [(img, img_region, 0)]
    else:
        face_objs = DetectorWrapper.detect_faces(detector_backend, img, align)

@0phoi5
Copy link
Author

0phoi5 commented Feb 20, 2024

Possibly not the perfect solution, but this works well enough.
{img_name} needs to be printed at the point of failure. I used a try/except, one can then see what image failed to read correctly and simply remove it from the input directory or fix the image itself.

    # img might be path, base64 or numpy array. Convert it to numpy whatever it is.
    try:
        img, img_name = load_image(img)
        img_region = [0, 0, img.shape[1], img.shape[0]]

        if detector_backend == "skip":
            face_objs = [(img, img_region, 0)]
        else:
            face_objs = DetectorWrapper.detect_faces(detector_backend, img, align)
    except AttributeError as e:
        raise ValueError(f"Unreadable file - AttributeError occured for {img_name}: {e}")
ValueError: Unreadable file - AttributeError occured for /path/to/dir/failed_image.jpg: 'NoneType' object has no attribute 'shape'

@0phoi5 0phoi5 changed the title Lack of vebosity during AttributeErrors Lack of verbosity during AttributeErrors Feb 20, 2024
@serengil serengil added the bug Something isn't working label Feb 20, 2024
@serengil
Copy link
Owner

Definitely, users should see a meaningful exception message.

Firstly, as I see you are using an old version of DeepFace. Would you please update the package first? This may not be an issue for the current package.

Instead of using try catch, I prefer to raise an error if img is None after load_image performed: https://github.com/serengil/deepface/blob/master/deepface/modules/detection.py#L77

@serengil
Copy link
Owner

Closed with PR - #1038

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants