Can not save json result file on Roboflow yolov8 colab

Hello all,
Just like on shared colab script on: Google Colab

I was able to successfully call my custom pre-trained weight and perform instance segmentation.
However, I struggled so hard but can not save the return file in json.
Please see the below simple script and help me how to save the prediction

!pip install roboflow

rf = Roboflow(api_key="YOUR_PRIVATE_API_KEY")
workspace = rf.workspace("workspace-id")
project = workspace.project("model-id")
version = project.version("version-number")
model = version.model

prediction = model.predict("INSERT IMAGE PATH")

# Plot the prediction
prediction.plot()

# Convert predictions to JSON
prediction.json()

so now, i get a typical json file type looking segmentation result, and it is clear to even see from print(prediction). But I simply can not save this…please help.

Have you tried to use something like this?

def safe_create_path_parent(path: str) -> None:
    path_parent = os.path.dirname(path)
    os.makedirs(path_parent, exist_ok=True)


def dump_to_json(target_path: str, content: dict) -> None:
    safe_create_path_parent(target_path)
    with open(target_path, 'w') as outfile:
        json.dump(content, outfile, indent=4)

I’m a bit lost. Can you modify the directory so it can work on my script?
Also, where is the location of the saved file?

Should I be using sometime like

import os

def safe_create_path_parent(path: str) -> None:
    path_parent = os.path.dirname(/content/Untitled Folder)
    os.makedirs(path_parent, exist_ok=True)


def dump_to_json(target_path: str, content: dict) -> None:
    safe_create_path_parent(target_path)
    with open(target_path, 'w') as outfile:
        json.dump(content, outfile, indent=4)

??

[quote=“Sun_Ho_Ro, post:1, topic:1563”]

!pip install roboflow

import os

def safe_create_path_parent(path: str) -> None:
    path_parent = os.path.dirname(path)
    os.makedirs(path_parent, exist_ok=True)


def dump_to_json(target_path: str, content: dict) -> None:
    safe_create_path_parent(target_path)
    with open(target_path, 'w') as outfile:
        json.dump(content, outfile, indent=4)

rf = Roboflow(api_key="YOUR_PRIVATE_API_KEY")
workspace = rf.workspace("workspace-id")
project = workspace.project("model-id")
version = project.version("version-number")
model = version.model

image_path = "INSERT IMAGE PATH"
prediction = model.predict("INSERT IMAGE PATH")

# Plot the prediction
prediction.plot()

# Convert predictions to JSON
json_results = prediction.json()

# make safe save path and save results files
dump_to_json(f“{os.path.basename(image_path)}.json”, json_results)

Hi, the code provided by @SkalskiP is of example functions you can use to save the JSON data to files.

Switch your code to the above to save JSON files of predictions.

Alternatively, you can add detections to a Pandas DataFrame and export from the dataframe.

I think almost there…one last help, please.

@Mohamed @SkalskiP

Hi @Sun_Ho_Ro - the code I’ve provided above should work fine for saving the JSON results.

Can you try deleting the “/content/Untitled Folder” from your folders in your Colab environment, and running it again?