forked from Hironsan/google-vision-sampler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_detection.py
48 lines (36 loc) · 1.28 KB
/
face_detection.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
This script uses the Vision API's face detection capabilities to find faces
based on an image's content.
To run the example, install the necessary libraries by running:
pip install -r requirements.txt
Run the script on an image to get faces, E.g.:
./face_detection.py <path-to-image>
"""
import argparse
import os
from utils import Service, encode_image
def main(photo_file):
"""Run a face detection request on a single image"""
access_token = os.environ.get('VISION_API')
service = Service('vision', 'v1', access_token=access_token)
with open(photo_file, 'rb') as image:
base64_image = encode_image(image)
body = {
'requests': [{
'image': {
'content': base64_image,
},
'features': [{
'type': 'FACE_DETECTION',
'maxResults': 1,
}]
}]
}
response = service.execute(body=body)
faces = response['responses'][0]['faceAnnotations']
print('Found faces: {}'.format(faces))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('image_file', help='The image you\'d like to detect text.')
args = parser.parse_args()
main(args.image_file)