Part 1 - Setup

We will be using

  • PIL (Python Image Library) for getting the picture
  • pytesseract for the OCR (Object Character Recognition)
  • googlemaps and gmaps for mapping

In [31]:
from PIL import Image 
import pytesseract 
import googlemaps
import gmaps as jupmap
import sys 
from datetime import datetime

# get my private keys for google maps and gmaps
f = open('private.key', 'r')
for line in f: 
    temp = line.rstrip('').replace(',','').replace('\n','').split(" ")
    exec(temp[0])
myMap = googlemaps.Client(key=googlemap_key)
jupmap.configure(api_key=jupmap_key)

Part 2 - Use OCR to read the address

On Android this can be done with Google Vision


In [27]:
img = Image.open('mm_address.jpg')
label = pytesseract.image_to_string(img)
print(label)
clientLocation = label.splitlines()[2] + ', ' + label.splitlines()[3]
print(clientLocation)
testLocation = '2403 Englewood Ave, Durham, NC 27705'
print(testLocation)


Gee Girl

1234 My Street
Durham, NC 27700
1234 My Street, Durham, NC 27700
2403 Englewood Ave, Durham, NC 27705

Testing location

This might not be neccessary as google will take addresses


In [28]:
testGeoCode = myMap.geocode(testLocation)[0]
lat = testGeoCode.get('geometry').get('location').get('lat')
lng = testGeoCode.get('geometry').get('location').get('lng')
print(lat, ' ', lng )
clientList = ['300 N Roxboro St, Durham, NC 27701','911 W Cornwallis Rd, Durham, NC 27707', '345 W Main Street, Durham, NC 27701' ]
wp=[]
for x in clientList:
    testGeoCode = myMap.geocode(x)[0]
    lat = testGeoCode.get('geometry').get('location').get('lat')
    lng = testGeoCode.get('geometry').get('location').get('lng')
    wp.append([lat,lng])
print(wp)


36.0157788   -78.9322282
[[35.995092, -78.8966271], [35.972583, -78.934848], [35.99659949999999, -78.9040012]]

Google Maps

This api does not have a waypoint optimization Here are more details on how to do it: https://developers.google.com/maps/documentation/directions/intro


In [29]:
m = jupmap.Map()
home = (36.0160282, -78.9321707)
foodLion = (36.0193147,-78.9603636)
church = (35.9969749, -78.9091543)
dl = jupmap.directions_layer(church, home, waypoints=wp)
#googlemaps has an optimize_waypoints=True but I can't find it in jupyter gmaps 
m.add_layer(dl)
m

In [ ]: