目录

如何将YOLO-v5识别结果输出到MySQL数据库

如何将YOLO v5识别结果输出到MySQL数据库

https://img-home.csdnimg.cn/images/20240715101418.png

关键词由CSDN通过智能技术生成

嘿嘿嘿,我又来啦~~✿✿ヽ(°▽°)ノ✿ 本期内容就是有关检测结果的输出实践,来吧展示~

步骤:建立数据库、表——修改YOLO v5 detect.py 代码——运行,具体如下:

步骤一:建立数据库、表

https://i-blog.csdnimg.cn/blog_migrate/0a9837907d755dca7b7022ec5fc63dfc.png

构建名为demo的库,在库下面构建detect的表,设置列 picture和time,格式分别是longtext和datetime

步骤二:修改yolo v5 detect.py代码

  • 引入库及sql配置

,点击链接直接跳转至 博主博客

#数据库引用部分
import sys
import time

import pymysql
from dbutils.pooled_db import PooledDB
from pathlib import Path

FILE = Path(__file__).resolve()
ROOT = FILE.parents[0]  # YOLOv5 root directory
if str(ROOT) not in sys.path:
    sys.path.append(str(ROOT))  # add ROOT to PATH

# 数据库配置模块
class Config(object):
    # DEBUG = True
    # SECRET_KEY = "umsuldfsdflskjdf"
    # PERMANENT_SESSION_LIFETIME = timedelta(minutes=20)
    # SESSION_REFRESH_EACH_REQUEST = True
    # SESSION_TYPE = "redis"
    PYMYSQL_POOL  = PooledDB(
        creator=pymysql,  # 使用链接数据库的模块
        maxconnections=6,  # 连接池允许的最大连接数,0和None表示不限制连接数
        mincached=2,  # 初始化时,链接池中至少创建的空闲的链接,0表示不创建
        maxcached=5,  # 链接池中最多闲置的链接,0和None不限制
        maxshared=3,
        # 链接池中最多共享的链接数量,0和None表示全部共享。PS: 无用,因为pymysql和MySQLdb等模块的 threadsafety都为1,所有值无论设置为多少,_maxcached永远为0,所以永远是所有链接都共享。
        blocking=True,  # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
        maxusage=None,  # 一个链接最多被重复使用的次数,None表示无限制
        setsession=[],  # 开始会话前执行的命令列表。
        ping=0,
        # ping MySQL服务端,检查是否服务可用。
        host='localhost',
        port=3306,
        user='root',
        password='******',#你自己的密码
        database='demo',#提前建好库
        charset='utf8'
    )


# 数据库操作类
class SQLHelper(object):
    @staticmethod
    def open(cursor):
        POOL = Config.PYMYSQL_POOL
        conn = POOL.connection()
        cursor = conn.cursor(cursor=cursor)
        return conn, cursor

    @staticmethod
    def close(conn, cursor):
        conn.commit()
        cursor.close()
        conn.close()

    @classmethod
    def fetch_one(cls, sql, args, cursor=pymysql.cursors.DictCursor):
        conn, cursor = cls.open(cursor)
        cursor.execute(sql, args)
        obj = cursor.fetchone()
        cls.close(conn, cursor)
        return obj

    @classmethod
    def fetch_all(cls, sql, args, cursor=pymysql.cursors.DictCursor):
        conn, cursor = cls.open(cursor)
        cursor.execute(sql, args)
        obj = cursor.fetchall()
        cls.close(conn, cursor)
        return obj
  • print部分插入sql输出
#   存入mysql数据库
            if s not in ("0: 480x640", "0: 480x640 "):
                nowTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())  # 当前时间
                obj = SQLHelper.fetch_one(
                    "insert into detect(picture,time) values(%s,%s)",
                    args=(s, nowTime))

在print time这部分模块这里插入,如下

https://i-blog.csdnimg.cn/blog_migrate/fb2f811559b17706179b049707212619.png

如果没有检测结果则不需要输出到数据库,查看过数据库后,发现空检测部分的输出是 “0: 480x640” 和 “0: 480x640 " ,需要剔除

步骤三:运行结果

https://i-blog.csdnimg.cn/blog_migrate/13b379b36340c452cabb73fb1ddf6ef7.png

最后结果如上,实践完结~