tensorflow利用预训练模型进行目标检测(二):将检测结果存入mysql数据库

mysql版本:5.7 ; 数据库:rdshare;表captain_america3_sd用来记录某帧是否被检测。表captain_america3_d用来记录检测到的数据。

python模块,包部分内容参考http://www.runoob.com/python/python-modules.html  https://www.cnblogs.com/ningskyer/articles/6025964.html

一、连接数据库

参考:

# 将视频插入数据库
def video_insert(filename,couse_id):
    conn =MySQLdb.connect(user='root',passwd='****',host='sh-cdb-myegtz7i.sql.tencentcdb.com',port=63619,db='bitbear',charset='utf8')
    cursor = conn.cursor()

    # 查找课程报告表中courseh_id等于解析得到的course_id的记录,得到courser_id
    # courseh_id是课程记录表中的course_id;courser_id是课程报告表中的主键;course_id是本程序中
    sql="SELECT courser_id FROM course_report WHERE courseh_id ='%s' "% (couse_id);
    cursor.execute(sql)
    results = cursor.fetchall()
    if(results):
        print(results)
        courser_id=results[0][0]
        print(results[0][0])

        # 获取该文件的路径
        #rarpath = os.getcwd();
        rarpath =filename
        print(rarpath)

        # 将记录插入
        #try:
        sql="UPDATE course_report SET json = '%s' WHERE courser_id = '%s' " % (rarpath,courser_id)
        cursor.execute(sql)
        cursor.rowcount
        conn.commit()
        cursor.close()
View Code

首先需要安装mysql驱动  sudo apt-get install python-mysqldb 

安装完成之后可以在Python解释器中测试一下
输入 import MySQLdb #注意大小写
如果不报错,就证明安装成功了。
简单测试版本
# 将detection的结果存入mysql数据库
def detection_to_database(object_name):
    conn =MySQLdb.connect(user='root',passwd='****',host='localhost',port=3306,db='rdshare',charset='utf8')
    cursor = conn.cursor()


    #sql="SELECT person FROM captain_america3_d WHERE id =1 ";
    #cursor.execute(sql)
    #results = cursor.fetchall()
    #if(results):
    #    print(results)

    sql="INSERT INTO captain_america3_sd (is_detected) VALUES (1)"
    cursor.execute(sql)
    cursor.rowcount
    conn.commit()
    cursor.close()
View Code

猜你喜欢

转载自www.cnblogs.com/vactor/p/10031414.html