1 OpenCV 读相机
python
import cv2
cap = cv2.VideoCapture(0)
while (1):
# get a frame
ret, frame = cap.read()
# show a frame
cv2.imshow("capture", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
# cv2.imwrite("/opt/code/image/fangjian2.jpeg", frame)
#pass
break
cap.release()
cv2.destroyAllWindows()
2 yolov8推理
python
from ultralytics import YOLO
model =YOLO('yolov8n.pt')
result = model.predict('dog.jpg',imgsz = 640,show = True)
3 yolov8 实时推理相机图片
python
from ultralytics import YOLO
import cv2
def get_img(cap):
while (1):
# get a frame
ret, frame = cap.read()
# show a frame
# cv2.imshow("capture", frame)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# # cv2.imwrite("/opt/code/image/fangjian2.jpeg", frame)
# #pass
# break
return frame
m_cap = cv2.VideoCapture(0)
model =YOLO('yolov8n.pt')
# 输出检测结果和坐标
while True:
img = get_img(m_cap)
cv2.imshow("capture", img)
cv2.waitKey(1)
#results = model.predict(img)
results = model.predict(img)
annotated_frame = results[0].plot()
cv2.imshow("YOLOv8 Tracking", annotated_frame)
cv2.waitKey(1)
4 result
5 PS
总结,在一台老旧的电脑上
跑yolov8 n 感觉速度可以
识别精度也还凑合