Hi, I’m trying to crop images around traffic signs, I’m trying to do it with polygon coordinates but in the results there are always cropped pieces of roads or other random pieces of the image. Am I doing something wrong with the normalization? Or is this not possible?
This is my code:
for el in glob.glob('D:/Dataset Custom Mapillary/images/*.jpg'):
name = Path(el).stem
with open('D:/Dataset Custom Mapillary/annotations_image/' + name + '.json', "r") as file:
temp = geojson.load(file)
image_id = temp[1][0]['id_image']
image_url = f'https://graph.mapillary.com/{image_id}?access_token={codes.API_KEY}&fields=height,width,thumb_original_url'
detections_url = f'https://graph.mapillary.com/{image_id}/detections?access_token={codes.API_KEY}&fields=geometry'
# request the image: get the height, width, and a URL to the original resolution blurred image
response = requests.get(image_url)
image_data = response.json()
height = image_data['height']
width = image_data['width']
jpeg_url = image_data['thumb_original_url']
print(jpeg_url)
# request the detection
response = requests.get(detections_url)
json = response.json()
detection = json['data'][0]
base64_string = detection['geometry']
# decode from base64
vector_data = base64.decodebytes(base64_string.encode('utf-8'))
# decode the vector tile into detection geometry
decoded_geometry = mapbox_vector_tile.decode(vector_data)
# select just the coordinate xy pairs from this detection
detection_coordinates = decoded_geometry['mpy-or']['features'][0]['geometry']['coordinates']
print(detection_coordinates)
# normalize by the 4096 extent, then multiply by image height and width to get true coordinate location
pg = [[[int(x / 4096 * width), int(y / 4096 * height)] for x, y in tuple(coord_pair)] for coord_pair in detection_coordinates]
print(pg)
img = cv2.imread(el)
pts = np.array(pg)
## (1) Crop the bounding rect
rect = cv2.boundingRect(pts)
x, y, w, h = rect
cropped = img[y:y + h, x:x + w].copy()
## (2) make mask
pts = pts - pts.min(axis=0)
mask = np.zeros(cropped.shape[:2], np.uint8)
cv2.drawContours(mask, [pts], -1, (255, 255, 255), -1, cv2.LINE_AA)
## (3) do bit-op
dst = cv2.bitwise_and(cropped, cropped, mask=mask)
## (4) add the white background
bg = np.ones_like(cropped, np.uint8) * 255
cv2.bitwise_not(bg, bg, mask=mask)
dst2 = bg + dst
cv2.imwrite("D:/Dataset Custom Mapillary/test_crop/cropped_"+name+".jpg", cropped)
cv2.imwrite("D:/Dataset Custom Mapillary/test_crop/mask_"+name+".jpg", mask)
cv2.imwrite("D:/Dataset Custom Mapillary/test_crop/dst_"+name+".jpg", dst)
cv2.imwrite("D:/Dataset Custom Mapillary/test_crop/dst2_"+name+".jpg", dst2)
This is the output:
4032 3024
[[[2934, 1632], [2997, 1632], [2997, 1520], [2934, 1520], [2934, 1632]]]
[[[2888, 1204], [2950, 1204], [2950, 1122], [2888, 1122], [2888, 1204]]]
1223074091455516
3264 2448
[[[333, 2596], [451, 2596], [451, 2399], [333, 2399], [333, 2596]]]
[[[265, 1551], [359, 1551], [359, 1433], [265, 1433], [265, 1551]]]
1381323478934088
2576 1932
[[[1665, 2620], [1759, 2620], [1759, 2492], [1665, 2492], [1665, 2620]]]
[[[1047, 1235], [1106, 1235], [1106, 1175], [1047, 1175], [1047, 1235]]]
169575281758353
4032 3024
[[[2757, 2729], [2957, 2729], [2957, 2473], [2757, 2473], [2757, 2729]]]
[[[2713, 2014], [2910, 2014], [2910, 1825], [2713, 1825], [2713, 2014]]]
181274350545769
4032 3024
[[[885, 2516], [942, 2516], [942, 2307], [885, 2307], [885, 2516]]]
[[[871, 1857], [927, 1857], [927, 1703], [871, 1703], [871, 1857]]]
248177517087493
4032 3024
[[[1390, 2296], [1540, 2296], [1540, 2074], [1390, 2074], [1390, 2296]]]
[[[1368, 1695], [1515, 1695], [1515, 1531], [1368, 1531], [1368, 1695]]]
278425803923946
3968 2976
[[[352, 3746], [424, 3746], [424, 3646], [352, 3646], [352, 3746]]]
[[[341, 2721], [410, 2721], [410, 2649], [341, 2649], [341, 2721]]]
296311645439373
3968 2976
[[[352, 3746], [424, 3746], [424, 3646], [352, 3646], [352, 3746]]]
[[[341, 2721], [410, 2721], [410, 2649], [341, 2649], [341, 2721]]]
296369235433614