Skip to content

Instantly share code, notes, and snippets.

@symisc

symisc/auto_blur_faces.py

Last active May 27, 2021
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@symisc 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