voiture
voiture

Reputation: 7

Send info every 2 seconds to database

I made a program where Yolov3 send info to my database. But I want it to send it every 2 seconds otherwise it fills up my database too fast. This is my code

           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()***

I want to execute the last 3 lines every 2 seconds so my database is not overfull. can somebody pls help me?

Upvotes: 0

Views: 187

Answers (3)

opolikos
opolikos

Reputation: 23

Use time.sleep function. Pass required time as the parameter in seconds.

Upvotes: 1

you can use time.sleep() such as:

   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)

Upvotes: 1

QWERTYL
QWERTYL

Reputation: 1373

Use time.sleep(time_in_seconds).

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

Upvotes: 0

Related Questions