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

fix(represent): the represent is broken when user-given image. Fix it and support pathlib.Path for img_path #917

Merged
merged 2 commits into from
Dec 14, 2023

Conversation

fseasy
Copy link

@fseasy fseasy commented Dec 14, 2023

Background

I want to directly use the represent function on pre-detected face images.

When I just pass the image path in pathlib.Path, it raise error on not-supported type. But I think it should support it because it's popular in Python3. OK, it's a tiny problem, and I just change the image path to str.

After given the image path in str, It still failed. Because

https://github.com/serengil/deepface/blob/master/deepface/DeepFace.py#L706

return a tuple of (img, image_name), but here we assign wrong.

What's more, the region is also wrong type: It should be a dict of x,y,w,h if from extract_faces, but here is a list.

Changes

I fixed them all, in detail:

  1. support represent user given image
  2. img_path support pathlib.Path

And test is added on represent user given image. Docs for reprent on return types is also updated.

Please give me some advice if it's not ok.

# --------------------------------
if len(img.shape) == 4:
img = img[0] # e.g. (1, 224, 224, 3) to (224, 224, 3)
if len(img.shape) == 3:
img = cv2.resize(img, target_size)
img = np.expand_dims(img, axis=0)
# when represent is called from verify, this is already normalized
img = np.expand_dims(img, axis=0) # Why we remove a axis=0 previously and here expand one?
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here need author review. I don't know why should this, so I add a comment without change anything.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

face recognition models expect inputs like (1, 224, 224, 3). so, this is intended behaviour. please remove your comment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but Why we do img = img[0] in 726

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is expanding. if your input is (224, 224, 3) then this makes (1, 224, 224, 3). your command drops a dimension on the other hand.

img = img_path.copy()
else:
raise ValueError(f"unexpected type for img_path - {type(img_path)}")
# Try load. If load error, will raise exception internal
img, _ = functions.load_image(img_path)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because load_image in fact support many type, and have properly raise. so here I just use try-except logic -- just call it. if failed, it will raise, and it should has almost same effects.

if isinstance(img_path, str):
img = functions.load_image(img_path)
elif type(img_path).__module__ == np.__name__:
if type(img_path).__module__ == np.__name__:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we do not need to check type. load_image handles loading different type of inputs. So, we can get rid of the if and else block here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously I find the diffrence between here and load_image for copy. But after carefully look, no copy is ok. I'll do it.

@@ -94,6 +94,14 @@ def load_image(img):
if type(img).__module__ == np.__name__:
return img, None

try:
# Test whether img is a Python3's Path. If hit, tranform to str to let following logic work.
from pathlib import Path
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already imported in line 3: https://github.com/serengil/deepface/blob/master/deepface/commons/functions.py#L3C21-L3C25

This causes linting error in github actions:

************* Module deepface.commons.functions
deepface/commons/functions.py:99:8: W0621: Redefining name 'Path' from outer scope (line 3) (redefined-outer-name)
deepface/commons/functions.py:99:8: W0404: Reimport 'Path' (imported line 3) (reimported)

@serengil
Copy link
Owner

Added some comments.

Also, linting step is failed: https://github.com/serengil/deepface/actions/runs/7205630424/job/19636358763?pr=917

@fseasy
Copy link
Author

fseasy commented Dec 14, 2023

Added some comments.

Also, linting step is failed: https://github.com/serengil/deepface/actions/runs/7205630424/job/19636358763?pr=917

Thanks for quickly response. I'll change it.

@fseasy
Copy link
Author

fseasy commented Dec 14, 2023

updated. please check if you have time.

@serengil serengil merged commit 64e6ace into serengil:master Dec 14, 2023
2 checks passed
@serengil
Copy link
Owner

Thank you for your contribution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants