predict.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # coding: utf8
  2. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # 环境变量配置,用于控制是否使用GPU
  16. # 说明文档:https://paddlex.readthedocs.io/zh_CN/develop/appendix/parameters.html#gpu
  17. import os
  18. os.environ['CUDA_VISIBLE_DEVICES'] = '0'
  19. import os.path as osp
  20. import paddlex as pdx
  21. img_file = 'aluminum_inspection/JPEGImages/budaodian-116.jpg'
  22. model_dir = 'output/faster_rcnn_r50_vd_dcn/best_model/'
  23. save_dir = './visualize/predict'
  24. # 设置置信度阈值
  25. score_threshold = 0.9
  26. if not os.path.exists(save_dir):
  27. os.makedirs(save_dir)
  28. model = pdx.load_model(model_dir)
  29. res = model.predict(img_file)
  30. det_vis = pdx.det.visualize(
  31. img_file, res, threshold=score_threshold, save_dir=save_dir)