Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 每2秒向数据库发送一次信息_Python_Python 3.x_Python 2.7 - Fatal编程技术网

Python 每2秒向数据库发送一次信息

Python 每2秒向数据库发送一次信息,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,我制作了一个程序,Yolov3将信息发送到我的数据库。 但我希望它每2秒发送一次,否则它会很快填满我的数据库。 这是我的密码 def findObjects(outputs, img): hT, wT, cT = img.shape bbox = [] classIds = [] confs = [] for output in outputs: for det in o

我制作了一个程序,Yolov3将信息发送到我的数据库。 但我希望它每2秒发送一次,否则它会很快填满我的数据库。 这是我的密码

           def findObjects(outputs, img):
        hT, wT, cT = img.shape
        bbox = []
        classIds = []
        confs = []
        for output in outputs:
            for det in output:
                scores = det[5:]
                classId = np.argmax(scores)
                confidence = scores[classId]
                if confidence > confThreshold:
                    w, h = int(det[2] * wT), int(det[3] * hT)
                    x, y = int((det[0] * wT) - w / 2), int((det[1] * hT) - h / 2)
                    bbox.append([x, y, w, h])
                    classIds.append(classId)
                    confs.append(float(confidence))

        indices = cv.dnn.NMSBoxes(bbox, confs, confThreshold, nmsThreshold)

        for i in indices:
            i = i[0]
            box = bbox[i]
            x, y, w, h = box[0], box[1], box[2], box[3]
            cv.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
            cv.putText(img, f'{classNames[classIds[i]].upper()} {int(confs[i] * 100)}%',
                       (x, y - 10), cv.FONT_HERSHEY_SIMPLEX, 0.6, (255, 0, 0), 2)

            DetectedName = (f'{classNames[classIds[i]].upper()}')
            DetectedProcent = (f'{int(confs[i] * 100)}%')
            print(DetectedName)
            print(DetectedProcent)
            current_datetime = datetime.now()
            print(current_datetime)
            ***insert = "INSERT INTO DetectedObjects (WayOfDetection, ObjectName, Certainty, TimeOfDetection, LoggedInUser) VALUES (%s,%s,%s,%s,%s)"
            mycursor.execute(insert, ("webcam", DetectedName, DetectedProcent, current_datetime, self.username))
            conn.commit()***
我想每2秒执行最后3行,这样我的数据库就不会过满。 有人能帮我吗?

使用
时间。睡眠(时间以秒为单位)

使用
time.sleep(以秒为单位的时间)


您可以使用
time.sleep()
,例如:

   while True:
        ***insert = "INSERT INTO DetectedObjects (WayOfDetection, ObjectName, Certainty, TimeOfDetection, LoggedInUser) VALUES (%s,%s,%s,%s,%s)"
        mycursor.execute(insert, ("webcam", DetectedName, DetectedProcent, current_datetime, self.username))
        conn.commit()***
        time.sleep(2)

您可以使用
time.sleep()
,例如:

   while True:
        ***insert = "INSERT INTO DetectedObjects (WayOfDetection, ObjectName, Certainty, TimeOfDetection, LoggedInUser) VALUES (%s,%s,%s,%s,%s)"
        mycursor.execute(insert, ("webcam", DetectedName, DetectedProcent, current_datetime, self.username))
        conn.commit()***
        time.sleep(2)

使用
time.sleep
功能。以秒为单位传递所需时间。

使用
时间。睡眠功能。以秒为参数传递所需时间。

当插入的行与表中最后一行之间的间隔小于2秒时,是否可以忽略服务器级别的插入?这可以在服务器级别轻松完成,因此不需要在客户端级别执行任何操作(使用INSERT IGNORE除外)。当插入的行与表中最后一行之间的间隔小于2秒时,可以在服务器级别忽略插入?这可以在服务器级别轻松完成,因此不需要在客户端级别执行某些操作(使用INSERT IGNORE除外)。