test_supervision.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import supervision as sv
  2. import cv2
  3. import os
  4. from inference import get_model
  5. from ultralytics import YOLO
  6. print(sv.__version__)
  7. HOME = os.getcwd()
  8. # print(HOME)
  9. IMAGE_PATH = f"{HOME}//images//dog-3.jpeg"
  10. image = cv2.imread(IMAGE_PATH)
  11. # # 修正 cv2.imshow() 函数调用,添加窗口名称
  12. # cv2.imshow('Display Image', image)
  13. # # 等待按键事件,防止窗口立即关闭
  14. # cv2.waitKey(0)
  15. # # 关闭所有 OpenCV 窗口
  16. # cv2.destroyAllWindows()
  17. # model = get_model(model_id="yolov8s-640")
  18. # result = model.infer(image)[0]
  19. # detections = sv.Detections.from_inference(result)
  20. model = YOLO("yolov8s.pt")
  21. result = model(image, verbose=False)[0]
  22. detections = sv.Detections.from_ultralytics(result)
  23. # print(detections)
  24. box_annotator = sv.BoxAnnotator()
  25. label_annotator = sv.LabelAnnotator()
  26. annotated_image = image.copy()
  27. annotated_image = box_annotator.annotate(annotated_image, detections=detections)
  28. annotated_image = label_annotator.annotate(annotated_image, detections=detections)
  29. sv.plot_image(image=annotated_image, size=(8, 8))
  30. print('end of file')