-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_to_labelme.py.bak
63 lines (47 loc) · 1.79 KB
/
convert_to_labelme.py.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import json
import base64
IMAGE_PATH = "/home/dyuthi/Pictures/Screenshots/food2.png"
def image_to_base64(image_path):
with open(image_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
return encoded_string
def convert_to_labelme(result):
image_info = result['image']
predictions = result['predictions']
labelme_data = {
"version": "4.5.6",
"flags": {},
"shapes": []
}
for prediction in predictions:
points = [[point['x'], point['y']] for point in prediction['points']]
shape = {
"label": prediction['class'],
"points": points,
"group_id": None,
"shape_type": "polygon",
"flags": {}
}
labelme_data['shapes'].append(shape)
labelme_data["imagePath"] = IMAGE_PATH # Provide the image path here
# Generate image data and add it to the JSON
image_data = image_to_base64(IMAGE_PATH)
labelme_data["imageData"] = image_data
labelme_data["imageHeight"] = image_info['height']
labelme_data["imageWidth"] = image_info['width']
return json.dumps(labelme_data, indent=2)
from inference_sdk import InferenceHTTPClient
CLIENT = InferenceHTTPClient(
api_url="https://outline.roboflow.com",
api_key="McnrQqcI6NyxbRYaSnjd"
)
result = CLIENT.infer(IMAGE_PATH, model_id="food-usjv9/1")
# Assuming `result` is the dictionary returned by the inference SDK
labelme_json = convert_to_labelme(result)
print(labelme_json)
# Specify the file path where you want to save the JSON data
output_file_path = "labelme_data.json"
# Write the JSON data to the file
with open(output_file_path, "w") as json_file:
json_file.write(labelme_json)
print(f"LabelMe JSON data has been saved to {output_file_path}")