DeepFace analyze function rises serialization issue

Question:

i would like to use deepface algorithm for detection human’s emotion from the image, here is my code :

import  cv2
from deepface import DeepFace
import numpy as np
import json
image =cv2.imread('emotion.jpg')
#json.dumps(image)
analyze =DeepFace.analyze(image,actions='emotions',enforce_detection=False)
print(analyze)

but when i have run the code, got the following error

C:UsersUserPycharmProjectsAI_ProjectvenvScriptspython.exe C:UsersUserPycharmProjectsAI_Projectemotion_detection.py 
Action: e:   0%|          | 0/8 [00:00<?, ?it/s]
Traceback (most recent call last):
  File "C:UsersUserPycharmProjectsAI_Projectemotion_detection.py", line 7, in <module>
    analyze =DeepFace.analyze(image,actions=('emotions'),enforce_detection=False)
  File "C:UsersUserPycharmProjectsAI_Projectvenvlibsite-packagesdeepfaceDeepFace.py", line 452, in analyze
    resp_obj["region"][parameter] = int(region[i]) #int cast is for the exception - object of type 'float32' is not JSON serializable
IndexError: list index out of range

Process finished with exit code 1

i have searched a lot about this problem, for instance try the following code

json.dumps(image)

but when i run, got :

TypeError: Object of type ndarray is not JSON serializable

please tell me is there any solution? or it is just deepface’s bug?

Asked By: Machine_Learning

||

Answers:

you have to pass the image directly. No need to read with opencv & then pass to Deepface.analyze. Read documentaion clearly.

backends = ['opencv', 'ssd', 'dlib', 'mtcnn', 'retinaface', 'mediapipe']
analyze = DeepFace.analyze(img_path = 'emotion.jpg', detector_backend =backends[4])
print(analyze)
Answered By: Bhargav
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.