如何在Python脚本(Flask Server)中保存dlib.get_frontal_face_detector()的输出(以在其他框架中重用?)]

问题描述 投票:0回答:1

我是Python新手。我正在使用Flask构建网络应用。

在客户端(JavaScript)上

,我有一个脚本,该脚本使用客户端的网络摄像头“抓取”帧并将其发送到服务器端。 在服务器端(Flask),我有一个Python脚本,该脚本使用OpenCV(读取客户端发送的帧)和DLib检测该帧中的面部,然后输出68个面部界标。然后将这68个面部地标的坐标发送到客户端(即作为响应)。

该应用程序运行,但是,由于我在每个帧中都使用dlib.get_frontal_face_detector()

(检测面部),因此这会增加服务器的响应时间(响应总共需要125毫秒dlib.get_frontal_face_detector()使用了83%。

我正在尝试实施由Davis King(DLib的作者)在GitHub第1556期上提出的解决方案:“每隔几帧就运行一次检测器”。但是,我无法保存dlib.get_frontal_face_detector()的输出。

这是服务器脚本中我认为对该问题至关重要的部分:

@app.route("/prediction", methods=["POST"])
def prediction():
    if request.method == "POST":
      count = session.get("frame_number")
      image = request.data               
      if count%5 == 0: #use DLib's face detector every 5 frames and store the face’s coordinates
        facial_landmarks, face_detection_coordinates = analyse_frame(image) 
        session["face_coordinates"] = face_detection_coordinates #here I try to store face's coordinates using the Flask’s Session Object (which allows to store data across requests)  
      return jsonify(faces=facial_landmarks)

函数analyse_frame()是执行以下操作的自定义函数:

face_detector = dlib.get_frontal_face_detector() #face detection
facial_landmarks = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") #facial landmarks
def analyse_frame(image): 
    img = Image.open(io.BytesIO(image))
    img_cv2 = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2RGBA)
    gray = cv2.cvtColor(img_cv2, cv2.COLOR_RGBA2GRAY)

    face = face_detector(gray, 0) #face detection  (this is the output that I need to store)  

    listx = [] #empty list to store x coordinates (of the facial landmarks)
    listy = [] #empty list to store y coordinates

    for face in face:
        landmarks = facial_landmarks(gray, face) #detect facial landmarks
        for n in range(0, 68): #facial landmarks
            x = landmarks.part(n).x #facial landmarks
            y = landmarks.part(n).y #facial landmarks
            listx.append(x)
            listy.append(y)

    faces = listx +listy
    try:
        return (faces, face) #”faces” represents the coordinates of the 68 facial landmarks; “face” represents the coordinates of the rectangle that encapsulates the detected face and that I need to store to reuse in other frames
    except:
        return []

这是我得到的错误:

Traceback (most recent call last):
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\site-packages\flask\app.py", line 2328, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\site-packages\flask\app.py", line 2314, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\site-packages\flask\app.py", line 1760, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\site-packages\flask\_compat.py", line 36, in reraise
    raise value
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\site-packages\flask\app.py", line 2311, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\site-packages\flask\app.py", line 1835, in full_dispatch_request
    return self.finalize_request(rv)
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\site-packages\flask\app.py", line 1852, in finalize_request
    response = self.process_response(response)
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\site-packages\flask\app.py", line 2133, in process_response
    self.session_interface.save_session(self, ctx.session, response)
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\site-packages\flask\sessions.py", line 375, in save_session
    val = self.get_signing_serializer(app).dumps(dict(session))
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\site-packages\itsdangerous\serializer.py", line 166, in dumps
    payload = want_bytes(self.dump_payload(obj))
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\site-packages\itsdangerous\url_safe.py", line 42, in dump_payload
    json = super(URLSafeSerializerMixin, self).dump_payload(obj)
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\site-packages\itsdangerous\serializer.py", line 133, in dump_payload
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\json\__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\site-packages\flask\json\__init__.py", line 81, in default
    return _json.JSONEncoder.default(self, o)
  File "C:\Users\lbrandao\Anaconda3\envs\pd_facial\lib\json\encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'rectangle' is not JSON serializable

我认为问题与dlib.get_frontal_face_detector()的输出具有类型:class'dlib.rectangles'有关。但是,我无法找到解决方案,并且已经尝试了数周……

有帮助吗?

我是Python新手。我正在使用Flask构建网络应用。在客户端(JavaScript)上,我有一个脚本,该脚本使用客户端的网络摄像头“抓取”帧并将其发送到服务器端。在...

python flask dlib
1个回答
0
投票

这是解决方法*:

© www.soinside.com 2019 - 2024. All rights reserved.