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

import os
import time

from ai_tool.plugins import CameraDetectPlugin
from ai_tool.plugins.camera_detect import libs
from .detect_node import DetectNode
from .usb_cam_node import UsbCamNode
from .rtm_pose_node import RTMPostNode

lib_path = os.path.dirname(libs.__file__)

yolov8n_path = os.path.join(lib_path, 'yolov8n.q.onnx')
rtmpose_path = os.path.join(lib_path, 'rtmpose-t.sim.q.onnx')


class CameraPosePlugin(CameraDetectPlugin):

    def __init__(self):
        super().__init__()

    def run_camera_plugin(self):
        detect = DetectNode("dete0", yolov8n_path, None)
        pose = RTMPostNode("pose0", rtmpose_path, self.data_callback)
        self.usb_cam = UsbCamNode("usbCam0", 1, self.data_callback)
        detect.attach_to([self.usb_cam])
        pose.attach_to([detect])
        self.usb_cam.start()
        self.start_time = time.time()
        self.e_Event.wait()
        print(f'开始关闭进程 {os.getpid()}')
        self.usb_cam.stop_cap()
        print('相机关闭成功')
        self.usb_cam.detach_recursively()
        print('进程结束')
