Skip to content

Instantly share code, notes, and snippets.

@sdcubber
Created November 15, 2018 09:15
Show Gist options
  • Save sdcubber/ebce5b02b2d995e93f79b1677e564ee7 to your computer and use it in GitHub Desktop.
Save sdcubber/ebce5b02b2d995e93f79b1677e564ee7 to your computer and use it in GitHub Desktop.
# Call the vision API to extract text from the image
imageresponse = vision_client.document_text_detection(image=image)
# Lump all the text from the image in a single string
text = ' - '.join(imageresponse.text_annotations[0].description.split('\n'))
# Call the natural language API to extract entities for our text
document = language.types.Document(content=text, type=language.enums.Document.Type.PLAIN_TEXT)
languageresponse = language_client.analyze_entities(document=document)
# Collect all text that was identified as a "PERSON"
entities = languageresponse.entities
persons = [e.name for e in entities if e.type == 1]
# Loop over all persons and draw black boxes over them
for person in persons:
for annotation in imageresponse.text_annotations[1:]:
if annotation.description in person:
v = annotation.bounding_poly.vertices
box = [v[0].x, v[0].y, v[2].x, v[2].y]
draw.rectangle(box, fill='black')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment