Skip to content

Instantly share code, notes, and snippets.

@symisc
Last active May 27, 2021 00:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save symisc/6ecdea76ba0d33d73ea7f23cade0d216 to your computer and use it in GitHub Desktop.
Save symisc/6ecdea76ba0d33d73ea7f23cade0d216 to your computer and use it in GitHub Desktop.
Apply a blur filter automatically for each detected face using the PixLab Rest API (Python Sample)
import requests
import json
imgUrl = 'https://pixlab.io/images/m3.jpg' # Target picture we want to blur any face on
# Detect all human faces in a given image via /facedetect first and blur all of them later via /mogrify.
# https://pixlab.io/cmd?id=facedetect & https://pixlab.io/cmd?id=mogrify for additional information.
req = requests.get('https://api.pixlab.io/facedetect',params={
'img': imgUrl,
'key':'PIXLAB_API_KEY',
})
reply = req.json()
if reply['status'] != 200:
print (reply['error'])
exit();
total = len(reply['faces']) # Total detected faces
print(str(total)+" faces were detected")
if total < 1:
# No faces were detected, exit immediately
exit()
# Pass the detected faces coordinates untouched to mogrify
coordinates = reply['faces']
# Call mogrify via POST and pass the facial coordinates extracted earlier to blur the face(s)
req = requests.post('https://api.pixlab.io/mogrify',headers={'Content-Type':'application/json'},data=json.dumps({
'img': imgUrl,
'key':'PIXLAB_API_KEY',
'cord': coordinates # The field of interest
}))
reply = req.json()
if reply['status'] != 200:
print (reply['error'])
else:
print ("Blurred Faces URL: "+ reply['ssl_link'])
@symisc
Copy link
Author

symisc commented May 31, 2020

Detect all human faces in a given image via /facedetect first and blur all of them later via /mogrify.
https://pixlab.io/cmd?id=facedetect & https://pixlab.io/cmd?id=mogrify for additional information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment