from core.ocr import load_model as ocrLoadModel, detect as ocrDetect
from core.face import load_model as faceLoadModel, detect as faceDetect
from core.yolo import load_model as yoloLoadModel, detect as yoloDetect

# 添加一个标记变量，用于监控load_model是否已被调用
_model_loaded = False


def load_model():
    global _model_loaded

    # 如果已经调用过，直接忽略
    if _model_loaded:
        return

    # 首次调用时加载模型
    ocrLoadModel()
    faceLoadModel()
    yoloLoadModel()

    # 标记为已调用
    _model_loaded = True


def detect(frame):
    # 先进行YOLO检测
    yolo_flag, yolo_result = yoloDetect(frame)
    print("YOLO检测结果：", yolo_result)
    if yolo_flag:
        return (True, yolo_result, "yolo")

    # YOLO未检测到，进行人脸检测
    face_flag, face_result = faceDetect(frame)
    print("人脸检测结果：", face_result)
    if face_flag:
        return (True, face_result, "face")

    # 人脸未检测到，进行OCR检测
    ocr_flag, ocr_result = ocrDetect(frame)
    print("OCR检测结果：", ocr_result)
    if ocr_flag:
        return (True, ocr_result, "ocr")

    # 所有检测都未检测到
    return (False, "未检测到任何内容", "none")