model_test01.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import os
  2. from pathlib import Path
  3. from app.core.config import settings
  4. import json
  5. import cv2
  6. from app.utils.card_inference.fry_bisenetv2_predictor_V04_250819 import FryBisenetV2Predictor
  7. BASE_PATH = Path(__file__).parent.parent.absolute()
  8. def predict_single_image(config_params: dict,
  9. img_path: str,
  10. output_dir: str,
  11. only_json=False):
  12. # 配置参数
  13. model_path = BASE_PATH / config_params["pth_path"]
  14. real_seg_class_dict = config_params['class_dict']
  15. imgSize_train_dict = config_params['img_size']
  16. confidence = config_params['confidence']
  17. input_channels = config_params['input_channels']
  18. # 为不同类别设置不同颜色(可选)
  19. label_colors_dict = {
  20. 'outer_box': (255, 0, 0),
  21. }
  22. # 创建预测器
  23. predictor = FryBisenetV2Predictor(
  24. pth_path=str(model_path),
  25. real_seg_class_dict=real_seg_class_dict,
  26. imgSize_train_dict=imgSize_train_dict,
  27. confidence=confidence,
  28. label_colors_dict=label_colors_dict,
  29. input_channels=input_channels,
  30. )
  31. # 单张图片预测
  32. print("=== 单张图片预测 ===")
  33. now_img_path = img_path
  34. answer_json_dir_str = output_dir
  35. if not only_json:
  36. result = predictor.predict_single_image(
  37. img_path=now_img_path,
  38. save_visualization=True,
  39. save_json=True,
  40. answer_json_dir_str=answer_json_dir_str
  41. )
  42. else:
  43. img_bgr = cv2.imread(now_img_path)
  44. result = predictor.predict_from_image(img_bgr)
  45. return result
  46. if __name__ == '__main__':
  47. big_img_path = r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\250805_pokemon_0001.jpg"
  48. config = settings.CARD_MODELS_CONFIG
  49. # predict_single_image(config['pokemon_front_inner_box'],
  50. # img_path=big_img_path,
  51. # output_dir=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\inner")
  52. #
  53. # predict_single_image(config['pokemon_back_inner_box'],
  54. # img_path=r"C:\Users\wow38\Downloads\_250829_1656_最新模型汇总\_250829_1814_宝可梦背面内框模型\pth_and_images\images\Pokémon_back_0805_0001.jpg",
  55. # output_dir=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\back_inner")
  56. #
  57. #
  58. # predict_single_image(config['outer_box'],
  59. # big_img_path,
  60. # output_dir=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\outer")
  61. # predict_single_image(config['pokemon_back_corner_defect'],
  62. # img_path=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\img_2.png",
  63. # output_dir=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\back_corner")
  64. # predict_single_image(config['pokemon_front_face_no_reflect_defect'],
  65. # img_path=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\250805_pokemon_0001_grid_r3_c4.jpg",
  66. # output_dir=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\face_no_reflect")
  67. result = predict_single_image(config['pokemon_front_corner_no_reflect_defect'],
  68. img_path=r"C:\Users\wow38\Downloads\_250829_1656_最新模型汇总\_250829_1817_宝可梦非闪光卡正面边角模型\pth_and_images\images\250730_pokemon_0031_bottom_grid_r0_c5.jpg",
  69. output_dir=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\corner_no_reflect")
  70. # result = predict_single_image(config['pokemon_front_face_no_reflect_defect'],
  71. # img_path=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\250805_pokemon_0001_grid_r3_c4.jpg",
  72. # output_dir=r"C:\Code\ML\Project\CheckCardBoxAndDefectServer\temp\face_no_reflect",
  73. # only_json=True)
  74. # print(result)