# api_week5_work
**Repository Path**: he_jian_hao/api_week5_work
## Basic Information
- **Project Name**: api_week5_work
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 1
- **Created**: 2020-10-24
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# 人脸识别+计算机视觉
* [1.三种(两种已提供/再找一种练习)及以上不同API平台人脸识别(面部检测/人脸集合)](#work1)
* [2.计算机视觉(Azure API)所有的功能](#work2)
* [3.学习心得(总结)](#work3)
***
### 一.三种不同API平台人脸识别(面部检测/人脸集合)
#### A.microsoft windows azure API
#### A-1 面部检测
##### 输入
```
import requests
import json
```
##### 输入密匙
```
subscription_key = "22860f2ec4fa40668703f86a97b571b6"
assert subscription_key
```
##### 终结点链接
```
face_api_url = 'https://chick.cognitiveservices.azure.com/face/v1.0/detect'
```
##### 请求正文body与图片链接
```
image_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603391956426&di=c8f7f01de1249fa11649d7e45ca04517&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180205%2F1e7e0127a60a481f86d7de0633ac42bf.jpeg'
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
```
##### 请求参数parameters
```
params = {
'returnFaceId': 'true',
'returnFaceLandmarks': 'false',
```
##### 可选参数,依据api文档
```
'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, json={"url": image_url})
##### json.dumps 将json代码转为bytes
json.dumps(response.json())
```
##### 获得:
```
'[{"faceId": "670f1e49-3f7c-41f7-9f24-0530aeae0457", "faceRectangle": {"top": 69, "left": 341, "width": 124, "height": 124}, "faceAttributes": {"smile": 1.0, "headPose": {"pitch": 8.7, "roll": 29.8, "yaw": 5.0}, "gender": "male", "age": 22.0, "facialHair": {"moustache": 0.1, "beard": 0.1, "sideburns": 0.1}, "glasses": "NoGlasses", "emotion": {"anger": 0.0, "contempt": 0.0, "disgust": 0.0, "fear": 0.0, "happiness": 1.0, "neutral": 0.0, "sadness": 0.0, "surprise": 0.0}, "blur": {"blurLevel": "low", "value": 0.1}, "exposure": {"exposureLevel": "overExposure", "value": 0.8}, "noise": {"noiseLevel": "medium", "value": 0.64}, "makeup": {"eyeMakeup": true, "lipMakeup": true}, "accessories": [], "occlusion": {"foreheadOccluded": false, "eyeOccluded": false, "mouthOccluded": false}, "hair": {"bald": 0.02, "invisible": false, "hairColor": [{"color": "brown", "confidence": 1.0}, {"color": "black", "confidence": 0.94}, {"color": "red", "confidence": 0.18}, {"color": "gray", "confidence": 0.11}, {"color": "blond", "confidence": 0.1}, {"color": "other", "confidence": 0.07}, {"color": "white", "confidence": 0.0}]}}}]'
```
#### A-2 json转义
##### 输入:
```
results = response.json()
results
```
##### 获得:
```
[{'faceId': '670f1e49-3f7c-41f7-9f24-0530aeae0457',
'faceRectangle': {'top': 69, 'left': 341, 'width': 124, 'height': 124},
'faceAttributes': {'smile': 1.0,
'headPose': {'pitch': 8.7, 'roll': 29.8, 'yaw': 5.0},
'gender': 'male',
'age': 22.0,
'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},
'glasses': 'NoGlasses',
'emotion': {'anger': 0.0,
'contempt': 0.0,
'disgust': 0.0,
'fear': 0.0,
'happiness': 1.0,
'neutral': 0.0,
'sadness': 0.0,
'surprise': 0.0},
'blur': {'blurLevel': 'low', 'value': 0.1},
'exposure': {'exposureLevel': 'overExposure', 'value': 0.8},
'noise': {'noiseLevel': 'medium', 'value': 0.64},
'makeup': {'eyeMakeup': True, 'lipMakeup': True},
'accessories': [],
'occlusion': {'foreheadOccluded': False,
'eyeOccluded': False,
'mouthOccluded': False},
'hair': {'bald': 0.02,
'invisible': False,
'hairColor': [{'color': 'brown', 'confidence': 1.0},
{'color': 'black', 'confidence': 0.94},
{'color': 'red', 'confidence': 0.18},
{'color': 'gray', 'confidence': 0.11},
{'color': 'blond', 'confidence': 0.1},
{'color': 'other', 'confidence': 0.07},
{'color': 'white', 'confidence': 0.0}]}}}]
```
#### A-3 pandas 数据表格化
##### 输入
```
import pandas as pd
df_face = pd.json_normalize(results)
df_face
```
##### 获得
|faceId|faceRectangle.top|faceRectangle.left|faceRectangle.width|faceRectangle.height|faceAttributes.smile|faceAttributes.headPose.pitch|faceAttributes.headPose.roll|faceAttributes.headPose.yaw|faceAttributes.gender|...|faceAttributes.noise.value|faceAttributes.makeup.eyeMakeup|faceAttributes.makeup.lipMakeup|faceAttributes.accessories|faceAttributes.occlusion.foreheadOccluded|faceAttributes.occlusion.eyeOccluded|faceAttributes.occlusion.mouthOccluded|faceAttributes.hair.bald|faceAttributes.hair.invisible|faceAttributes.hair.hairColor|
|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|
|0 670f1e49-3f7c-41f7-9f24-0530aeae0457|69|341|124|124|1.0|8.7|29.8|5.0|male|...|0.64|True|True|[]|False|False|False|0.02|False|[{'color': 'brown', 'confidence': 1.0}, {'colo...|
##### (一)- FaceList & Find Similar
###### 输入
```
import requests
```
#### 1、创建列表
##### faceListId
```
import requests
# 1、create 列表
# faceListId
faceListId = "chick_01"#自定义名称列表
create_facelists_url = "https://chick.cognitiveservices.azure.com/face/v1.0/facelists/{}" #填写url 详情见api文档
subscription_key = "22860f2ec4fa40668703f86a97b571b6"#api秘钥
assert subscription_key
headers = {
# Request headers
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': subscription_key,
}
data = {
"name":"网新周四下午课",#自定义名称
"userDate":"55人,30女,25男",
"recognitionModel":"recognition_03"
}
r_create = requests.put(create_facelists_url.format(faceListId), headers=headers, json=data)
r_create.content
```
###### 获得
```
b''
```
##### get facelist
###### 输入
```
# 检查你的facelist的信息
get_facelist_url = "https://chick.cognitiveservices.azure.com/face/v1.0/facelists/chick_01" # 最后一个为自己使用的facelistid
r_get_facelist = requests.get(get_facelist_url,headers=headers)
```
###### 获得
```
'persistedFaces': [], 'faceListId': 'chick_01', 'name': '网新周四下午课'
```
###### 1.添加人脸
```
add_face_url = "https://chick.cognitiveservices.azure.com/face/v1.0/facelists/chick_01/persistedfaces"
assert subscription_key
headers = {
# Request headers
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': subscription_key,
}
img_url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603472477147&di=6af53f0e4a6422d56f01e405ae65febd&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180811%2Fefb2c10f038b4749b5a3669f34f26e2c.jpeg"
params_add_face={
"userData":"jack"
}
r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={"url":img_url})
r_add_face.status_code
```
###### 获得
```
# 200
```
##### 检查facelist的信息
###### 输入
```
get_facelist_url = "https://chick.cognitiveservices.azure.com/face/v1.0/facelists/chick_01"
r_get_facelist = requests.get(get_facelist_url,headers=headers)
r_get_facelist.json()
```
###### 获得
```
{'persistedFaces': [{'persistedFaceId': '5d9e67fe-1efc-40c2-a47b-1fec6ed038a0',
'userData': 'jack'},
{'persistedFaceId': '2c1f2a3e-1be2-4cee-b86f-d0d2955ca12f',
'userData': '丘天惠'},
{'persistedFaceId': 'fcffffc5-4bb7-4861-bf2d-139f14b70ada',
'userData': '林嘉茵'},
{'persistedFaceId': '14b1db26-575e-4779-99ee-f25572d868b7',
'userData': '汤玲萍'},
{'persistedFaceId': '0f6c69cd-1b6f-4cdb-8c52-6632b130cb3a',
'userData': '曾雯燕'},
{'persistedFaceId': '746d0204-6ee8-4ae0-9cf9-acf01a3c4a45',
'userData': '谢依希'},
{'persistedFaceId': '15989467-2c13-4264-ad9a-462402c26d9f',
'userData': '杨悦聪'},
{'persistedFaceId': '08eb7b73-c8f1-43a8-89a5-7fddd0bf6247',
'userData': '刘瑜鹏'},
{'persistedFaceId': 'e623fda6-d2b6-4f02-a649-34cb477f6dfd',
'userData': '陈嘉仪'},
{'persistedFaceId': '9e649ccf-0f9e-495d-9015-33492f4c617a',
'userData': '徐旖芊'},
{'persistedFaceId': 'd2d389c9-fbd7-4bc8-b259-7e3f52598a2a',
'userData': '刘心如'},
{'persistedFaceId': '521195a5-d1eb-4409-8153-cd626e39c5a1',
'userData': '刘宇'},
{'persistedFaceId': '71d1a774-89b1-4272-913d-edd93573df6b',
'userData': '李婷'},
{'persistedFaceId': 'aebb7efd-61a2-43c2-94f3-fefa9d158ca0',
'userData': '黄智毅'},
{'persistedFaceId': '8f7c4888-4247-49c5-ad37-b05dbeeb893f',
'userData': '黄慧文'},
{'persistedFaceId': '2d837c33-fd3b-49b1-a6d7-94e9451118ae',
'userData': '张铭睿'},
{'persistedFaceId': '6ac2d219-5ffd-4e5f-aaef-33213e3f06c8',
'userData': '陈婷'},
{'persistedFaceId': 'ab5c923e-0119-45a8-a21b-e127e26302f1',
'userData': '洪可凡'},
{'persistedFaceId': 'ec4ece7d-9934-485b-8042-335eb013832e',
'userData': '卢继志'},
{'persistedFaceId': 'b90e5be6-bf70-4527-940f-38c09cd53181',
'userData': '张梓乐'},
{'persistedFaceId': '7886b65a-7ddb-4a58-8215-66453f06eb0b',
'userData': '丘天惠'},
{'persistedFaceId': '7a3e4931-1d82-4f60-84e7-8f5bd2be0985',
'userData': '林嘉茵'},
{'persistedFaceId': '1288d4a0-8122-48f2-9db6-a817a2126c89',
'userData': '汤玲萍'},
{'persistedFaceId': '87a6cc3a-8bcb-4887-bd50-f89e6e669f8a',
'userData': '曾雯燕'},
{'persistedFaceId': '2939667f-7cb9-40dc-8ba2-d74c40eb8209',
'userData': '谢依希'},
{'persistedFaceId': '5205885f-27a7-40d1-b0b7-f9657928a015',
'userData': '杨悦聪'},
{'persistedFaceId': '32996c2b-3e32-44b0-8d87-25ae7ed88a9e',
'userData': '周雨'},
{'persistedFaceId': '2211e0e4-ceba-490b-816a-1f30a38a64a4',
'userData': '刘瑜鹏'},
{'persistedFaceId': '40dc14c4-711d-4e9a-8239-5af68123f8fc',
'userData': '陈嘉仪'},
{'persistedFaceId': '46bbabc7-6b15-431b-a1e8-10d94c95a839',
'userData': '徐旖芊'},
{'persistedFaceId': 'be586d44-cdd6-438e-ae31-2f0cd3891022',
'userData': '刘心如'},
{'persistedFaceId': '368bdd82-2b02-43d0-ba24-4d5f83a5d8d6',
'userData': '刘宇'},
{'persistedFaceId': 'de22e58e-c15f-403a-b07e-c8945120fb0d',
'userData': '李婷'},
{'persistedFaceId': '99e90fcd-137a-4ab4-8271-80492699b4cc',
'userData': '黄智毅'},
{'persistedFaceId': 'a34f1193-302c-448b-bdbd-3609435faea7',
'userData': '黄慧文'},
{'persistedFaceId': '1ba88f00-00cb-4081-a2d6-ebf38024082a',
'userData': '张铭睿'},
{'persistedFaceId': 'b975dff7-6844-4ac0-b3d4-fc4beac11c44',
'userData': '陈婷'},
{'persistedFaceId': 'd51dc9ba-f0cf-411e-8a6d-1add7f5fdf0a',
'userData': '洪可凡'},
{'persistedFaceId': '1ae9a43f-7ad8-475f-b8a1-9c3e92695e48',
'userData': '卢继志'},
{'persistedFaceId': '771549af-94b7-4fff-965e-543f4cbfc76c',
'userData': '张梓乐'}],
'faceListId': 'chick_01',
'name': '网新周四下午课'}
```
###### 2.返回persistedFaceId
```
for i in faceId:
# print(i)
if i["userData"] == "林嘉茵":
faceId_02 = i['persistedFaceId']
faceId_02
```
###### 获得
```
'4a15a5df-2665-40b5-99ce-8b40be80091a'
```
##### 3.封装成函数方便添加图片/函数——可以重复使用相同的功能
###### 输入
```
def AddFace(img_url=str,userData=str):
add_face_url ="https://chick.cognitiveservices.azure.com/face/v1.0/facelists/chick_01/persistedfaces"
assert subscription_key
headers = {
# Request headers
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': subscription_key,
}
img_url = img_url
params_add_face={
"userData":userData
}
r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={"url":img_url})
return r_add_face.status_code
```
###### 获得
```
AddFace("http://huangjieqi.gitee.io/picture_storage/Autumnhui.jpg","丘天惠")
AddFace("http://huangjieqi.gitee.io/picture_storage/L-Tony-info.jpg","林嘉茵")
AddFace("http://huangjieqi.gitee.io/picture_storage/TLINGP.jpg","汤玲萍")
AddFace("http://huangjieqi.gitee.io/picture_storage/WenYanZeng.jpg","曾雯燕")
AddFace("http://huangjieqi.gitee.io/picture_storage/XIEIC.jpg","谢依希")
AddFace("http://huangjieqi.gitee.io/picture_storage/YuecongYang.png","杨悦聪")
AddFace("http://huangjieqi.gitee.io/picture_storage/Zoezhouyu.jpg","周雨")
AddFace("http://huangjieqi.gitee.io/picture_storage/crayon-heimi.jpg","刘瑜鹏")
AddFace("http://huangjieqi.gitee.io/picture_storage/jiayichen.jpg","陈嘉仪")
AddFace("http://huangjieqi.gitee.io/picture_storage/kg2000.jpg","徐旖芊")
AddFace("http://huangjieqi.gitee.io/picture_storage/liuxinrujiayou.jpg","刘心如")
AddFace("http://huangjieqi.gitee.io/picture_storage/liuyu19.png","刘宇")
AddFace("http://huangjieqi.gitee.io/picture_storage/ltco.jpg","李婷")
AddFace("http://huangjieqi.gitee.io/picture_storage/lucaszy.jpg","黄智毅")
AddFace("http://huangjieqi.gitee.io/picture_storage/pingzi0211.jpg","黄慧文")
AddFace("http://huangjieqi.gitee.io/picture_storage/shmimy-cn.jpg","张铭睿")
AddFace("http://huangjieqi.gitee.io/picture_storage/yichenting.jpg","陈婷")
AddFace("http://huangjieqi.gitee.io/picture_storage/coco022.jpg","洪可凡")
AddFace("http://huangjieqi.gitee.io/picture_storage/lujizhi.png","卢继志")
AddFace("http://huangjieqi.gitee.io/picture_storage/zzlhyy.jpg","张梓乐")
```
##### 4.检测人脸
```
face_api_url = 'https://chick.cognitiveservices.azure.com/face/v1.0/detect'
image_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603391956426&di=c8f7f01de1249fa11649d7e45ca04517&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180205%2F1e7e0127a60a481f86d7de0633ac42bf.jpeg'
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
# 请求参数
params = {
'returnFaceId': 'true',
'returnFaceLandmarks': 'false',
# 选择模型
'recognitionModel':'recognition_03',
'detectionModel':'detection_01',
# 可选参数,参考API文档
'returnFaceAttributes': '',
}
response = requests.post(face_api_url, params=params,headers=headers, json={"url": image_url})
response.json()
```
##### 5.返回人脸相似置信度
###### 输入
```
# Detect 检测人脸的id
# replace with the string from your endpoint URL
face_api_url = 'https://chick.cognitiveservices.azure.com/face/v1.0/detect'
# 请求正文
image_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603472477147&di=6af53f0e4a6422d56f01e405ae65febd&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180811%2Fefb2c10f038b4749b5a3669f34f26e2c.jpeg'
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
# 请求参数
params = {
'returnFaceId': 'true',
'returnFaceLandmarks': 'false',
# 选择model
'recognitionModel':'recognition_03',#此参数需与facelist参数一致
'detectionModel':'detection_01',
# 可选参数,请仔细阅读API文档
'returnFaceAttributes': '',
}
response = requests.post(face_api_url, params=params,
headers=headers, json={"url": image_url})
# json.dumps 将json--->字符串
response.json()
```
###### 获得
```
[{'faceId': '65109723-281a-4e9b-abfd-6ade9722bda7',
'faceRectangle': {'top': 75, 'left': 75, 'width': 133, 'height': 133}}]
```
###### 输入
```
findsimilars_url = "https://chick.cognitiveservices.azure.com/face/v1.0/findsimilars"
# 请求正文 faceID需要先检测一张照片获取
data_findsimilars = {
"faceId":"65109723-281a-4e9b-abfd-6ade9722bda7", #取上方的faceID
"faceListId": "chick_01",
"maxNumOfCandidatesReturned": 10,
"mode": "matchFace" #matchPerson #一种为验证模式,一种为相似值模式
}
r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)
r_findsimilars.json()
```
###### 获得
```
[{'persistedFaceId': '5d9e67fe-1efc-40c2-a47b-1fec6ed038a0',
'confidence': 1.0},
{'persistedFaceId': '68dca36b-255a-413e-a6b3-bc0f6afd4a6e',
'confidence': 1.0},
{'persistedFaceId': '14b1db26-575e-4779-99ee-f25572d868b7',
'confidence': 0.09491},
{'persistedFaceId': '1288d4a0-8122-48f2-9db6-a817a2126c89',
'confidence': 0.09491},
{'persistedFaceId': 'ce5e84b5-0247-4f68-9957-98a955c61748',
'confidence': 0.09491},
{'persistedFaceId': 'f7e14b14-7efe-485b-80de-5cb4ccbb9f7e',
'confidence': 0.09491},
{'persistedFaceId': '9e649ccf-0f9e-495d-9015-33492f4c617a',
'confidence': 0.09488},
{'persistedFaceId': '46bbabc7-6b15-431b-a1e8-10d94c95a839',
'confidence': 0.09488},
{'persistedFaceId': '2421d1ce-0cb7-4a63-ba8b-abe39722bed0',
'confidence': 0.09488},
{'persistedFaceId': '41684a27-add4-45e8-b791-5d417faba1e8',
'confidence': 0.09488}]
```
###### ·用Pandas简化数据与返回facelist数据
```
import pandas as pd
faceListId_df = pd.json_normalize(r_get_facelist.json()["persistedFaces"])# 升级pandas才能运行
faceListId_df
```
###### 获得

###### ·返回相似度数据
```
find_df = pd.json_normalize(r_findsimilars.json())# 升级pandas才能运行
find_df
```
###### 获得

###### ·合并
```
pd.merge(faceListId_df, find_df,how='inner', on='persistedFaceId').sort_values(by="confidence",ascending = False)
```
###### 获得

#### B.face ++
##### B-1 准备工作
```
api_secret = "cCgd0W4aKtbCdxQYy9DuxTcs1DwFPtk9"
api_key = '3hc0EXssRUfkJGEnnBBQPrzBVHcQaPyp' # 输入api_secret,api_key
```
##### B-2 创建人脸集合
###### 输入
```
import requests,json
display_name = "网新二班人脸集合" # 自定义集合名称
outer_id = "201023" #自定义标识
user_data = "52人,20男生,32女生" # 自定义用户信息
CreateFace_Url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/create"
payload = {
'api_key': api_key,
'api_secret': api_secret,
'display_name':display_name,
'outer_id':outer_id,
'user_data':user_data
}
r = requests.post(CreateFace_Url, params=payload)
r.json() #json转义
```
###### 获得
```
{'faceset_token': '37071d95016c1b2d81591a6f0c1681f2',
'time_used': 238,
'face_count': 0,
'face_added': 0,
'request_id': '1602153952,cd5f639d-734d-421e-912c-c4bca610f3cf',
'outer_id': '00001',
'failure_detail': []}
```
##### B-3 FaceSet GetDetail(获取人脸集合信息)
###### 输入
```
GetDetail_Url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail"
payload = {
'api_key': api_key,
'api_secret': api_secret,
'outer_id':outer_id,
}
r = requests.post(GetDetail_Url,params=payload)
r.json()
```
###### 获得
```
{'faceset_token': '8ae27be5f5b6487cad35babfd759dfd3',
'tags': '',
'time_used': 114,
'user_data': '52人,20男生,32女生',
'display_name': '网新二班人脸集合',
'face_tokens': [],
'face_count': 0,
'request_id': '1603431481,96e8f59c-89f5-4210-9ca3-039e5fc68b86',
'outer_id': '00001'}
```
##### B-4 FaceSet AddFace(增加人脸信息)
###### 输入
```
AddFace_url = " https://api-cn.faceplusplus.com/facepp/v3/faceset/addface"
payload = {
'api_key': api_key,
'api_secret': api_secret,
'faceset_token':'37071d95016c1b2d81591a6f0c1681f2',
'face_tokens':'b0407b9e803ebd39d511cd7956fd5bf5',
}
r = requests.post(AddFace_url,params=payload)
r.json()
```
###### 获得
```
{'time_used': 71,
'error_message': 'INVALID_FACESET_TOKEN',
'request_id': '1603431660,d8e0217f-2328-415f-ac5d-a1e9c25c18c5'}
```
##### B-5 FaceSet RemoveFace(移除人脸信息)
###### 输入
```
RemoveFace_url = " https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface"
payload = {
'api_key': api_key,
'api_secret': api_secret,
'faceset_token':'37071d95016c1b2d81591a6f0c1681f2',
'face_tokens':'b0407b9e803ebd39d511cd7956fd5bf5',
}
r = requests.post(RemoveFace_url,params=payload)
r.json()
```
###### 获得
```
{'time_used': 65,
'error_message': 'INVALID_FACESET_TOKEN',
'request_id': '1603431915,3da427af-569c-4a9d-a508-211a735451f2'}
```
##### B-6 FaceSet Update(更新人脸集合信息)
###### 输入
```
Update_url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/update"
payload = {
'api_key': api_key,
'api_secret': api_secret,
'faceset_token':'37071d95016c1b2d81591a6f0c1681f2',
'user_data':"53人,21男生,32女生",
}
r = requests.post(Update_url,params=payload)
r.json()
```
###### 获得
```
{'time_used': 70,
'error_message': 'INVALID_FACESET_TOKEN',
'request_id': '1603432104,a73fa86c-2ea5-428a-ab8a-a95df7ff9636'}
```
##### B-7 Compare Face(对比人脸相似度)
##### 方案一(直接对比)
###### 输入
```
liudehua01 = "https://gss0.baidu.com/9fo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/7c1ed21b0ef41bd57f7f20ff57da81cb39db3d89.jpg"
liudehua02 = "https://tse3-mm.cn.bing.net/th/id/OIP.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=Api&rs=1"
wangzulan = "https://tse3-mm.cn.bing.net/th/id/OIP.ZnXeGoVYT4jQudiPOGZn3QAAAA?pid=Api&rs=1"
r = requests.post(Compare_url,params=payload)
r.json()
```
###### 获得
```
{'faces1': [{'face_rectangle': {'width': 824,
'top': 871,
'left': 1114,
'height': 824},
'face_token': 'a1dabce83c86b20b68dcea537eae57c5'}],
'faces2': [{'face_rectangle': {'width': 86,
'top': 91,
'left': 65,
'height': 86},
'face_token': '359ace131af5848047ff86b4bd521c66'}],
'time_used': 1883,
'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},
'confidence': 26.085,
'image_id2': 'g6kg8zfyOouG6ftP+GvEfg==',
'image_id1': 'KIOXEC2V/MyL4zuopAcNig==',
'request_id': '1603432253,b0865971-7335-412b-bd8e-4857234b249f'}
```
##### 方案二 面部检测(获取face_token)
###### 输入
```Detect_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
img_url = liudehua01
payload = {
"image_url":img_url,
'api_key': api_key,
'api_secret': api_secret,
'return_attributes':'gender,age,smiling,emotion',
}
r = requests.post(Detect_url,params=payload)
r.json()
```
###### 获得
```
{'request_id': '1603432398,b494273f-c183-43bc-b5c3-82989e2187c6',
'time_used': 1488,
'faces': [{'face_token': '4ff18bc16a8e9664cc44a60aa3d7493e',
'face_rectangle': {'top': 871, 'left': 1114, 'width': 824, 'height': 824},
'attributes': {'gender': {'value': 'Male'},
'age': {'value': 59},
'smile': {'value': 99.998, 'threshold': 50.0},
'emotion': {'anger': 0.0,
'disgust': 0.047,
'fear': 0.0,
'happiness': 99.945,
'neutral': 0.0,
'sadness': 0.007,
'surprise': 0.0}}}],
'image_id': 'KIOXEC2V/MyL4zuopAcNig==',
'face_num': 1}
```
#### C.百度智能云
##### C-1 人脸检测
##### 输入
```
import requests
# client_id 为官网获取的API Key, client_secret 为官网获取的Secret Key
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'
client_id = "BWCBPvb4so5AWMwD2vuwDkfT"
client_secret = "qq2brubZaRHBV5M7fYdruBlW7XdCZ3ln"
response = requests.get(host.format(client_id, client_secret))
if response:
print(response.json())
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
params = "{\"image\":\"027d8308a2ec665acb1bdf63e513bcb9\",\"image_type\":\"FACE_TOKEN\",\"face_field\":\"faceshape,facetype\"}"
```
##### 获得token
```
{'refresh_token': '25.a2af4330fbdca0089c0a66a0ad374c90.315360000.1918817380.282335-22866714', 'expires_in': 2592000, 'session_key': '9mzdCXDHWf7xFVuYxV/BmL3KshKrOs668v42SohQ0XfHHl0YYJzndfzzCq7djZa6KLD47mvL6QTyDkttQ08VHqH78S5sdQ==', 'access_token': '24.2fe8999800076d5c0940c1b9dab0bc76.2592000.1606049380.282335-22866714', 'scope': 'public brain_all_scope vis-faceverify_faceverify_h5-face-liveness vis-faceverify_FACE_V3 vis-faceverify_idl_face_merge vis-faceverify_FACE_EFFECT vis-faceverify_face_feature_sdk wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component', 'session_secret': '564b991bbb00958466f779ab2b6530af'}
```
##### 输入
```
access_token = '24.2fe8999800076d5c0940c1b9dab0bc76.2592000.1606049380.282335-22866714'
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/json'}
response = requests.post(request_url, data=params, headers=headers)
if response:
print (response.json())
```
##### 获得
```
{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 2594156505250, 'timestamp': 1603457424, 'cached': 0, 'result': {'face_num': 1, 'face_list': [{'face_token': '027d8308a2ec665acb1bdf63e513bcb9', 'location': {'left': 97.35, 'top': 83.68, 'width': 99, 'height': 89, 'rotation': -13}, 'face_probability': 1, 'angle': {'pitch': -5.8, 'roll': -14.62, 'yaw': -16.19}, 'face_shape': {'type': 'round', 'probability': 0.43}, 'face_type': {'type': 'human', 'probability': 1}}]}}
```
#### C-2 人脸对比
##### 输入
```
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/match"
params = "[{\"image\": \"https://https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1701833838,2036817484&fm=26&gp=0.jpg\", \"image_type\": \"URL\", \"face_type\": \"CERT\", \"quality_control\": \"LOW\"}, {\"image\": \"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603015508446&di=eee4e2c852d804bc3b80a719df3df9ef&imgtype=0&src=http%3A%2F%2Fimg2-cloud.itouchtv.cn%2Ftvtouchtv%2Fimage%2F20170914%2F1505363583630378.jpg\", \"image_type\": \"URL\", \"face_type\": \"LIVE\", \"quality_control\": \"LOW\"}]"
access_token = '24.2fe8999800076d5c0940c1b9dab0bc76.2592000.1606049380.282335-22866714' # 调用鉴权接口获取的token
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/json'}
response = requests.post(request_url, data=params, headers=headers)
response.json()
```
##### 获得
```
{'error_code': 0,
'error_msg': 'SUCCESS',
'log_id': 2010017575056,
'timestamp': 1603458008,
'cached': 0,
'result': {'score': 78.07155609,
'face_list': [{'face_token': '1d421751f4a377d403f424a02fce6f47'},
{'face_token': '93d2501e1cb1685c26107d0b19cc854a'}]}}
```
#### C-3人脸集合
##### 第一步
##### 输入
```
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/add"
params = "{\"group_id\":\"group2\"}"
access_token = '24.2fe8999800076d5c0940c1b9dab0bc76.2592000.1606049380.282335-22866714' # 调用鉴权接口获取的token
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/json'}
response = requests.post(request_url, data=params, headers=headers)
response.json()
```
##### 获得
```
{'error_code': 0,
'error_msg': 'SUCCESS',
'log_id': 7905201151589,
'timestamp': 1603458220,
'cached': 0,
'result': None}
```
##### 第二步
##### 输入
```
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add"
params = "{\"image\":\"91cf0a5aa45b0371989e56760b30548c\",\"image_type\":\"FACE_TOKEN\",\"group_id\":\"group1\",\"user_id\":\"user1\",\"user_info\":\"abc\",\"quality_control\":\"LOW\",\"liveness_control\":\"NORMAL\"}"
access_token = '24.2fe8999800076d5c0940c1b9dab0bc76.2592000.1606049380.282335-22866714' # 调用鉴权接口获取的token
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/json'}
response = requests.post(request_url, data=params, headers=headers)
response.json()
```
##### 获得
```
{'error_code': 0,
'error_msg': 'SUCCESS',
'log_id': 9489551565158,
'timestamp': 1603458383,
'cached': 0,
'result': {'face_token': '91cf0a5aa45b0371989e56760b30548c',
'location': {'left': 186.93,
'top': 99.21,
'width': 121,
'height': 118,
'rotation': 1}}}
```
#### 第三步
##### 输入
```
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add"
params = "{\"image\":\"91cf0a5aa45b0371989e56760b30548c\",\"image_type\":\"FACE_TOKEN\",\"group_id\":\"group1\",\"user_id\":\"user1\",\"user_info\":\"abc\",\"quality_control\":\"LOW\",\"liveness_control\":\"NORMAL\"}"
access_token = '24.2fe8999800076d5c0940c1b9dab0bc76.2592000.1606049380.282335-22866714' # 调用鉴权接口获取的token
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/json'}
response = requests.post(request_url, data=params, headers=headers)
response.json()
```
##### 获得
```
{'error_code': 0,
'error_msg': 'SUCCESS',
'log_id': 9489551565158,
'timestamp': 1603458383,
'cached': 0,
'result': {'face_token': '91cf0a5aa45b0371989e56760b30548c',
'location': {'left': 186.93,
'top': 99.21,
'width': 121,
'height': 118,
'rotation': 1}}}
```
***
### 二.计算机视觉(Azure API)所有的功能
#### A 计算机视觉
* ##### 学习并完成所有Azure computer version 的API调用
* ##### 分析远程图像
* ##### 分析本地图片
* ##### 生成缩略图
* ##### 提取印刷体文本和手写文本
#### A-1 分析远程图像
##### 输入
```
import requests
# If you are using a Jupyter notebook, uncomment the following line.
%matplotlib inline
import matplotlib.pyplot as plt
import json
from PIL import Image
from io import BytesIO
# Add your Computer Vision subscription key and endpoint to your environment variables.
# if 'COMPUTER_VISION_SUBSCRIPTION_KEY' in os.environ:
# subscription_key = os.environ['COMPUTER_VISION_SUBSCRIPTION_KEY']
# else:
# print("\nSet the COMPUTER_VISION_SUBSCRIPTION_KEY environment variable.\n**Restart your shell or IDE for changes to take effect.**")
# sys.exit()
endpoint = "https://shijue01.cognitiveservices.azure.com/" (计算机视觉个人终结点)
# if 'COMPUTER_VISION_ENDPOINT' in os.environ:
# endpoint = os.environ['COMPUTER_VISION_ENDPOINT']
subscription_key = "8a2abe4e786c40c1936efe083f794112" (计算机视觉秘钥)
# base url
analyze_url = endpoint+ "vision/v2.1/analyze"
# Set image_url to the URL of an image that you want to analyze.
image_url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603444862064&di=a64f57fc7d2973009b1ae8d538bf09d9&imgtype=0&src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F2018-09-10%2F5b95d4a16d7d4.jpg"
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
# 参数
params = {'visualFeatures': 'Categories,Description,Color'}
# 请求主体bodyk
data = {'url': image_url}
response = requests.post(analyze_url, headers=headers,params=params, json=data)
response.raise_for_status()
# The 'analysis' object contains various fields that describe the image. The most
# relevant caption for the image is obtained from the 'description' property.
analysis = response.json()
print(json.dumps(response.json()))
image_caption = analysis["description"]["captions"][0]["text"].capitalize()
# Display the image and overlay it with the caption.
image = Image.open(BytesIO(requests.get(image_url).content))
plt.imshow(image)
plt.axis("off")
_ = plt.title(image_caption, size="x-large", y=-0.1)
plt.show()
```
##### 获得
```
{"categories": [{"name": "building_", "score": 0.77734375, "detail": {"landmarks": []}}, {"name": "outdoor_", "score": 0.00390625, "detail": {"landmarks": []}}], "color": {"dominantColorForeground": "Blue", "dominantColorBackground": "Blue", "dominantColors": ["Blue"], "accentColor": "09458A", "isBwImg": false, "isBWImg": false}, "description": {"tags": ["outdoor", "building", "water", "light", "large", "city", "front", "tower", "clock", "sitting", "street", "boat", "tall", "green", "lit", "traffic", "standing", "parked", "ocean", "night", "river", "flying", "blue", "sign", "group", "kite", "beach"], "captions": [{"text": "a large tower in a city", "confidence": 0.7080765374550395}]}, "requestId": "056225a6-3cf6-4543-8bf4-39390a6112e7", "metadata": {"height": 1200, "width": 1920, "format": "Jpeg"}}
```

#### A-2 分析本地图片
##### 输入
```
import os
import sys
import requests
# If you are using a Jupyter notebook, uncomment the following line.
%matplotlib inline
import matplotlib.pyplot as plt
from PIL import Image
from io import BytesIO
# Add your Computer Vision subscription key and endpoint to your environment variables.
# if 'COMPUTER_VISION_SUBSCRIPTION_KEY' in os.environ:
# subscription_key = os.environ['COMPUTER_VISION_SUBSCRIPTION_KEY']
# else:
# print("\nSet the COMPUTER_VISION_SUBSCRIPTION_KEY environment variable.\n**Restart your shell or IDE for changes to take effect.**")
# sys.exit()
# if 'COMPUTER_VISION_ENDPOINT' in os.environ:
# endpoint = os.environ['COMPUTER_VISION_ENDPOINT']
# analyze_url = endpoint + "vision/v2.1/analyze"
# Set image_path to the local path of an image that you want to analyze.
image_path = '/Users/86132/Desktop/学习文件/API/api_week5_work/本地图片.jpg' # 插入本地图片链接
# Read the image into a byte array
image_data = open(image_path, "rb").read()
headers = {'Ocp-Apim-Subscription-Key': "8a2abe4e786c40c1936efe083f794112",
'Content-Type': 'application/octet-stream'}
params = {'visualFeatures': 'Categories,Description,Color'}
response = requests.post(
analyze_url, headers=headers, params=params, data=image_data)
response.raise_for_status()
# The 'analysis' object contains various fields that describe the image. The most
# relevant caption for the image is obtained from the 'description' property.
analysis = response.json()
print(analysis)
image_caption = analysis["description"]["captions"][0]["text"].capitalize()
# Display the image and overlay it with the caption.
image = Image.open(BytesIO(image_data))
plt.imshow(image)
plt.axis("off")
_ = plt.title(image_caption, size="x-large", y=-0.1)
```
##### 获得
```
{'categories': [{'name': 'others_', 'score': 0.00390625}, {'name': 'outdoor_', 'score': 0.0078125, 'detail': {'landmarks': []}}], 'color': {'dominantColorForeground': 'Blue', 'dominantColorBackground': 'Grey', 'dominantColors': ['Grey', 'Blue'], 'accentColor': '203F6D', 'isBwImg': False, 'isBWImg': False}, 'description': {'tags': ['outdoor', 'snow', 'nature', 'water', 'tree', 'man', 'covered', 'standing', 'skiing', 'field', 'beach', 'clouds', 'sun', 'riding', 'people', 'hill', 'mountain', 'flying', 'slope', 'air', 'walking', 'woman', 'ocean', 'sunset', 'group', 'board', 'sand'], 'captions': [{'text': 'a tree covered in snow', 'confidence': 0.8273600224194236}]}, 'requestId': 'fad9519a-bc67-41fc-a375-435c9132fedc', 'metadata': {'height': 313, 'width': 500, 'format': 'Jpeg'}}
```

#### A-3 生成缩略图
##### 输入
```
import os
import sys
import requests
# If you are using a Jupyter notebook, uncomment the following line.
# %matplotlib inline
import matplotlib.pyplot as plt
from PIL import Image
from io import BytesIO
# Add your Computer Vision subscription key and endpoint to your environment variables.
# if 'COMPUTER_VISION_SUBSCRIPTION_KEY' in os.environ:
# subscription_key = os.environ['COMPUTER_VISION_SUBSCRIPTION_KEY']
# else:
# print("\nSet the COMPUTER_VISION_SUBSCRIPTION_KEY environment variable.\n**Restart your shell or IDE for changes to take effect.**")
# sys.exit()
# if 'COMPUTER_VISION_ENDPOINT' in os.environ:
# endpoint = os.environ['COMPUTER_VISION_ENDPOINT']
thumbnail_url = "https://api-computervvsion-cyl.cognitiveservices.azure.com/" + "vision/v2.1/generateThumbnail"
# Set image_url to the URL of an image that you want to analyze.
image_url = "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2096015808,1580004639&fm=26&gp=0.jpg" #插入图片链接
headers = {'Ocp-Apim-Subscription-Key': "dd748cf10bf9404399e5416d9399e218"}
params = {'width': '100', 'height': '100', 'smartCropping': 'true'}
data = {'url': image_url}
response = requests.post(thumbnail_url, headers=headers,
params=params, json=data)
response.raise_for_status()
thumbnail = Image.open(BytesIO(response.content))
# Display the thumbnail.
plt.imshow(thumbnail)
plt.axis("off")
# Verify the thumbnail size.
print("Thumbnail is {0}-by-{1}".format(*thumbnail.size))
```
##### 获得
```
Thumbnail is 100-by-100
```

#### A-4 提取图片文本
##### 输入
```
import os
import sys
import requests
%matplotlib inline
import matplotlib.pyplot as plt
from PIL import Image
from io import BytesIO
landmark_analyze_url = "https://shijue01.cognitiveservices.azure.com/" #插入计算机视觉终结点链接+ "vision/v3.1/models/landmarks/analyze"
# Set image_url to the URL of an image that you want to analyze.
image_url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603464156547&di=474b33d715dedb989807b13961f33369&imgtype=0&src=http%3A%2F%2Fyouimg1.c-ctrip.com%2Ftarget%2F10040k000000c77ziAC41.jpg"
# 插入图片链接
headers = {'Ocp-Apim-Subscription-Key': "8a2abe4e786c40c1936efe083f794112"} #插入计算机视觉秘钥
params = {'model': 'landmarks'}
data = {'url': image_url}
response = requests.post(
landmark_analyze_url, headers=headers, params=params, json=data)
response.raise_for_status()
# The 'analysis' object contains various fields that describe the image. The
# most relevant landmark for the image is obtained from the 'result' property.
analysis = response.json()
assert analysis["result"]["landmarks"] is not []
print(analysis)
landmark_name = analysis["result"]["landmarks"][0]["name"].capitalize()
# Display the image and overlay it with the landmark name.
image = Image.open(BytesIO(requests.get(image_url).content))
plt.imshow(image)
plt.axis("off")
_ = plt.title(landmark_name, size="x-large", y=-0.1)
plt.show()
```
##### 获得
```
{'result': {'landmarks': [{'name': 'Leaning Tower of Pisa', 'confidence': 1.0}]}, 'requestId': '9cb7f985-a2fb-4989-9733-5beaddde2741', 'metadata': {'height': 1280, 'width': 1250, 'format': 'Jpeg'}}
```

***
### 三.学习心得(总结)
#### 1.GitHub education pack
##### GitHub 学生包的祖册流程以及注意事项
* 百度搜索github education pack进入学生包页面,点击右上角蓝色get your pack进入GitHub登陆页面
* 进行GitHub登陆并进行资料填写后,在一至两天内会收到认证成功邮件
* 在使用GitHub登陆微软账号时,注意微软账户与GitHub账户主邮箱都要设置为学生邮箱
* 认证成功通过后,重新进入GitHub education pack网页,下拉至azure框,点击进入azure
* 若可使用azure,则进入页面后不会有填写验证学生邮箱等要求,仅有同意协议要求
* 若出现验证学生邮箱页面,则停止尝试,多次尝试后账号可能被封停
#### 2.api使用心得
##### (1)在azure使用过程中,自行学习[人脸文档](https://docs.microsoft.com/zh-cn/azure/cognitive-services/face/)以及[人脸api v1.0文档](https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236),从其中学习人脸识别代码填写以及学习反馈错误时的原因与如何处理。
##### (2)多尝试不同平台的api人脸识别技术,对比其中不同点,例如[face++](https://www.faceplusplus.com.cn/);[百度智能云](https://cloud.baidu.com/campaign/Promotionai/index.html?track=cp:npinzhuan|pf:pc|pp:npinzhuan-biaoti|pu:wenzineirong|ci:|kw:10014512)。
##### (3)api功能不仅仅只用于人脸检测,其功能多种多样,其中包括人脸集合、人脸对比、图片信息获取等功能,在学习人脸识别之余,更应该多尝试其不同功能,提升自身对于api使用熟练度。
#### 3.总结
##### api作为一种功能强大的智能系统,在未来的发展拥有更大的潜力与优势,学习并精通api的使用,能使我们在未来的工作中拥有更大的优势,在我们学习人脸识别的同时,也要学习api的其他功能,以此加深对api的了解以及提升自身的使用熟练度。
##### api与python中间存在互相提升的关系,在api的学习中我们能够提升对python的理解能力,python的学习也能使我们更加熟练的使用api。