Multi-Face Recognition & Data Exploitation

Profile image for whtn12
December 6, 2017 | Views: 1451

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

Multi-Face Recognition & Data Exploit with Python + Azure FaceAPI

Howdy Azure users again,

Today I will be showing you how to exploit the powerful Azure FaceAPI n order to extract data about people in images.I assume that you already read the previous article as there are some fundamentals you need to know about Azure in order to follow along.

https://s3curi7y.tn/2017/12/02/azure-face-api-hands-using-python-basic-version/

Also, you can find the code I’m about to explain in my repository.

What does this code do exactly?

Actually, this is a Python code which allows the detection and identification of human faces using Azure’s API. It is composed of two main functions paraMade and recogn each requiring a key and a URL.

How does it work?

The code is composed of two functions, both get their arguments from the sys.argv array.

  • paraMade:

This function is responsible for sending a request with the URL of the image and receiving details about detected faces in JSON format (JS Object Notation)

def paraMade(key, url): # defining the function
subscription_key = key # getting API token
uri_base = ‘https://eastus.api.cognitive.microsoft.com’ # setting endpoint URL
headers = {
‘Content-Type’: ‘application/json’,
‘Ocp-Apim-Subscription-Key’: subscription_key,
} # setting the headers of the request
params = {
‘returnFaceId’: ‘true’,
‘returnFaceLandmarks’: ‘false’,
‘returnFaceAttributes’: ‘age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise’,
} # setting the parameters of the request
body = {‘url’: url} #assigning the url of the image from the func argument

try:
response = requests.request(‘POST’, uri_base + ‘/face/v1.0/detect’, json=body, data=None, headers=headers, params=params) # sending POST request to the endpoint and collecting response
parsed = json.loads(response.text) # parsing the response
print(“tIn this picture : %snWe found that:n” % url)

print (json.dumps(parsed, sort_keys=True, indent=2)) # printing the list of detected faces

print(“tThere are %i people” % parsed.len())# counting number of detected faces
for person in parsed: # looping through the list of faces
print(“t> Person %i:ntThis is a %i-year old %s” %(parsed.index(person)+1, person[“faceAttributes”][“age”],person[“faceAttributes”][“gender”]))         #
if int(person[“faceAttributes”][“hair”][“bald”]) == 0:                    # PARSING JSON OBJECTS
if is_male(person[“faceAttributes”][“gender”]):                          #
print(“tHis face, has the id %s” % (person[‘faceId’]))                # and using data to make a paragraph
else:
print(“tHer face, has the id %s”% (person[‘faceId’]))                 # except Exception as e:
print(‘Error:’)
print(e)

  • recogn:

def recogn(KEY, img_url):                                   # defining the function
CF.Key.set(KEY)                                                 # setting the API key
BASE_URL = ‘https://eastus.api.cognitive.microsoft.com/face/v1.0/’   # endpoint URL
CF.BaseUrl.set(BASE_URL)                               # setting the endpoint URL
detected = CF.face.detect(img_url)
print(detected)                                                     # printing the list of detected faces
def getRectangle(faceDictionary):                       # defining rectangle coordinates-related function
rect = faceDictionary[‘faceRectangle’]
left = rect[‘left’]
top = rect[‘top’]
bottom = left + rect[‘height’]
right = top + rect[‘width’]
return ((left, top), (bottom, right))                          # returning coordinates

response = requests.get(img_url)                        # downloading image
img = Image.open(BytesIO(response.content))   # opening image using BytesIO

draw = ImageDraw.Draw(img)                             # setting the image for drawing using ImageDraw from Pillow
for face in detected:                                             # looping in list of detected faces
draw.rectangle(getRectangle(face), outline=’blue’) # drawing rectangles

img.show()                                                            # outputting results

Usage?

Command: python FaceAPI.py {key} {Image URL}

Author: Muhammad Habib Jawady

Share and Earn Cybytes
FacebookTwitterGoogle+LinkedInEmail
Save
+1
1
0
Use Cybytes and
Tip the Author!
Join
Share and Earn
Cybytes
FacebookTwitterGoogle+LinkedInEmail
Ready to share your knowledge and expertise?
Be the Best at Whatever You Do.
We Have the Tools to Get You There.
Visit the NEW Marketplace of Over 500 Skill Enhancement Tools.
Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Cybrary On The Go

Get the Cybrary app for Android for online and offline viewing of our lessons.

Get it on Google Play
 

Support Cybrary

Donate Here to Get This Month's Donor Badge

 

Cybrary|0P3N

DUHK: The Technique That Got the VPN Compromised
Views: 2704 / December 10, 2017
What is Docker? [Series]
Views: 2976 / December 9, 2017
Wanna-Cry Ransomware
Views: 2832 / December 9, 2017
The Abyssal Depth of the Deep Web
Views: 2771 / December 8, 2017
Skip to toolbar

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel