Fracisz

Untitled

May 7th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. import requests
  2. import json
  3. # Detect all human faces & extract their facial landmarks via `facelandmarks`.
  4. # Once done, mimic the famous Snapchat flower crown filter.
  5. # Only three commands are actually needed in order to mimic the Snapchat filters:
  6. # face landmarks: https://pixlab.io/#/cmd?id=facelandmarks
  7. # smart resize: https://pixlab.io/#/cmd?id=smartresize
  8. # merge: https://pixlab.io/#/cmd?id=merge
  9. # Optionally: blur, grayscale, drawtext, oilpaint, etc. for cool background effects.
  10. # The following is target image that we'll superpose our filter on top of it.
  11. # This image must contain at least one face. free free to change the link to whatever your want.
  12. # Note that you can upload your own images from your app very easily. Refer to the docs for additional info.
  13. img = 'https://ak6.picdn.net/shutterstock/videos/10819841/thumb/8.jpg'
  14. # The flower crown to be composited on top of the target face
  15. flower_crown = 'http://pixlab.xyz/images/flower_crown.png'
  16. # You PixLab API key
  17. key = 'Your_PixLab_Key'
  18. # This list contain all the coordinates of the regions where the flower crown should be
  19. # Composited on top of the target face later using the `merge` command.
  20. coordinates = []
  21. # First off, call `facelandmarks` and extract all present faces plus their landmarks.
  22. print ("Detecting and extracting facial landmarks..")
  23. req = requests.get('https://api.pixlab.io/facelandmarks',params={
  24. 'img': img,
  25. 'key': key,
  26. })
  27. reply = req.json()
  28. if reply['status'] != 200:
  29. print (reply['error'])
  30. exit();
  31. total = len(reply['faces']) # Total detected faces
  32. if total < 1:
  33. # No faces were detected
  34. print ("No faces were detected..exiting")
  35. exit()
  36. print(str(total)+" faces were detected")
  37. # Iterate all over the detected faces and make our flower crown filter..
  38. for face in reply['faces']:
  39. cord = face['rectangle']
  40. # Show the face coordinates
  41. print ("Coordinates...")
  42. print ('\twidth: ' + str(cord['width']) + ' height: ' + str(cord['height']) + ' x: ' + str(cord['left']) +' y: ' + str(cord['top']))
  43. # Show landmarks:
  44. print ("Landmarks...")
  45. landmarks = face['landmarks']
  46. print ("\tBone Center: X: " + str(landmarks['bone']['center']['x']) + ", Y: "+str(landmarks['bone']['center']['y']))
  47. print ("\tBone Outer Left: X: " + str(landmarks['bone']['outer_left']['x']) + ", Y: "+str(landmarks['bone']['outer_left']['y']))
  48. print ("\tBone Outer Right: X: "+ str(landmarks['bone']['outer_right']['x'])+ ", Y: "+str(landmarks['bone']['outer_right']['y']))
  49. # More landmarks on the docs..Let's make our flower crown filter now
  50. # Resize the flower crown which is quite big right now to exactly the face width using smart resize.
  51. print ("Resizing the snap flower crown...")
  52. req = requests.get('https://api.pixlab.io/smartresize',params={
  53. 'img':flower_crown,
  54. 'key':key,
  55. 'width': 20 + cord['width'], # Face width
  56. 'height':0 # Let Pixlab decide the best height for this picture
  57. })
  58. reply = req.json()
  59. if reply['status'] != 200:
  60. print (reply['error'])
  61. exit()
  62. else:
  63. fit_crown = reply['link']
  64. # Composite the flower crown at the bone center region
  65. coordinates.append({
  66. 'img': fit_crown, # The resized crown flower
  67. 'x': landmarks['bone']['center']['x'],
  68. 'y': landmarks['bone']['center']['y'] - 10,
  69. 'center': True,
  70. 'center_y': True
  71. })
  72. # Finally, Perform the composite operation
  73. print ("Composite operation...")
  74. req = requests.post('https://api.pixlab.io/merge',
  75. headers={'Content-Type':'application/json'},
  76. data=json.dumps({
  77. 'src':img, # The target image.
  78. 'key':key,
  79. 'cord': coordinates # The coordinates list filled earlier with the resized images (i.e. The flower crown & the dog parts) and regions of interest
  80. })
  81. )
  82. reply = req.json()
  83. if reply['status'] != 200:
  84. print (reply['error'])
  85. else:
  86. # Optionally call blur, oilpaint, grayscale, meme for cool background effects..
  87. print ("Snap Filter Effect: "+ reply['link'])
Advertisement
Add Comment
Please, Sign In to add comment