test_icon.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """
  2. 象形图识别测试脚本
  3. 用法:python test_icon.py
  4. """
  5. import sys
  6. import os
  7. sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
  8. from PIL import Image
  9. from agent.agent import OcrAgent, image_to_base64, resize_image
  10. import json
  11. # 预期结果
  12. CASES = [
  13. {"file": "pic/A.png", "name": "镀铜药水", "expected": {"GHS05", "GHS09"}},
  14. {"file": "pic/B.png", "name": "液氨", "expected": {"GHS04", "GHS05", "GHS06", "GHS09"}},
  15. {"file": "pic/C.png", "name": "三氯苯", "expected": {"GHS07", "GHS09"}},
  16. ]
  17. agent = OcrAgent()
  18. print("=" * 60)
  19. for case in CASES:
  20. img = Image.open(case["file"]).convert("RGB")
  21. img = resize_image(img, max_size=1600)
  22. b64 = image_to_base64(img, quality=95)
  23. _, result_json = agent.extract_icon(b64)
  24. got = set(json.loads(result_json)["tag_images"])
  25. expected = case["expected"]
  26. hit = got & expected # 正确识别
  27. missed = expected - got # 漏识别
  28. extra = got - expected # 多识别
  29. status = "✓ PASS" if not missed and not extra else "✗ FAIL"
  30. print(f"\n[{status}] {case['name']}")
  31. print(f" 期望: {sorted(expected)}")
  32. print(f" 识别: {sorted(got)}")
  33. if missed: print(f" 漏识别: {sorted(missed)}")
  34. if extra: print(f" 误报: {sorted(extra)}")
  35. print("\n" + "=" * 60)