簡體   English   中英

python時間格式凍結在一個真正的循環中

[英]python time format freezed in a while True loop

我想寫信給BOOTEX.log :在{day}/{month}/{year} {hour}:{minute}:{nsecond}.{microsecond} BOOTEX.log {n faces}上看到了{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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM