当前位置: 首页 > article >正文

YOLOV8 OpenCV + usb 相机 实时识别

0、STM32 上位机通信

STM32 上位机通信

1、OpenCV 读相机

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推理

from ultralytics import  YOLO
model =YOLO('yolov8n.pt')

result = model.predict('dog.jpg',imgsz = 640,show = True)

3、 yolov8 实时推理相机图片

 
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 感觉速度可以
在这里插入图片描述
识别精度也还凑合

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 += 1
    frame = 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 = r
            x1 = 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 输出检测框坐标把物体推入到收纳盒

在这里插入图片描述


http://www.kler.cn/a/539494.html

相关文章:

  • 利用NestJS构建高效的RESTful API接口
  • 详解SQLAlchemy的函数relationship
  • 对“云原生”的初印象
  • DeepSeek 评价开源框架存在幻觉么?
  • 电商行业的新篇章:3D和AR技术助力销售转化率提升!
  • Windows安装cwgo,一直安装的是linux平台的
  • JMeter常用函数整理
  • 高并发读多写少场景下的高效键查询与顺序统计的方案思路
  • 【Spring Boot】Spring 事务探秘:核心机制与应用场景解析
  • Android studio怎么创建assets目录
  • 编程语言的深度剖析:从语法到性能优化
  • 【JavaScript】《JavaScript高级程序设计 (第4版) 》笔记-Chapter7-迭代器与生成器
  • 【自动化办公】基于WPF和阿里云API的高效识别PDF多个区域内容并保存至JSON文件,再将JSON文件转换解析为表格输出
  • pytest生成报告no tests ran in 0.01s
  • Java WORD和PDF互相转换以及数据填充示例
  • Windows Docker笔记-Docker容器操作
  • react 17 源码调试环境搭建(超简单)
  • springboot项目的单元测试
  • vue.js v-model实现原理
  • 编译QCefView时出现的setlocal命令错误
  • 【含文档+PPT+源码】基于微信小程序的校园志愿者管理系统的设计与实现
  • Linux之安装docker
  • MySQL 数据库的体系架构
  • 【虚幻引擎UE】UE4.23到UE5.5的核心功能变化
  • 【Linux基础】Linux下常用的系统命令
  • redis缓存应用