yolov5中编写一个detect.py中让程序一直执行下去的方法

yolov5中编写一个detect.py中让程序一直执行下去的方法

def run():
    # ...
    def process_image(path, im, im0s, vid_cap, s):
        try:
            if not any(path.lower().endswith(ext) for ext in file_extensions):
                # 删除非图片文件
                os.remove(path)
                return
            
            assert im0 is not None, f'Image Not Found {path}'
            # 在此之后添加您的 yolov5 代码
        
        except AssertionError as e:
            # 删除无法找到或无法识别的图像文件
            error_message = str(e)
            image_path = error_message.split()[-1]
            os.remove(image_path)
            # 返回到循环
            return
        
        # 继续进行后续处理或其他操作
        
    for path, im, im0s, vid_cap, s in dataset:
        process_image(path, im, im0s, vid_cap, s)
    # ...
def process_files_in_folder(folder_path):
    while True:
        # 检查文件夹是否为空
        if len(os.listdir(folder_path)) == 0:
            print(f"No images found in {folder_path}. Waiting for images...")
            # 等待一段时间,然后重新检查文件夹
            sleep(5)  # 每隔 5 秒检查一次文件夹
            continue

        print("Images found in the folder!")
        run()  # 执行您的 yolov5 代码
if __name__ == '__main__':
    folder_path = "E:/python/yoloProjectTestSmallLarge/data/images"  # 您的图片文件夹路径
    process_files_in_folder(folder_path)
    opt = parse_opt()
    main(opt)

你可能感兴趣的:(python,YOLO,前端,javascript,python)