model_test01.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import os
  2. from pathlib import Path
  3. import json
  4. import cv2
  5. from app.utils.card_inference.fry_bisenetv2_predictor_V04_250819 import FryBisenetV2Predictor
  6. BASE_PATH = Path(__file__).parent.parent.absolute()
  7. def predict_single_image(config_params: dict,
  8. img_path: str,
  9. output_dir: str,
  10. only_json=False):
  11. # 配置参数
  12. model_path = BASE_PATH / config_params["pth_path"]
  13. real_seg_class_dict = config_params['class_dict']
  14. imgSize_train_dict = config_params['img_size']
  15. confidence = config_params['confidence']
  16. input_channels = config_params['input_channels']
  17. # 为不同类别设置不同颜色(可选)
  18. label_colors_dict = {
  19. 'outer_box': (255, 0, 0),
  20. }
  21. # 创建预测器
  22. predictor = FryBisenetV2Predictor(
  23. pth_path=str(model_path),
  24. real_seg_class_dict=real_seg_class_dict,
  25. imgSize_train_dict=imgSize_train_dict,
  26. confidence=confidence,
  27. label_colors_dict=label_colors_dict,
  28. input_channels=input_channels,
  29. )
  30. # 单张图片预测
  31. print("=== 单张图片预测 ===")
  32. now_img_path = img_path
  33. answer_json_dir_str = output_dir
  34. if not only_json:
  35. result = predictor.predict_single_image(
  36. img_path=now_img_path,
  37. save_visualization=True,
  38. save_json=True,
  39. answer_json_dir_str=answer_json_dir_str
  40. )
  41. else:
  42. img_bgr = cv2.imread(now_img_path)
  43. result = predictor.predict_from_image(img_bgr)
  44. return result
  45. if __name__ == '__main__':
  46. config = {
  47. "pokemon_outer_box": {
  48. "pth_path": "Model/pokemon_outer_box.pth",
  49. "class_dict": {1: 'outer_box'},
  50. "img_size": {'width': 1280, 'height': 1280},
  51. "confidence": 0.5,
  52. "input_channels": 3,
  53. },
  54. "pokemon_inner_box": {
  55. "pth_path": "Model/pokemon_inner_box.pth",
  56. "class_dict": {1: 'inner_box'},
  57. "img_size": {'width': 1280, 'height': 1280},
  58. "confidence": 0.5,
  59. "input_channels": 3,
  60. },
  61. "pokemon_back_corner_defect": {
  62. "pth_path": "Model/pokemon_back_corner_defect.pth",
  63. "class_dict": {
  64. 1: 'wear', 2: 'wear_and_impact', 3: 'impact',
  65. 4: 'damaged', 5: 'wear_and_stain',
  66. },
  67. "img_size": {'width': 512, 'height': 512},
  68. "confidence": 0.5,
  69. "input_channels": 3,
  70. },
  71. "pokemon_front_corner_reflect_defect": {
  72. "pth_path": "Model/pokemon_front_corner_reflect_defect.pth",
  73. "class_dict": {"1": "impact", "2": "wear_and_impact", "3": "wear"},
  74. "img_size": {'width': 512, 'height': 512},
  75. "confidence": 0.5,
  76. "input_channels": 3,
  77. },
  78. "pokemon_front_corner_no_reflect_defect": {
  79. "pth_path": "Model/pokemon_front_corner_no_reflect_defect.pth",
  80. "class_dict": {"1": "wear", "2": "wear_and_impact", "3": "impact", "4": "damaged"},
  81. "img_size": {'width': 512, 'height': 512},
  82. "confidence": 0.5,
  83. "input_channels": 3,
  84. },
  85. "pokemon_front_face_no_reflect_defect": {
  86. "pth_path": "Model/pokemon_front_face_no_reflect_defect.pth",
  87. "class_dict": {"1": "scratch", "2": "pit", "3": "stain"},
  88. "img_size": {'width': 512, 'height': 512},
  89. "confidence": 0.5,
  90. "input_channels": 3,
  91. },
  92. }
  93. predict_single_image(config['pokemon_inner_box'],
  94. img_path=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\250805_pokemon_0001.jpg",
  95. output_dir=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\inner")
  96. predict_single_image(config['pokemon_outer_box'],
  97. img_path=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\250805_pokemon_0001.jpg",
  98. output_dir=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\outer")
  99. # predict_single_image(config['pokemon_back_corner_defect'],
  100. # img_path=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\img_2.png",
  101. # output_dir=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\back_corner")
  102. predict_single_image(config['pokemon_front_face_no_reflect_defect'],
  103. img_path=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\250805_pokemon_0001.jpg",
  104. output_dir=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\face_no_reflect")
  105. # result = predict_single_image(config['pokemon_front_corner_no_reflect_defect'],
  106. # img_path=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\00006_250805_pokemon_0001_bottom_grid_r0_c5.jpg",
  107. # output_dir=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\corner_no_reflect")
  108. # result = predict_single_image(config['pokemon_front_face_no_reflect_defect'],
  109. # img_path=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\250805_pokemon_0001_grid_r3_c4.jpg",
  110. # output_dir=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\face_no_reflect",
  111. # only_json=True)
  112. # print(result)