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



我制作了一个程序,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(),例如:

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函数。以秒为单位将所需时间作为参数传递。

使用time.sleep(time_in_seconds)

while True:
//run last 3 lines
time.sleep(2)

最新更新