当前位置:   article > 正文

yolo8摄像头实时检测(可修改识别边框)_yolov8 调用摄像头 无显示

yolov8 调用摄像头 无显示

提前配置好yolo8的虚拟环境,同时补充安装包    supervision

  1. #开 发 者 :刘 海 超
  2. #开 发 时 间:2023-05-18 14:20
  3. import cv2
  4. import supervision as sv
  5. from ultralytics import YOLO
  6. def main():
  7. # 保存视频
  8. writer = cv2.VideoWriter('webcam_yolo.mp4',
  9. cv2.VideoWriter_fourcc(*'mp4v'),
  10. 7,
  11. (1280, 720))
  12. # define resolution
  13. cap = cv2.VideoCapture(0)
  14. cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
  15. cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
  16. # specify the model
  17. model = YOLO("yolov8m.pt")
  18. # 自定义边框
  19. box_annotator = sv.BoxAnnotator(
  20. thickness=2,
  21. text_thickness=2,
  22. text_scale=1
  23. )
  24. while True:
  25. ret, frame = cap.read()
  26. result = model(frame, agnostic_nms=True)[0]
  27. detections = sv.Detections.from_yolov8(result)
  28. labels = [
  29. f"{model.model.names[class_id]} {confidence:0.2f}"
  30. for _, _, confidence, class_id,_
  31. in detections
  32. ]
  33. frame = box_annotator.annotate(
  34. scene=frame,
  35. detections=detections,
  36. labels=labels
  37. )
  38. writer.write(frame)
  39. cv2.imshow("yolov8", frame)
  40. if (cv2.waitKey(30) == 27): # break with escape key
  41. break
  42. cap.release()
  43. writer.release()
  44. cv2.destroyAllWindows()
  45. if __name__ == "__main__":
  46. main()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/441944
推荐阅读
相关标签
  

闽ICP备14008679号