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])