utils.py 1.1 KB

1234567891011121314151617181920212223242526
  1. import numpy as np
  2. import cv2
  3. def draw_grid(img: np.ndarray, grid_w: int, grid_h:int):
  4. """划格网"""
  5. img_h, img_w, _ = img.shape
  6. # 绘制横向网格线
  7. for i in range((img_h // grid_h)+1):
  8. cv2.line(img, (0, i*grid_h), (img_w, i*grid_h), (0, 255, 0), 2)
  9. # 绘制纵向网格线
  10. for i in range((img_w // grid_w)+1):
  11. cv2.line(img, (i*grid_w, 0), (i*grid_w, img_h), (0, 255, 0), 2)
  12. return img
  13. def draw_predict_grid(img, patches_index, predicted_class, confidence):
  14. """在img上绘制预测结果"""
  15. for i, (idx_w, idx_h) in enumerate(patches_index):
  16. cv2.circle(img, (idx_w, idx_h), 10, (0, 255, 0), -1)
  17. text1 = f'cls:{predicted_class[i]}'
  18. text2 = f'prob:{confidence[i] * 100:.1f}%'
  19. color = (0, 0, 255) if predicted_class[i] else (255, 0, 0)
  20. cv2.putText(img, text1, (idx_w, idx_h + 128),
  21. cv2.FONT_HERSHEY_SIMPLEX, 1.0, color, 2)
  22. cv2.putText(img, text2, (idx_w, idx_h + 128 + 25),
  23. cv2.FONT_HERSHEY_SIMPLEX, 1.0, color, 2)
  24. return img