问题描述:在运行的.py,出现找不到图片的问题 。
具体错误如下所示:
Traceback (most recent call last):File "C:/Users/aoqia/Desktop/ultralytics-main/ultralytics/yolo/v8/detect/predict.py", line 101, in predict()File "C:/Users/aoqia/Desktop/ultralytics-main/ultralytics/yolo/v8/detect/predict.py", line 97, in predictpredictor.predict_cli()File "C:\Users\aoqia\Desktop\ultralytics-main\ultralytics\yolo\engine\predictor.py", line 119, in predict_clifor _ in gen:# running CLI inference without accumulating any outputs (do not modify)File "C:\Users\aoqia\anaconda3\envs\pytorch\lib\site-packages\torch\autograd\grad_mode.py", line 44, in generator_contextresponse = gen.send(None)File "C:\Users\aoqia\Desktop\ultralytics-main\ultralytics\yolo\engine\predictor.py", line 146, in stream_inferenceself.setup_source(source if source is not None else self.args.source)File "C:\Users\aoqia\Desktop\ultralytics-main\ultralytics\yolo\engine\predictor.py", line 128, in setup_sourceself.dataset = load_inference_source(source=source,File "C:\Users\aoqia\Desktop\ultralytics-main\ultralytics\yolo\data\build.py", line 186, in load_inference_sourcedataset = LoadImages(source,File "C:\Users\aoqia\Desktop\ultralytics-main\ultralytics\yolo\data\dataloaders\stream_loaders.py", line 188, in __init__raise FileNotFoundError(f'{p} does not exist')FileNotFoundError: C:\Users\aoqia\Desktop\ultralytics-main\ultralytics\000490.jpg does not exist
解决办法1:这个图片是需要放到这个文件夹才行 。因为.py是从文件夹中找图片的,放到根目录,肯定找不到了 。解决之后第二个图所示 。
文章插图
图1
图2
解决办法2:
把后面的代码修改
源代码是这个
def predict(cfg=DEFAULT_CFG, use_python=False):model = cfg.model or 'best.pt'source = cfg.source if cfg.source is not None else ROOT / 'assets/000490.jpg' if (ROOT / 'assets').exists() \else 'https://ultralytics.com/images/bus.jpg'args = dict(model=model, source=source)if use_python:from ultralytics import YOLOYOLO(model)(**args)else:predictor = DetectionPredictor(overrides=args)predictor.predict_cli()
改后的代码是这个:注意两点,图片路径必须是绝对路径,并且路径是双斜杠 。之后就运行通了 。如图3就是运行通的结果图 。
【FileNotFoundError: C:\Users\aoqia\Deskto】def predict(cfg=DEFAULT_CFG, use_python=False):model = 'best.pt'source ='C:\\Users\\aoqia\\Desktop\\ultralytics-main\\000490.jpg'args = dict(model=model, source=source)if use_python:from ultralytics import YOLOYOLO(model)(**args)else:predictor = DetectionPredictor(overrides=args)predictor.predict_cli()
图3