需要查看最后一个文件是否与上一个文件相同。

huangapple go评论64阅读模式
英文:

Need to see if the last file is the same as the previous file

问题

我正在使用 yolov5 来检测人员,并且我正在使用以下代码来读取包含人数的文件并显示它:

import os

directory_path = r'C:\Users\MRTS\yolov5\runs\detect\exp2\labels'

os.chdir(directory_path)

most_recent_file = None
most_recent_time = 0

while True:
    for entry in os.scandir(directory_path):
        if entry.is_file():
            # 获取文件的修改时间使用 entry.stat().st_mtime_ns
            mod_time = entry.stat().st_mtime_ns
            if mod_time > most_recent_time:
                # 更新最新文件和其修改时间
                most_recent_file = r'{}'.format(entry.name)
                most_recent_time = mod_time
                
    # 打开文件以进行读取
    with open(most_recent_file, 'r') as f:  
    # 将文件内容读入变量
        lines = len(f.readlines())
        print('总人数:', lines)
        time.sleep(3)

我的问题是,如果它没有检测到人员,它将不会创建一个文件,因此这段代码将继续读取上一个文件。我想要查看最新的文件是否仍然与之前的文件相同,在这种情况下,打印出没有人。基本上,如果没有新文件,就没有人,我想要打印出来。

英文:

I'm using yolov5 to detect people and I'm using the following code to read the files containing the number of people and display it:

import os

directory_path = r'C:\Users\MRTS\yolov5\runs\detect\exp2\labels'

os.chdir(directory_path)

most_recent_file = None
most_recent_time = 0

while True:
    for entry in os.scandir(directory_path):
        if entry.is_file():
            # get the modification time of the file using entry.stat().st_mtime_ns
            mod_time = entry.stat().st_mtime_ns
            if mod_time > most_recent_time:
                # update the most recent file and its modification time
                most_recent_file = r'{}'.format(entry.name)
                most_recent_time = mod_time
                
    # open the file for reading 
    with open(most_recent_file, 'r') as f:  
    # read file contents into a variable 
        lines = len(f.readlines())
        print('Total Number of People:', lines)
        time.sleep(3)

My problem is that if it doesn't detects people, it will not create a file, so this code would just keep reading the last one. I want to see if the most recent file is still the same as the previous and in that case, print that there are 0 people. Basically, if there are no new files, there are no people and I want to print that.

答案1

得分: 1

你可以创建一个变量来跟踪most_recent_file的路径,只有在新的most_recent_file与它不同的情况下才打印:

import os
import time

directory_path = "."
recently_parsed = None
while True:
    most_recent_time, most_recent_file = max(
        (
            (entry.stat().st_mtime_ns, entry.name)
            for entry in os.scandir(directory_path)
            if entry.is_file()
        )
    )
    with open(most_recent_file, "r") as f:
        if recently_parsed == most_recent_file:
            print("没有新的人员")
        else:
            recently_parsed = most_recent_file
            lines = len(f.readlines())
            print("总人数:", lines)
        time.sleep(3)

请注意,我还将most_recent_file的确定替换为了max调用。

英文:

You can create a variable to keep track of the path of the most_recent_file, and only print if the new most_recent_file is different from it:

import os
import time

directory_path = "."
recently_parsed = None
while True:
    most_recent_time, most_recent_file = max(
        (
            (entry.stat().st_mtime_ns, entry.name)
            for entry in os.scandir(directory_path)
            if entry.is_file()
        )
    )
    with open(most_recent_file, "r") as f:
        if recently_parsed == most_recent_file:
            print("No new people")
        else:
            recently_parsed = most_recent_file
            lines = len(f.readlines())
            print("Total Number of People:", lines)
        time.sleep(3)

Note that I've also replaced the most_recent_file determination with a max call.

huangapple
  • 本文由 发表于 2023年7月17日 22:27:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定