serengil / deepface

A Lightweight Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion and Race) Library for Python
https://www.youtube.com/watch?v=WnUVYQP4h44&list=PLsS_1RYmYQQFdWqxQggXHynP1rqaYXv_E&index=1
MIT License
14.34k stars 2.2k forks source link

Analyze with numpy input. #380

Closed PankajSavaliya closed 2 years ago

PankajSavaliya commented 2 years ago
img = BytesIO(requests.get(image.url).content)
frame_byte = bytearray(urllib.request.urlopen(data.image.url).read())
np_array = numpy.asarray(frame_byte, dtype=numpy.uint8)
frame = cv2.imdecode(np_array,
                     cv2.IMREAD_UNCHANGED)  # 'Load it as it is'

print(np_array, type(np_array))  # type is numpy.ndarray
print(frame, type(frame))  # type is numpy.ndarray

obj = DeepFace.analyze(img_path=np_array, actions=['age', 'gender', ],
                       enforce_detection=False)

both output types are numpy.ndarray and np_array getting an error and the second one getting the right result, can you please explain why the first one is not working even it's the right type for method?.

FranSPG commented 2 years ago

If i reproduced exactly the error you got, I think that the problem here is even though they are both NumPy ndarray, their shapes are not equals, you can see this with np_array.shape and frame.shape.

Screen Shot 2021-12-07 at 10 44 14

You need to process the binary as an image and that's the job that you're doing in the line

frame = cv2.imdecode(np_array, cv2.IMREAD_UNCHANGED) # 'Load it as it is'.

If this doesn't solve your error, please provide more information about the error and maybe I could help you.