#!/usr/bin/env python
# encoding: utf-8
"""
@author: simin. Gong
@license: (C) Copyright 2013-2023 Spacemit Corporation Limited.
@contact: simin.gong@spacemit.com
@file: face_detect_node.py
@date: 2024/12/18 19:52 
@desc:
"""

from infer_module import yoloFace as yoloface
from infer_module.yoloFace import FaceBoxs
import vp_module as vp


class FaceDetectNode(vp.vp_node):
    def __init__(self, node_name, model_path, callback=None, mode=vp.QueueMode.DropOldest):
        super().__init__(node_name, mode)
        self.initialized()
        self.callback = callback
        self.engin = yoloface.create_infer(model_path)

    def __del__(self):
        self.gate_open()
        self.deinitialized()
        # self.engin.reset()

    def handle_frame_meta(self, meta):
        meta.any_face = self.engin.commit(meta.frame)
        faces = meta.any_face.get()
        json = FaceBoxs(faces).to_json()
        if self.callback:
            self.callback(None, json)
        return super().handle_frame_meta(meta)