我想将 OpenCV 与 YOLOv8 集成ultralytics
,所以我想从模型预测中获取边界框坐标。我该怎么做呢?
from ultralytics import YOLO
import cv2
model = YOLO('yolov8n.pt')
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
while True:
_, frame = cap.read()
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = model.predict(img)
for r in results:
for c in r.boxes.cls:
print(model.names[int(c)])
cv2.imshow('YOLO V8 Detection', frame)
if cv2.waitKey(1) & 0xFF == ord(' '):
break
cap.release()
cv2.destroyAllWindows()
我想在 OpenCV 中显示 YOLO 带注释的图像。我知道我可以使用流参数model.predict(source='0', show=True)
。但我想连续监视我的程序的预测类名称,同时显示图像输出。
这将循环播放视频中的每一帧,使用内置的 ultralytics 注释器绘制相应的框:
from ultralytics import YOLO
import cv2
from ultralytics.utils.plotting import Annotator # ultralytics.yolo.utils.plotting is deprecated
model = YOLO('yolov8n.pt')
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
while True:
_, img = cap.read()
# BGR to RGB conversion is performed under the hood
# see: https://github.com/ultralytics/ultralytics/issues/2575
results = model.predict(img)
for r in results:
annotator = Annotator(img)
boxes = r.boxes
for box in boxes:
b = box.xyxy[0] # get box coordinates in (top, left, bottom, right) format
c = box.cls
annotator.box_label(b, model.names[int(c)])
img = annotator.result()
cv2.imshow('YOLO V8 Detection', img)
if cv2.waitKey(1) & 0xFF == ord(' '):
break
cap.release()
cv2.destroyAllWindows()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)