-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_service.py
34 lines (26 loc) · 1.08 KB
/
face_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from PIL import Image, ImageDraw
from azure.cognitiveservices.vision.face import FaceClient
from msrest.authentication import CognitiveServicesCredentials
from os import environ
def get_faces(image_path):
face_client = FaceClient(environ['face_endpoint'], CognitiveServicesCredentials(environ['face_key']))
image = open(image_path, 'rb')
return face_client.face.detect_with_stream(image)
# Convert width height to a point in a rectangle
def get_rectangle(faceDictionary):
rect = faceDictionary.face_rectangle
left = rect.left
top = rect.top
right = left + rect.width
bottom = top + rect.height
return (left, top), (right, bottom)
def draw_face_rectangles(image_path: str, detected_faces):
# For each face returned use the face rectangle and draw a red box.
image = Image.open(image_path)
draw = ImageDraw.Draw(image)
for face in detected_faces:
draw.rectangle(get_rectangle(face), outline='red', width=4)
image.show()
def face_recognition(path: str):
detected_faces = get_faces(path)
draw_face_rectangles(path, detected_faces)