try: from .leg_baidu import LegAcupointsDetector as BaiduDetector from .leg_yolo import LegAcupointsDetector as YoloDetector from .leg_manual import LegAcupointsDetector as ManualDetector from .config import Config except ImportError: from leg_baidu import LegAcupointsDetector as BaiduDetector from leg_yolo import LegAcupointsDetector as YoloDetector from leg_manual import LegAcupointsDetector as ManualDetector from config import Config import os class LegDetector: """腿部穴位检测器,整合了Baidu自动检测和手动标注两种方法""" def __init__(self): """ 初始化检测器,同时加载Baidu检测器和手动检测器 """ self.baidu = BaiduDetector() self.yolo = YoloDetector() self.manual = ManualDetector() def detect_by_yolo(self, image_path, output_path=None): """ 使用百度API进行自动检测 参数: image_path (str): 输入图片路径 output_path (str, optional): 输出图片路径,如果不指定则自动生成 返回: dict: 包含穴位坐标的字典 {"穴位名称": (x, y), ...} """ if output_path is None: # 从输入图片名生成输出图片名 base_name = os.path.basename(image_path) name, ext = os.path.splitext(base_name) output_path = Config.get_output_path(f"{name}_yolo{ext}") return self.yolo.process_image(image_path, output_path) def detect_by_baidu(self, image_path, output_path=None): """ 使用百度API进行自动检测 参数: image_path (str): 输入图片路径 output_path (str, optional): 输出图片路径,如果不指定则自动生成 返回: dict: 包含穴位坐标的字典 {"穴位名称": (x, y), ...} """ if output_path is None: # 从输入图片名生成输出图片名 base_name = os.path.basename(image_path) name, ext = os.path.splitext(base_name) output_path = Config.get_output_path(f"{name}_baidu{ext}") return self.baidu.process_image(image_path, output_path) def detect_by_manual(self, image_path, json_data, output_path=None): """ 使用手动标注数据进行检测 参数: image_path (str): 输入图片路径 json_data (dict): JSON数据 output_path (str, optional): 输出图片路径,如果不指定则自动生成 返回: dict: 包含穴位坐标的字典 {"穴位名称": (x, y), ...} """ if output_path is None: # 从输入图片名生成输出图片名 base_name = os.path.basename(image_path) name, ext = os.path.splitext(base_name) output_path = Config.get_output_path(f"{name}_manual{ext}") self.manual.load_data(json_data) return self.manual.process_image(image_path, output_path) if __name__ == "__main__": # 示例用法 sample_json = { "shapes": [ { "label": "C1", "points": [ [ 258, 2.0 ], [ 265, 130 ], [ 281, 315 ] ], "curvatures": { "thigh": 10, "calf": -10 }, "shape_type": "linestrip" }, { "label": "C2", "points": [ [ 350, 1.55 ], [ 362, 110 ], [ 401, 302 ] ], "curvatures": { "thigh": -10, "calf": 20 }, "shape_type": "linestrip" } ] } # 创建检测器实例 detector = LegDetector() # 测试图片路径 test_image = Config.get_image_path("leg.png") # yolo检测示例 yolo_results = detector.detect_by_yolo(test_image) print("yolo检测结果:") print(yolo_results) # baidu检测示例 baidu_results = detector.detect_by_baidu(test_image) print("baidu检测结果:") print(baidu_results) # 手动标注检测示例 manual_results = detector.detect_by_manual(test_image, sample_json) print("\n手动标注检测结果:") print(manual_results)