#
Map translation
#
Purpose
The map translation module is designed for estimating real world positions of vehicular traffic based on the in frame pixel coordinates of the source video.
#
How it works
In order to translate the frame coordinates into map coordinates, points of references were used.
In both the frame coordinates and the real world coordinates (lat, long), 8 points of references were taken. These points were marked and used as anchor points to calculate the movement of the vehicles and translate it into estimated real world coordinates.
As shown in the image above, the coordinates divide the shape into four main quadrants. Depending on the quadrant that the vehicle is in, the sign for the operation to calculate the real world coordinate will change (negative or positive).
The process overall is straight foward. Suppose the vehicle is currentl in Q1, at location (x_i, y_i). We can use the reference point A, call it (x_a, y_b), and its known translation to lat and lng, call it (lat_a, lat_b), with a following reference point B, to calculate a x and y ratio using the following equation:
ratio = map\_point_b - map\_point_a / frame\_point_b - frame\_point_a
In code, this was expressed as:
# code start
def calculate_ratios(map_pt1, map_pt2, frame_pt1, frame_pt2):
x = abs(map_pt2[0] - map_pt1[0]) / abs(frame_pt2[0] - frame_pt1[0])
y = abs(map_pt2[1] - map_pt1[1]) / abs(frame_pt2[1] - frame_pt1[1])
return [x, y]
# code end
This ratio is then multiplied by the current in frame coordinates to achieve the location in the map. In code this, was done as follows:
# code start
def distance_away_map_coords(box, ratio_x, ratio_y, reference_point):
reference_x = reference_point[0]
box_x = box[0]
refernce_y = reference_point[1]
box_y = box[1]
distance_x = abs(box_x - reference_x)
distance_y = abs(box_y - refernce_y)
distance_in_map_x = distance_x * ratio_x
distance_in_map_y = distance_y * ratio_y
return [distance_in_map_x, distance_in_map_y]
# code end
#
Code Structure
The overall code application is divided into server-client structure. The front end code is written in basic js and leaflet js, following the common js setup structure while python with the socket library is used to send udp packets to the front end. The basic flow of data is show as follows:
#
Demo
This is a demo of the map translation: