How to autorun detect.py (Yolov5) when add video to folder?

Viewed 522

I already trained and got weight file in folder. I'd like to know. How to autorun detect.py (python detect.py --weights runs/train/exp/weights/best.pt --img 640 --conf 0.3 --source data/images/AAA.avi --save-txt) automatic when I put my video in folder.

Please recommend to me Thank you so much.

1 Answers

First, modify the main function so that detect.py can be executed in another Python file.

def main(opt):
    check_requirements(exclude=('tensorboard', 'thop'))
    run(**vars(opt))

if __name__ == "__main__":
    opt = parse_opt()
    main(opt)

Replace the above with the below.

def main(video):
    global video_path
    video_path = video
    opt = parse_opt()
    check_requirements(exclude=('tensorboard', 'thop'))
    run(**vars(opt))

And you need to modified on 78 line in detect.py

    source = str(source)

edit the source.

    source = video_path

weights, img, and conf, which are parsers excluding source, can be put in the default of the parse_opt function or directly in the run function. I modified the parser's default to avoid conflicts.

parser.add_argument('--weights', nargs='+', type=str, default='runs/train/exp/weights/best.pt', help='model path(s)')
parser.add_argument('--conf-thres', type=float, default=0.3, help='confidence threshold')

Below is a python file that runs detect.py

import os

count = 0
while True:
    video_input = os.listdir('./data/images')
    if len(video_input) > count:
        count += 1
        detect.main('./data/images' + video_input[-1])
Related