MCPcopy
hub / github.com/MAA1999/M9A / analyze

Method analyze

agent/custom/reco/general.py:603–656  ·  view source on GitHub ↗
(
        self,
        context: Context,
        argv: CustomRecognition.AnalyzeArg,
    )

Source from the content-addressed store, hash-verified

601 """
602
603 def analyze(
604 self,
605 context: Context,
606 argv: CustomRecognition.AnalyzeArg,
607 ) -> CustomRecognition.AnalyzeResult | RectType | None:
608 try:
609 params = parse_params(argv.custom_recognition_param)
610
611 # 获取参数,默认过滤白色
612 target_color = params.get("target_color", [255, 255, 255])
613 tolerance = params.get("tolerance", 55)
614 recognition_node = params.get("recognition")
615
616 if not target_color or len(target_color) != 3:
617 logger.error(f"无效的target_color参数: {target_color}")
618 return None
619
620 if not recognition_node:
621 logger.error("未提供recognition参数")
622 return None
623
624 # 获取图像
625 img = argv.image
626
627 # 定义目标颜色和颜色容差
628 target_color_array = np.array(target_color)
629
630 # 创建颜色过滤掩码
631 lower_bound = np.maximum(target_color_array - tolerance, 0)
632 upper_bound = np.minimum(target_color_array + tolerance, 255)
633
634 # 创建掩码:保留在目标颜色范围内的像素
635 color_mask = np.all((img >= lower_bound) & (img <= upper_bound), axis=-1)
636
637 # 处理图像:目标颜色变成黑色,其他颜色变成白色
638 # 创建一个全白图像
639 processed_img = np.full_like(img, 255, dtype=np.uint8)
640 # 将匹配目标颜色的像素设置为黑色
641 processed_img[color_mask] = 0
642
643 # 在处理后的图像上运行OCR识别
644 reco_detail = context.run_recognition(recognition_node, processed_img)
645
646 if is_hit(reco_detail):
647 logger.debug(f"ColorOCR识别成功: {ocr_text(reco_detail)}")
648 return CustomRecognition.AnalyzeResult(
649 box=reco_detail.box, detail=reco_detail.raw_detail
650 )
651 else:
652 return None
653
654 except Exception as e:
655 logger.error(f"ColorOCR识别失败: {e}")
656 return None
657
658
659@AgentServer.custom_recognition("ColorOCRWithFallback")

Callers

nothing calls this directly

Calls 3

parse_paramsFunction · 0.90
is_hitFunction · 0.90
ocr_textFunction · 0.90

Tested by

no test coverage detected