I have a json file which contain polygons of filled circles. How can i implement this json file to the below code to get masks?
def polygons_to_mask_array_labelme(polygons, width : int = 300, height : int = 300) -> np.ndarray:
'''
This function takes a list of lists that contains polygon masks for each building. Example;
[[x11,y11,x12,y12,...],...,[xn1,yn1,xn2,yn2,...]]
The return of this function is an array of size width x height which contains a binary mask
as defined by the list of polygons
Example usage:
import json
with open(json_names[0], encoding = 'utf-8') as f:
data = json.load(f)
plt.imshow(polygons_to_mask_array(data['shapes'], 898, 559))
'''
img = Image.new('L', (width, height), 0)
for polygon in polygons:
nested_lst_of_tuples = [tuple(l) for l in polygon['points']]
try:
ImageDraw.Draw(img).polygon(nested_lst_of_tuples, outline=1, fill=1)
except:
print(nested_lst_of_tuples)
mask = np.array(img)
return mask
and the json file is:
name is f90.json, which shows
{"polygons": ["220.5,43.0 224.3625,42.38749999999999 227.85,40.61250000000001 230.6125,37.849999999999994 232.3875,34.36250000000001 233.0,30.5 232.3875,26.63749999999999 230.6125,23.150000000000006 227.85,20.38749999999999 224.3625,18.61250000000001 220.5,18.0 216.6375,18.61250000000001 213.15,20.38749999999999 210.3875,23.150000000000006 208.6125,26.63749999999999 208.0,30.5 208.6125,34.36250000000001 210.3875,37.849999999999994 213.15,40.61250000000001 216.6375,42.38749999999999", "351.5,116.0 355.3625,115.38749999999999 358.85,113.61250000000001 361.6125,110.85 363.3875,107.36250000000001 364.0,103.5 363.3875,99.63749999999999 361.6125,96.15 358.85,93.38749999999999 355.3625,91.61250000000001 351.5,91.0 347.6375,91.61250000000001 344.15,93.38749999999999 341.3875,96.15 339.6125,99.63749999999999 339.0,103.5 339.6125,107.36250000000001 341.3875,110.85 344.15,113.61250000000001 347.6375,115.38749999999999", "314.5,368.0 318.3625,367.3875 321.85,365.6125 324.6125,362.85 326.3875,359.3625 327.0,355.5 326.3875,351.6375 324.6125,348.15 321.85,345.3875 318.3625,343.6125 314.5,343.0 310.6375,343.6125 307.15,345.3875 304.3875,348.15 302.6125,351.6375 302.0,355.5 302.6125,359.3625 304.3875,362.85 307.15,365.6125 310.6375,367.3875", "475.5,353.0 479.3625,352.3875 482.85,350.6125 485.6125,347.85 487.3875,344.3625 488.0,340.5 487.3875,336.6375 485.6125,333.15 482.85,330.3875 479.3625,328.6125 475.5,328.0 471.6375,328.6125 468.15,330.3875 465.3875,333.15 463.6125,336.6375 463.0,340.5 463.6125,344.3625 465.3875,347.85 468.15,350.6125 471.6375,352.3875"]}