欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > YOLOV8 OpenCV + usb 相机 实时识别

YOLOV8 OpenCV + usb 相机 实时识别

2025/2/10 7:08:21 来源:https://blog.csdn.net/qq_36784503/article/details/143724986  浏览:    关键词:YOLOV8 OpenCV + usb 相机 实时识别

0、STM32 上位机通信

STM32 上位机通信

1、OpenCV 读相机

import cv2cap = cv2.VideoCapture(0)
while (1):# get a frameret, frame = cap.read()# show a framecv2.imshow("capture", frame)if cv2.waitKey(1) & 0xFF == ord('q'):# cv2.imwrite("/opt/code/image/fangjian2.jpeg", frame)#passbreak
cap.release()
cv2.destroyAllWindows()

在这里插入图片描述

2、yolov8推理

from ultralytics import  YOLO
model =YOLO('yolov8n.pt')result = model.predict('dog.jpg',imgsz = 640,show = True)

3、 yolov8 实时推理相机图片

 
from ultralytics import  YOLOimport cv2def get_img(cap):while (1):# get a frameret, 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#    breakreturn 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 感觉速度可以
在这里插入图片描述
识别精度也还凑合

6、 yolo v8 输出检测框坐标

import cv2
import os
from ultralytics import YOLO
import random
# 加载YOLOv8模型
#model = YOLO('yolov8n.pt')model = YOLO('../runs/detect/train6/weights/last.pt')# 打开视频文件
##--
frame = cv2.imread("I:/motor/fruit/" + str(7888) + '.jpg')
cv2.imshow('22', frame)
cv2.waitKey(0)
video_path = os.path.join('.', 'data', 'people.mp4')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
cap_out = cv2.VideoWriter('testwrite.avi', fourcc, 10.0, (1920, 1080), True)
dic = {0: 'Pome', 1: 'Pear'}
colors = [ (255, 50, 255),(255, 0, 0)]
detection_threshold = 0.5
i = 7888
while i < 8050:i += 1frame = cv2.imread("I:/motor/fruit/" + str(i) + '.jpg')results = model(frame)for result in results:detections = []for r in result.boxes.data.tolist():x1, y1, x2, y2, score, class_id = rx1 = int(x1)x2 = int(x2)y1 = int(y1)y2 = int(y2)class_id = int(class_id)cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (colors[class_id % len(colors)]), 3)cv2.putText(frame, dic[class_id], (x1-10,y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (colors[class_id % len(colors)]), 2)cv2.putText(frame, '('+str(x1)+','+str(y1)+')', (x1+100, y1+100), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(colors[class_id % len(colors)]), 2)cv2.waitKey(2)cv2.imshow('0', frame)cap_out.write(frame)if score > detection_threshold:detections.append([x1, y1, x2, y2, score])cap_out.write(frame)cap_out.release()
cv2.destroyAllWindows()

7、 利用yolo v8 输出检测框坐标把物体推入到收纳盒

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com