python PeeWee object save function for sqlite 的延迟意外变得太慢

问题描述 投票:0回答:0

我正在编写一个程序来记录opencv读取的帧的gps信息。我用 PeeWee 做 ORM。通常数据库插入操作花费 < 8ms, but sometimes it become > 100ms。 我不知道为什么会这样,所以我需要一些帮助来解决它。

 
import json
import cv2
from datetime import datetime,timedelta
import time
import sqlite3
import pynmea2
import uuid
from peewee import *
from loguru import logger
import os


db_another = SqliteDatabase('robu_another3.db',pragmas={"journal_mode": "wal","cache_size":-1024*64,"page_size":32768,"synchronous":"normal","temp_store":"memory"}, timeout=40)

class LandscapeFrameTest(Model):
    timestamp = DateTimeField(primary_key=True,verbose_name='时间戳', null=False)
    boot_start_time= DateTimeField(verbose_name='所属服务时间戳', null=False)
    image_frame_name = CharField(verbose_name='图片名称', null=False)
    infer_timestamp = DateTimeField(verbose_name='时间戳', null=False)
    gps_latitude = FloatField(verbose_name='纬度', null=False, default=0.0)
    gps_longitude = FloatField(verbose_name='经度', null=False, default=0.0)
    gps_speed= FloatField(verbose_name='每秒速度', null=False, default=0.0)
    gps_quality= FloatField(verbose_name='GPS信号质量',null=False,default=0.0)
    gps_sn= IntegerField(verbose_name='GPS信号卫星数量',null=False,default=0)
    z_acclspeed = FloatField(verbose_name='Z轴加速度', null=False, default=0.0)
    local_image_dir = CharField(verbose_name='图片本地存储目录', null=False, default="")
    remote_image_dir = CharField(verbose_name='图片远端存储目录', null=False, default="")
    object_storage_url_prefix =  CharField(verbose_name='图片远端存储URL', null=False, default="")
    original_image_path = CharField(verbose_name='原图片远端存储路径', null=False, default="")
    result_image_path = CharField(verbose_name='推理结果图片远端存储路径', null=False, default="")
    has_sent= BooleanField(verbose_name='是否成功发送',null=False,default=False)
    frame_file_has_sent= BooleanField(verbose_name='文件是否已成功发送',null=False,default=False)
    frame_meta_has_sent= BooleanField(verbose_name='元数据是否成功发送',null=False,default=False)
    try_count= IntegerField(verbose_name='重试次数',null=False,default=0)
    try_file_count= IntegerField(verbose_name='文件发送重试次数',null=False,default=0)
    try_meta_count= IntegerField(verbose_name='元数据发送重试次数',null=False,default=0)
    damage_count= IntegerField(verbose_name='病害数量',null=False,default=0)
    record_id= CharField(verbose_name='唯一编号', null=False)
    pic_high_quality = IntegerField(verbose_name='图片质量模式',null=False,default=False)
    frame_type= IntegerField(verbose_name='图片类型',null=False,default=1)
    class Meta:
        table_name = 'frame_records_test'
        database = db_another
        order_by = ('timestamp',)


happen = 0
happen_10 = 0
happen_20 = 0
happen_100 = 0
all_time = 0

cap = cv2.VideoCapture("/dev/video2", cv2.CAP_V4L)
image_width = 1920
image_height = 1080
cap.set(cv2.CAP_PROP_FRAME_WIDTH, image_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, image_height)
cap.set(cv2.CAP_PROP_FPS, 10)           #帧数
print('Opened: ',cap.isOpened())

db_another.drop_tables([LandscapeFrameTest])
db_another.create_tables([LandscapeFrameTest])

while True:
    ret, frame_read = cap.read()
    print(frame_read.shape)
    time_now = datetime.now()
    timestr_now = time_now.strftime('%Y-%m-%d %H:%M:%S.%f')
    timestr_now_str = time_now.strftime('%Y-%m-%d %H:%M:%S.%f')
    print(timestr_now_str)
    frame_object = {
                 "device_id":"48:b0:2d:95:60:96",
                 "aibox_id":"48:b0:2d:95:60:96",
                 "image_frame_name":"test",
                 "photo_time":timestr_now_str,
                 "infer_timestamp":"",
                 "boot_start_time":timestr_now_str,
                 "timestamp_timestr":timestr_now,
                 "boot_start_time":timestr_now_str,
                 "local_image_dir":"test",
                 "remote_image_dir":"test",
                 "original_image_path":"test",
                 "result_image_path":"test",
                 "object_storage_url_prefix":"test",
                 "gps_longitude":0.1,
                 "gps_latitude":5.5,
                 "gps_longitude_trans":0.55,
                 "gps_latitude_trans":3.5,
                 "gps_speed":1,
                 "gps_quality":2,
                 "gps_sn":2,
                 "z_acclspeed":1,
                 "landscape_jpeg":"dadad",
                 "try_count":0,
                 "try_file_count":0,
                 "try_meta_count":0,
                 "has_sent":False,
                 "frame_file_has_sent":False,
                 "frame_meta_has_sent":True,
                 "damage_count":0,
                 "damage_list":[],
                 "infer_cost":120,
                 "image_frame":10000000000,
                 "frame_type":1,
                 "pic_high_quality":2
                 }
    damage_list_len = 0
    start_p = time.time()
    f = LandscapeFrameTest(boot_start_time=frame_object['boot_start_time'],image_frame_name = frame_object['image_frame_name'],
            timestamp = frame_object['photo_time'],infer_timestamp = frame_object['infer_timestamp'],
            gps_latitude=frame_object['gps_latitude'],gps_longitude=frame_object['gps_longitude'],
            gps_speed=frame_object['gps_speed'],z_acclspeed=frame_object['z_acclspeed'],
            local_image_dir=frame_object['local_image_dir'],remote_image_dir=frame_object['remote_image_dir'],
            object_storage_url_prefix = frame_object['object_storage_url_prefix'],
            original_image_path=frame_object['original_image_path'],result_image_path=frame_object['result_image_path'],
            try_count=frame_object['try_count'],try_file_count=frame_object['try_file_count'],try_meta_count=frame_object['try_meta_count'],
            has_sent=frame_object['has_sent'],frame_file_has_sent=frame_object['frame_file_has_sent'],frame_meta_has_sent=frame_object['frame_meta_has_sent'],
            damage_count=frame_object['damage_count'],record_id =frame_object['image_frame'],
            pic_high_quality=frame_object['pic_high_quality'],gps_quality=frame_object['gps_quality'],gps_sn=frame_object['gps_sn'])
    f.save(force_insert=True)
    end_p = time.time()
    framedb_cost = (end_p-start_p)*1000
    all_time = all_time+1
    logger.debug('Cost {} For {} ,Happen 100 {},Happen 20 {},Happen 10 {},After {}.'.format(framedb_cost,frame_object['photo_time'],happen_100,happen_20,happen_10,all_time))
    if framedb_cost > 100:
        happen_100 = happen_100 +1
        break
    elif framedb_cost > 20:
        happen_20 = happen_20 +1
    elif framedb_cost > 10:
        happen_10 = happen_10 +1
    logger.debug(db_another.get_primary_keys('frame_records_test'))
    frame_nums = LandscapeFrameTest.select().count()
    logger.debug("Handle Frame Database  Frame All Count  {}".format(frame_nums))

我在 Nvidia AGX Orin 中运行编程器。

python performance sqlite nvidia peewee
© www.soinside.com 2019 - 2024. All rights reserved.