-
Notifications
You must be signed in to change notification settings - Fork 0
/
FCF.py
executable file
·64 lines (51 loc) · 2 KB
/
FCF.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import requests
import matplotlib.pyplot as plt
from PIL import Image
from matplotlib import patches
from io import BytesIO
import os
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cv2.imwrite('./modules/MMM-Testpython/CognitiveFace/CognitiveFace.jpg', frame)
cap.release()
cv2.destroyAllWindows()
subscription_key = "62114331d85f48c99c133b2ac9027967"
image_path = os.path.join('./modules/MMM-Testpython/CognitiveFace/CognitiveFace.jpg')
assert subscription_key
#face_api_url = 'https://magicmirror.cognitiveservices.azure.com/face/v1.0/detect'
#face_api_url = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect'
face_api_url = 'https://pay.cognitiveservices.azure.com/face/v1.0/detect'
image_data = open(image_path, "rb")
headers = {'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': subscription_key}
params = {
'returnFaceId': 'true',
'returnFaceLandmarks': 'false',
'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,' +
'emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'
}
response = requests.post(face_api_url, params=params, headers=headers, data=image_data)
response.raise_for_status()
faces = response.json()
image_read = open(image_path, "rb").read()
image = Image.open(BytesIO(image_read))
plt.figure(figsize=(8, 8))
ax = plt.imshow(image, alpha=1)
try:
for face in faces:
fr = face["faceRectangle"]
fa = face["faceAttributes"]
origin = (fr["left"], fr["top"])
p = patches.Rectangle(
origin, fr["width"], fr["height"], fill=False, linewidth=2, color='dodgerblue')
ax.axes.add_patch(p)
plt.text(origin[0], origin[1], "%s, %d"%(fa["gender"].capitalize(), fa["age"]),
fontsize=20, weight="bold", va="bottom", color='dodgerblue')
_ = plt.axis("off")
fig = plt.gcf()
fig.savefig('./modules/MMM-Testpython/CognitiveFace/CognitiveFace.jpg')
print(fa["gender"])
print(fa["age"])
except Exception as e:
print("notFind")