0

I already trained and got weight file in folder. I would like to know how to automatically detect the photos in the folder instead of running the code manually, and every time I add a photo to the folder, it will detect the new photo and save the result.

Please tell me how to do it, thank you.

Below is my code to detect photos

import glob
import torch
import cv2
import numpy as np

model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/runs/train/exp8/weights/best.pt',force_reload=True)

# print(model)
for img in glob.glob("C:/Users/csie/PycharmProjects/Fridge/testdata/*png"):
    cv_img =cv2.imread(img)
    results = model(cv_img)
    results.save()
  • You can implement watchdog, which would keep watching directory for any change and on change run you script https://michaelcho.me/article/using-pythons-watchdog-to-monitor-changes-to-a-directory – Smaurya Jul 30 '22 at 17:34

1 Answers1

0

try writing a script that run the code in a while. say.. after every 1 minute. or precisely, trigger the script to run the code whenever anything change in your input image folder.

try this: How to automatically run python script, when file is added to folder?

for reference: Creating automated python scripts that run every minute

Run python script every 5 minutes under Windows

Groot
  • 171
  • 13