I cant save predicted images using yolov8

error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:728: error: (-2:Unspecified error) could not find a writer for the specified extension in function ‘cv::imwrite_’

Traceback:

  File "C:\Users\kusic\AppData\Local\Programs\Python\Python310\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
  File "C:\Users\kusic\OneDrive\Desktop\realtime-test\yolov8-streamlit-detection-tracking\app.py", line 100, in <module>
    res = model.predict(
  File "C:\Users\kusic\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\utils\_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "C:\Users\kusic\AppData\Local\Programs\Python\Python310\lib\site-packages\ultralytics\yolo\engine\model.py", line 253, in predict
    return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream)
  File "C:\Users\kusic\AppData\Local\Programs\Python\Python310\lib\site-packages\ultralytics\yolo\engine\predictor.py", line 184, in __call__
    return list(self.stream_inference(source, model))  # merge list of Result into one
  File "C:\Users\kusic\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\utils\_contextlib.py", line 35, in generator_context
    response = gen.send(None)
  File "C:\Users\kusic\AppData\Local\Programs\Python\Python310\lib\site-packages\ultralytics\yolo\engine\predictor.py", line 266, in stream_inference
    self.save_preds(vid_cap, i, str(self.save_dir / p.name))
  File "C:\Users\kusic\AppData\Local\Programs\Python\Python310\lib\site-packages\ultralytics\yolo\engine\predictor.py", line 320, in save_preds
    cv2.imwrite(save_path, im0)

I m not sure that’s an opencv problem. May be you should ask to program author

can you please help me?

please check, if save_dir exists and that p.name contains a valid filename (extension)

i am using the ultralytics python package. so when i run prediction i want to save it but its not working. i dont understand. it worked sometime and now i keep getting this error. default it saves it in “runs/detect/predict/image0.jpg”

this is the function in the predict.py in the ultralytics package

def save_preds(self, vid_cap, idx, save_path):
        """Save video predictions as mp4 at specified path."""
        im0 = self.plotted_img
        # Save imgs
        if self.dataset.mode == 'image':
            cv2.imwrite(save_path, im0)
        else:  # 'video' or 'stream'
            if self.vid_path[idx] != save_path:  # new video
                self.vid_path[idx] = save_path
                if isinstance(self.vid_writer[idx], cv2.VideoWriter):
                    self.vid_writer[idx].release()  # release previous video writer
                if vid_cap:  # video
                    fps = int(vid_cap.get(cv2.CAP_PROP_FPS))  # integer required, floats produce error in MP4 codec
                    w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
                    h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
                else:  # stream
                    fps, w, h = 30, im0.shape[1], im0.shape[0]
                save_path = str(Path(save_path).with_suffix('.mp4'))  # force *.mp4 suffix on results videos
                self.vid_writer[idx] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
            self.vid_writer[idx].write(im0)

You seem to try to divide the strings you may want to concatenate:

str(self.save_dir / p.name)

…and turn into string a thing that already should be s string…

that is using python’s pathlib.Path class. the result of “division” is concatenation of paths. it doesn’t exactly behave like a string. some consumers need this turned into a string explicitly.

without a MRE, we can’t help. what has been posted so far is definitely insufficient to draw any conclusions.