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



我想写入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)

最新更新