-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
50 lines (40 loc) · 1.49 KB
/
app.py
File metadata and controls
50 lines (40 loc) · 1.49 KB
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
49
50
from io import BytesIO
import face_recognition
import requests
import sentry_sdk
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
sentry_sdk.init(
# DSN will be read from environment.
integrations=[
AwsLambdaIntegration(timeout_warning=True),
],
traces_sample_rate=1.0,
profiles_sample_rate=0.1,
)
def handler(event, context):
"""Lambda function that will extract face encodings from a photo."""
if not (
isinstance(event.get("api_url"), str) and isinstance(event.get("sources"), list)
):
raise ValueError("Bad event data.")
api_url = event["api_url"]
sources = event["sources"]
for source in sources:
if not (
isinstance(source.get("pk"), int)
and isinstance(source.get("token"), str)
and isinstance(source.get("photo_url"), str)
and source.get("type") in {"reference", "photo"}
):
raise ValueError("Bad event data.")
for source in sources:
response = requests.get(source["photo_url"], timeout=15)
response.raise_for_status()
image = face_recognition.load_image_file(BytesIO(response.content))
encodings = face_recognition.face_encodings(image)
url = f"{api_url}/api/facedetection/encodings/{source['type']}/{source['pk']}/"
data = {
"token": source["token"],
"encodings": [list(encoding) for encoding in encodings],
}
requests.post(url, json=data, timeout=30)