model_test01.py 4.3 KB

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