Guest User

Untitled

a guest
Apr 26th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. import pprint
  2. import boto3
  3.  
  4. from PIL import Image, ImageDraw
  5.  
  6. rek = boto3.client('rekognition')
  7.  
  8. with open('TestingZone/MonaLisa.jpg', 'rb') as f:
  9. image_bytes = f.read()
  10.  
  11. ##########################################
  12. response = rek.detect_labels(
  13. Image={
  14. 'Bytes': image_bytes
  15. })
  16. pprint.pprint(response)
  17.  
  18. ##########################################
  19. response = rek.detect_faces(
  20. Image={
  21. 'Bytes': image_bytes
  22. },
  23. Attributes=['ALL'])
  24. pprint.pprint(response)
  25.  
  26. ##########################################
  27. src = Image.open('TestingZone/MonaLisa.jpg')
  28. draw1 = ImageDraw.Draw(src)
  29. width, height = src.size
  30. img = Image.new("RGB", src.size)
  31. draw = ImageDraw.Draw(img)
  32. img.paste(src, (0, 0))
  33.  
  34. for face in response["FaceDetails"]:
  35. for point in face["Landmarks"]:
  36. x = point["X"] * width
  37. y = point["Y"] * height
  38. r = 5
  39. draw.ellipse((x-r, y-r, x+r, y+r), fill="red")
  40.  
  41. img.save('TestingZone/MonaLisa-rek.jpg')
  42.  
  43.  
  44. ##########################################
  45. with open('TestingZone/pike-st.jpg', 'rb') as f:
  46. image_bytes = f.read()
  47.  
  48. response = rek.detect_text(
  49. Image={
  50. 'Bytes': image_bytes
  51. })
  52. pprint.pprint(response)
  53.  
  54. src = Image.open('TestingZone/pike-st.jpg')
  55. draw1 = ImageDraw.Draw(src)
  56. width, height = src.size
  57. img = Image.new("RGB", src.size)
  58. draw = ImageDraw.Draw(img)
  59. img.paste(src, (0, 0))
  60.  
  61. for text in response["TextDetections"]:
  62. points = [(point['X']*width, point['Y']*height) for point in text["Geometry"]["Polygon"]]
  63. points.append(points[0])
  64. draw.line(points, fill="red", width=9)
  65.  
  66. img.save('TestingZone/pike-st-rek.jpg')
Add Comment
Please, Sign In to add comment