-
Notifications
You must be signed in to change notification settings - Fork 2
/
visualize_viewpoints.py
72 lines (58 loc) · 2.28 KB
/
visualize_viewpoints.py
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
64
65
66
67
68
69
70
71
72
"""
Visualize viewpoints of one panoramic image
"""
import argparse
from io import BytesIO
import requests
from PIL import Image
import numpy as np
from src.api_request import get_pano_data
from src.geometry import viewpoint_to_pixels
PANO_WIDTH = 2000 # pixels
RED = (255,0,0)
BLUE = (0,0,255)
GREEN = (0,255,0)
def visualize_viewpoints_street_view(pano_url, bbox, center_bbox, heading):
"""
Save visualization of viewpoint directions of one panoramic image
"""
# Get the panoramic image
try:
response = requests.get(pano_url)
source_image = Image.open(BytesIO(response.content))
except requests.exceptions.RequestException:
print("HTTP Request failed. Aborting.")
return
# Convert the viewpoints (in degrees) to pixel values
viewpoint_front = viewpoint_to_pixels(heading, PANO_WIDTH)
viewpoint_back = viewpoint_to_pixels(heading - 180, PANO_WIDTH)
# Draw the front and back viewpoints of the vehicle
np_image = np.array(source_image)
np_image[:, int(viewpoint_front), :] = RED
np_image[:, int(viewpoint_back), :] = BLUE
# Draw a bbox around the detected object
x_min, y_min, x_max, y_max = bbox
np_image[y_min:y_max, x_min, :] = GREEN
np_image[y_min:y_max, x_max, :] = GREEN
np_image[y_min, x_min:x_max, :] = GREEN
np_image[y_max, x_min:x_max, :] = GREEN
# Draw a line through the center of the bbox
np_image[:, int(center_bbox), :] = GREEN
# Save the image
img = Image.fromarray(np_image, "RGB")
img.save("examples/street_view.jpeg", "jpeg")
if __name__ == '__main__':
# Read command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('-b', '--bbox', type=str, required=True,
help='Bounding box of the detected bicycle symbol: x_min y_min x_max y_max')
parser.add_argument('-p', '--pano_id', type=str, required=True,
help='Id of the panoramic image')
args = parser.parse_args()
bbox = [int(item)for item in args.bbox.split(',')]
# API calls
panoramic_image, heading = get_pano_data(args.pano_id)
# Get the horizontal center of a bbox
center_bbox = (bbox[0] + bbox[2]) / 2
# Visualize viewpoints
visualize_viewpoints_street_view(panoramic_image, bbox, center_bbox, heading)