python 时间格式在 while True 循环中冻结

python time format freezed in a while True loop

我想写信给 BOOTEX.log:在 {day}/{month}/{year} {hour}:{minute}:{nsecond}.{microsecond}

上看到 {n faces} 个面孔
import cv2
import random
import datetime
now = datetime.datetime.now()


trained_face_data = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

webcam = cv2.VideoCapture(0)


import os

if os.path.isfile('./BOOT.log') == False:
   open("boot.log", "w+")
else:
   None

l = 0
def is_list_empty(list):
    # checking the length
        if len(list) == 0:
        # returning true as length is 0
            return True
    # returning false as length is greater than 0
        return False

while True:
    l = 0
    succesfull_frame_read, frame = webcam.read()

    grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    face_coordinates = trained_face_data.detectMultiScale(grey)



    for (x, y, w, h) in face_coordinates:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (random.randrange(226),random.randrange(226),random.randrange(226)), 2)
        l = l + 1
    
    lel = is_list_empty(face_coordinates)
    if lel == True:
        None
    else:
        f = open("BOOTEX.log", "a")
        f.write(f"\nsaw {l} faces on {now.day}/{now.month}/{now.year}  {now.hour}:{now.minute}:{now.second}.{now.microsecond}")
        f.close
    


    cv2.imshow('lol', frame)
    cv2.waitKey(1)

但是当它写入 BOOTEX.log 时,时间戳被冻结,我可以从毫秒看到它,如下所示:

saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 2 faces on 20/6/2021  19:50:14.985588
saw 2 faces on 20/6/2021  19:50:14.985588
saw 2 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588
saw 1 faces on 20/6/2021  19:50:14.985588

毫秒不能相同所以有人知道如何解决这个问题吗?

您需要立即更新:

import cv2
import random
import datetime



trained_face_data = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

webcam = cv2.VideoCapture(0)


import os

if os.path.isfile('./BOOT.log') == False:
   open("boot.log", "w+")
else:
   None

l = 0
def is_list_empty(list):
    # checking the length
        if len(list) == 0:
        # returning true as length is 0
            return True
    # returning false as length is greater than 0
        return False

while True:
    l = 0
    succesfull_frame_read, frame = webcam.read()

    grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    face_coordinates = trained_face_data.detectMultiScale(grey)



    for (x, y, w, h) in face_coordinates:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (random.randrange(226),random.randrange(226),random.randrange(226)), 2)
        l = l + 1
    
    lel = is_list_empty(face_coordinates)
    if lel == True:
        None
    else:
        f = open("BOOTEX.log", "a")
        now = datetime.datetime.now()

        f.write(f"\nsaw {l} faces on {now.day}/{now.month}/{now.year}  {now.hour}:{now.minute}:{now.second}.{now.microsecond}")
        f.close
    


    cv2.imshow('lol', frame)
    cv2.waitKey(1)