# API-work **Repository Path**: he_lai/api-work ## Basic Information - **Project Name**: API-work - **Description**: No description available - **Primary Language**: Unknown - **License**: ISC - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-10-23 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # API-work # API 作业-1 * [一、人脸识别](#face) * [二、计算机视觉](#vision) * [三、学习心得](#learn) ---

一、人脸识别

### Azure 参考:[Azure 人脸识别文档](https://docs.microsoft.com/zh-cn/rest/api/cognitiveservices/face/facelist/create) - Face Detect ```python # A-1 面部检测 import requests import json # set to your own subscription key value subscription_key = 'aa3f3860bc894397a5018c908cbb79ff' assert subscription_key # replace with the string from your endpoint URL face_api_url = 'https://api-hr.cognitiveservices.azure.com/face/v1.0/detect' # 请求正文body image_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603820202732&di=86272f613bbc0793a37c8afcabc088df&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202007%2F30%2F20200730175754_rhdsN.thumb.200_200_c.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 response # 返回状态码 ``` ```python # bytes or string 编码不同 ***** encoding ="utf-8" # response.text # response.content results = response.json() results [{'faceId': 'bf345c8a-376d-473a-8f57-75e0699ea576', 'faceRectangle': {'top': 40, 'left': 74, 'width': 58, 'height': 58}, 'faceAttributes': {'smile': 0.604, 'headPose': {'pitch': 0.7, 'roll': 4.8, 'yaw': 14.9}, 'gender': 'female', 'age': 23.0, 'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0}, 'glasses': 'NoGlasses', 'emotion': {'anger': 0.0, 'contempt': 0.003, 'disgust': 0.0, 'fear': 0.0, 'happiness': 0.604, 'neutral': 0.392, 'sadness': 0.001, 'surprise': 0.0}, 'blur': {'blurLevel': 'low', 'value': 0.0}, 'exposure': {'exposureLevel': 'goodExposure', 'value': 0.75}, 'noise': {'noiseLevel': 'medium', 'value': 0.4}, 'makeup': {'eyeMakeup': True, 'lipMakeup': True}, 'accessories': [], 'occlusion': {'foreheadOccluded': False, 'eyeOccluded': False, 'mouthOccluded': False}, 'hair': {'bald': 0.09, 'invisible': False, 'hairColor': [{'color': 'brown', 'confidence': 0.9}, {'color': 'red', 'confidence': 0.85}, {'color': 'other', 'confidence': 0.54}, {'color': 'black', 'confidence': 0.42}, {'color': 'blond', 'confidence': 0.31}, {'color': 'gray', 'confidence': 0.05}, {'color': 'white', 'confidence': 0.0}]}}}] ``` ```python # A-2 json转译 results = response.json() results [{'faceId': 'bf345c8a-376d-473a-8f57-75e0699ea576', 'faceRectangle': {'top': 40, 'left': 74, 'width': 58, 'height': 58}, 'faceAttributes': {'smile': 0.604, 'headPose': {'pitch': 0.7, 'roll': 4.8, 'yaw': 14.9}, 'gender': 'female', 'age': 23.0, 'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0}, 'glasses': 'NoGlasses', 'emotion': {'anger': 0.0, 'contempt': 0.003, 'disgust': 0.0, 'fear': 0.0, 'happiness': 0.604, 'neutral': 0.392, 'sadness': 0.001, 'surprise': 0.0}, 'blur': {'blurLevel': 'low', 'value': 0.0}, 'exposure': {'exposureLevel': 'goodExposure', 'value': 0.75}, 'noise': {'noiseLevel': 'medium', 'value': 0.4}, 'makeup': {'eyeMakeup': True, 'lipMakeup': True}, 'accessories': [], 'occlusion': {'foreheadOccluded': False, 'eyeOccluded': False, 'mouthOccluded': False}, 'hair': {'bald': 0.09, 'invisible': False, 'hairColor': [{'color': 'brown', 'confidence': 0.9}, {'color': 'red', 'confidence': 0.85}, {'color': 'other', 'confidence': 0.54}, {'color': 'black', 'confidence': 0.42}, {'color': 'blond', 'confidence': 0.31}, {'color': 'gray', 'confidence': 0.05}, {'color': 'white', 'confidence': 0.0}]}}}] ``` ```python # 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| |:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:|:--:| |bf345c8a-376d-473a-8f57-75e0699ea576|40|74|58|58|0.604|0.7|4.8|14.9|female|...|0.4|True|True|[]|0.0|False|False|False|0.09|False|[{'color': 'brown', 'confidence': 0.9}, {'colo...| ``` 1 rows × 38 columns ``` - FaceList & Find Similar ```python import requests # 1.创建人脸列表 faceListId = "list_004" # 设置人脸列表ID create_facelists_url ="https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}" # URL(见API文档) subscription_key ="aa3f3860bc894397a5018c908cbb79ff" # 密钥 # 请求头 headers = { 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': subscription_key, } # 相关信息及识别模型的选用 data = { "name": "sample_list", "userData": "相册", "recognitionModel": "recognition_03" } # 发送请求 r_create = requests.put(create_facelists_url.format(faceListId), headers=headers, json=data) r_create.content # b'' # 创建成功 # 2.添加人脸 add_face_url ="https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedfaces" assert subscription_key headers = { 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': subscription_key, } # 人脸相片地址 img_url ="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=781918665,2083273991&fm=26&gp=0.jpg" # 人脸数据 params_add_face={ "userData":"joy" } # 发送请求 r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={"url":img_url}) r_add_face.status_code # 200 r_add_face.json() # 返回persistedFaceId # {'persistedFaceId': '33bc32bc-2ffe-486f-9ee8-a86837911f50'} # 封装成函数方便添加图片 def AddFace(img_url=str,userData=str): add_face_url ="https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}/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","张梓乐") # 3.检测人脸 face_api_url = 'https://api-hjq.cognitiveservices.azure.com/face/v1.0/detect' image_url = 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=781918665,2083273991&fm=26&gp=0.jpg' headers = {'Ocp-Apim-Subscription-Key': subscription_key} # 请求参数 params = { 'returnFaceId': 'true', 'returnFaceLandmarks': 'false', # 选择模型 'recognitionModel':'recognition_03',#此参数需与facelist参数一致 'detectionModel':'detection_01', # 可选参数,请仔细阅读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}) response.json() # 4.返回人脸相似置信度 findsimilars_url = "https://api-hjq.cognitiveservices.azure.com/face/v1.0/findsimilars" # 请求正文 faceID需要先检测一张照片获取 data_findsimilars = { "faceId":"e5a9b7a6-8d5e-4b81-829c-1ba94a853e52", #取上方的faceID "faceListId": "list_003", "maxNumOfCandidatesReturned": 10, "mode": "matchFace" #matchPerson #一种为验证模式,一种为相似值模式 } r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars) r_findsimilars.json() # 查看列表 get_facelist_url = "https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}" r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers) r_get_facelist.json() # 用Pandas简化数据 import pandas as pd # 返回facelist数据 adf = pd.json_normalize(r_get_facelist.json()["persistedFaces"]) adf # 返回相似度数据 bdf = pd.json_normalize(r_findsimilars.json())# 升级pandas才能运行 bdf #合并 pd.merge(adf, bdf,how='inner', on='persistedFaceId').sort_values(by="confidence",ascending = False) # 5.删除人脸/人脸列表 faceListId = "list_004" # 需要删除的人脸列表 # 删除列表内人脸 delete_face_url = "https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedfaces/{}" assert subscription_key # 获取上面的persistedFaceId persistedFaceId = r_add_face.json()["persistedFaceId"] headers = { 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': subscription_key, } # 注意requests请求为delete r_delete_face = requests.delete(delete_face_url.format(faceListId,persistedFaceId),headers=headers) # 删除人脸列表 delete_facelist_url = "https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}" assert subscription_key headers = { 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': subscription_key, } r_delete_facelist = requests.delete(delete_facelist_url.format(faceListId),headers=headers) ``` ### Face++ 参考:[Face++ 人脸识别文档](https://console.faceplusplus.com.cn/documents/4888391) - Face Detect ### 1.导入模块 ```python import requests import json ``` ### 2.输入face++官网给的api_secret和api_key ```python api_secret = "vGEgDqiSEbiPlcEx0ggY8TP9Ch_7n0lh" api_key = '_fV9pDujMV301iO8z0PP7tMpPulyCaMl' ``` ### 3.detect face 添加一张人脸的图片,检测人脸 ```python api_url = "https://api-cn.faceplusplus.com/facepp/v3/detect" img_url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603534564342&di=64a2b527aed7e83ff904f01950b22940&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201911%2F17%2F20191117195100_vtwef.thumb.400_0.jpg" headers = { 'Content-Type': 'application/json', } payload = { "image_url":img_url, 'api_key': api_key, 'api_secret': api_secret, 'return_attributes':'gender,age,smiling,emotion', } r = requests.post(api_url, params=payload, headers=headers) r.status_code 200 # 返回状态码 ``` ```python # 返回人脸信息 results = r.json() results {'request_id': '1603524562,9c3d0b36-e7f2-419f-a547-1599ff3a9bde', 'time_used': 278, 'faces': [{'face_token': '1d5d6cc2613425ddf461fc23971f5397', 'face_rectangle': {'top': 70, 'left': 137, 'width': 178, 'height': 178}, 'attributes': {'gender': {'value': 'Female'}, 'age': {'value': 23}, 'smile': {'value': 0.0, 'threshold': 50.0}, 'emotion': {'anger': 0.013, 'disgust': 0.016, 'fear': 0.013, 'happiness': 0.117, 'neutral': 81.228, 'sadness': 12.424, 'surprise': 6.188}}}], 'image_id': '5ECQAfB52+R8LP4JjiSNNw==', 'face_num': 1} ``` ### 4.compare face 创建一个人脸的集合,存储人脸标识face_token一个FaceSet能够存储10000个face_token。 ```python api_secret = "vGEgDqiSEbiPlcEx0ggY8TP9Ch_7n0lh" api_key = '_fV9pDujMV301iO8z0PP7tMpPulyCaMl' display_name = "相册" # 人脸集合的名字 outer_id = "zhaolusi" # 账号下全局唯一的FaceSet自定义标识,可以用来管理 FaceSet 对象。 api_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, } r = requests.post(api_url, params=payload) r.json() # 返回人脸集合中的信息 {'faceset_token': 'e928099b1defddbf8f0f4bc7fb1bd3e8', 'time_used': 184, 'face_count': 0, 'face_added': 0, 'request_id': '1603524720,e6fdd753-9972-4ea0-a0ec-36fd8e457e24', 'outer_id': 'zhaolusi', 'failure_detail': []} ``` ### 5.FaceSet GetDetail 获取一个FaceSet的所有信息,包括此FaceSet的faceset_token,outer_id,display_name的信息,以及此FaceSet中存放的face_token数量与列表。 ```python api_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(api_url,params=payload) r.json() # 返回信息 {'faceset_token': 'e928099b1defddbf8f0f4bc7fb1bd3e8', 'tags': '', 'time_used': 95, 'user_data': '', 'display_name': '相册', 'face_tokens': [], 'face_count': 0, 'request_id': '1603524748,e936253a-b884-4c85-922e-39fbb517bd26', 'outer_id': 'zhaolusi'} ``` ### 6.FaceSet AddFace 为已经创建好的FaceSet添加人脸标识face_token. ```python api_url = " https://api-cn.faceplusplus.com/facepp/v3/faceset/addface" payload = { 'api_key': api_key, 'api_secret': api_secret, 'faceset_token':'e928099b1defddbf8f0f4bc7fb1bd3e8', 'face_tokens':'1d5d6cc2613425ddf461fc23971f5397', } r = requests.post(api_url,params=payload) r.json() {'faceset_token': 'e928099b1defddbf8f0f4bc7fb1bd3e8', 'time_used': 674, 'face_count': 1, 'face_added': 1, 'request_id': '1603524919,dba413a0-6d56-43ed-9f31-3e37d9beac8c', 'outer_id': 'zhaolusi', 'failure_detail': []} ``` ### 7.FaceSet RemoveFace 移除一个FaceSet中的某些或者全部face_token ```python api_url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface" payload = { 'api_key': api_key, 'api_secret': api_secret, 'faceset_token':'e928099b1defddbf8f0f4bc7fb1bd3e8', 'face_tokens':'1d5d6cc2613425ddf461fc23971f5397', } r = requests.post(api_url,params=payload) r.json() {'faceset_token': 'e928099b1defddbf8f0f4bc7fb1bd3e8', 'face_removed': 1, 'time_used': 201, 'face_count': 0, 'request_id': '1603525024,628ce3e6-c789-4ebe-bd3b-b40933d74df0', 'outer_id': 'zhaolusi', 'failure_detail': []} ``` ### 8.FaceSet Update 更新人脸集合的属性 ```python api_url = "https://api-cn.faceplusplus.com/facepp/v3/faceset/update" payload = { 'api_key': api_key, 'api_secret': api_secret, 'faceset_token':'e928099b1defddbf8f0f4bc7fb1bd3e8', 'display_name':"相册", } r = requests.post(api_url,params=payload) r.json() {'faceset_token': 'e928099b1defddbf8f0f4bc7fb1bd3e8', 'request_id': '1603525060,139c0bb5-e6b6-47d6-a380-1e7e5f50cdf2', 'time_used': 72, 'outer_id': 'zhaolusi'} ``` ### 9.1检测两张需要对比的人脸,获得face_token ```python api_secret = "vGEgDqiSEbiPlcEx0ggY8TP9Ch_7n0lh" api_key = '_fV9pDujMV301iO8z0PP7tMpPulyCaMl' api_url = "https://api-cn.faceplusplus.com/facepp/v3/detect" zrn = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603535266559&di=6f8b88ca1a5cedb6eced6ac4385d8a7a&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202009%2F18%2F20200918231607_8d90a.thumb.1000_0.jpg" headers = { 'Content-Type': 'application/json', } payload = { "image_url":zrn, 'api_key': api_key, 'api_secret': api_secret, 'return_attributes':'gender,age,smiling,emotion', } r = requests.post(api_url, params=payload, headers=headers) r.status_code 200 results = r.json() results {'request_id': '1603525219,de0b42f9-6cb2-4410-b4fc-3ae96013919b', 'time_used': 391, 'faces': [{'face_token': 'c18006563b4aea01921f741820576f37', 'face_rectangle': {'top': 195, 'left': 329, 'width': 334, 'height': 334}, 'attributes': {'gender': {'value': 'Female'}, 'age': {'value': 32}, 'smile': {'value': 0.092, 'threshold': 50.0}, 'emotion': {'anger': 0.006, 'disgust': 0.022, 'fear': 0.006, 'happiness': 0.447, 'neutral': 96.478, 'sadness': 0.042, 'surprise': 2.999}}}], 'image_id': 'ylMBdvtY/tw4R/STQQXCUw==', 'face_num': 1} ``` ### 9.2 第二张人脸图片 ```python api_secret = "vGEgDqiSEbiPlcEx0ggY8TP9Ch_7n0lh" api_key = '_fV9pDujMV301iO8z0PP7tMpPulyCaMl' api_url = "https://api-cn.faceplusplus.com/facepp/v3/detect" zls = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603535396449&di=57c3051fe091fe0e8dd82f191efbc9f7&imgtype=0&src=http%3A%2F%2Fimgsa.baidu.com%2Fbaike%2Fpic%2Fitem%2F267f9e2f07082838da47ac79b799a9014c08f111.jpg" headers = { 'Content-Type': 'application/json', } payload = { "image_url":zls, 'api_key': api_key, 'api_secret': api_secret, 'return_attributes':'gender,age,smiling,emotion', } r = requests.post(api_url, params=payload, headers=headers) r.status_code 200 results = r.json() results {'request_id': '1603525363,a67648bb-fdc9-4dcc-8b7e-8abba4c5c5ba', 'time_used': 673, 'faces': [{'face_token': '5118fbc4f0cf6cd435fa30da8a7a9f1c', 'face_rectangle': {'top': 178, 'left': 503, 'width': 464, 'height': 464}, 'attributes': {'gender': {'value': 'Female'}, 'age': {'value': 22}, 'smile': {'value': 96.212, 'threshold': 50.0}, 'emotion': {'anger': 0.008, 'disgust': 0.008, 'fear': 0.018, 'happiness': 95.569, 'neutral': 4.263, 'sadness': 0.123, 'surprise': 0.011}}}], 'image_id': 'v0yGX9B9xjPnyVRNLDe/QQ==', 'face_num': 1} ``` ### 10.Compare Face 将两个人脸进行比对,来判断是否为同一个人,返回比对结果置信度和不同误识率下的阈值. ```python api_secret = "vGEgDqiSEbiPlcEx0ggY8TP9Ch_7n0lh" api_key = '_fV9pDujMV301iO8z0PP7tMpPulyCaMl' api_url = "https://api-cn.faceplusplus.com/facepp/v3/compare" zls = '5118fbc4f0cf6cd435fa30da8a7a9f1c' zrn = 'c18006563b4aea01921f741820576f37' # 第二张人脸图片的url payload ={ 'api_key': api_key, 'api_secret': api_secret, 'face_token1': zls, 'face_token2': zrn } r = requests.post(api_url,params=payload) r.json() {'confidence': 38.645, #相似度值 'request_id': '1603525513,07aa8924-0bd5-4880-a9a7-3ad3c6ab142f', 'time_used': 435, 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101}} ``` ### 百度智能云 参考:[百度智能云人脸识别文档](https://ai.baidu.com/ai-doc/FACE/yk37c1u4t) #### 1. 导入模块,输入百度官网给出的API Key和Secret Key ```python import requests # client_id 为官网获取的APIKey,client_secret为官网获取的Secret Key host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}' client_id = "8YIS4b9PawXz9wjniTDeh5HZ" client_secret = "1cnF87p0NyYVLMmWxgfhrxHMkifu0p9k" response = requests.get(host.format(client_id, client_secret)) if response: print(response.json()) # 返回结果 {'refresh_token': '25.02463672e95e9a83036c81a4e593b3da.315360000.1918886256.282335-22869548', 'expires_in': 2592000, 'session_key': '9mzdD0L28bUFiyArrlppYZMTJFthzMUcxkTBEXWa0Xx5rNYE0+XNK6y6BhP+rWrJy9Q1ozE5+OT+MCeV8zwfFWt2JgXPMQ==', 'access_token': '24.4fccd940570684564d194d9fd1bd92e4.2592000.1606118256.282335-22869548', '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': '01fed1bf12ccb01d24a4a57b1afac680'} ``` ```python # (让它排列得整齐一点) results = response.json() results {'refresh_token': '25.02463672e95e9a83036c81a4e593b3da.315360000.1918886256.282335-22869548', 'expires_in': 2592000, 'session_key': '9mzdD0L28bUFiyArrlppYZMTJFthzMUcxkTBEXWa0Xx5rNYE0+XNK6y6BhP+rWrJy9Q1ozE5+OT+MCeV8zwfFWt2JgXPMQ==', 'access_token': '24.4fccd940570684564d194d9fd1bd92e4.2592000.1606118256.282335-22869548', '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': '01fed1bf12ccb01d24a4a57b1afac680'} ``` #### 2.1 人脸检测与属性分析 ```python request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect" #添加人脸图片的url params = "{\"image\":\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603536477084&di=5703c5fb651da5201f10ad840ea7375b&imgtype=0&src=http%3A%2F%2Fdingyue.ws.126.net%2F2020%2F0310%2Fc63836a2j00q6z2bw00acc000z901bjm.jpg\",\"image_type\":\"URL\",\"face_field\":\"faceshape,facetype\"}" access_token = '24.4fccd940570684564d194d9fd1bd92e4.2592000.1606118256.282335-22869548' 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': 2015545754555, 'timestamp': 1603526426, 'cached': 0, 'result': {'face_num': 1, 'face_list': [{'face_token': 'ae407fb0ae5f4efe58f3d0fac00a139b', 'location': {'left': 271.97, 'top': 517.34, 'width': 758, 'height': 715, 'rotation': 2}, 'face_probability': 1, 'angle': {'yaw': -27.84, 'pitch': 15.83, 'roll': 7.57}, 'face_shape': {'type': 'heart', 'probability': 0.51}, 'face_type': {'type': 'human', 'probability': 0.97}}]}} ``` #### 2.2 获取第二张人脸的face_token ```python request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect" #添加人脸图片的url params = "{\"image\":\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603536654523&di=fb80a33087a34326b30abf704734f4af&imgtype=0&src=http%3A%2F%2Fugc.hitv.com%2Fzhuanlan%2F20200228143906_6e6734bc-914e-4206-af77-5ab8ac77a5c1.jpg\",\"image_type\":\"URL\",\"face_field\":\"faceshape,facetype\"}" access_token = '24.4fccd940570684564d194d9fd1bd92e4.2592000.1606118256.282335-22869548' 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': 9415001455500, 'timestamp': 1603526586, 'cached': 0, 'result': {'face_num': 1, 'face_list': [{'face_token': 'ea1f2d40cd38038fcbbcfaebac4698e1', 'location': {'left': 167.59, 'top': 147.95, 'width': 144, 'height': 137, 'rotation': -2}, 'face_probability': 1, 'angle': {'yaw': 6.1, 'pitch': 10.96, 'roll': -5.92}, 'face_shape': {'type': 'round', 'probability': 0.46}, 'face_type': {'type': 'human', 'probability': 1}}]}} ``` #### 3. 人脸对比 ```python # 3.人脸对比 request_url = "https://aip.baidubce.com/rest/2.0/face/v3/match" params = "[{\"image\": \"ae407fb0ae5f4efe58f3d0fac00a139b\", \"image_type\": \"FACE_TOKEN\"}, {\"image\": \"ea1f2d40cd38038fcbbcfaebac4698e1\", \"image_type\": \"FACE_TOKEN\"}]" access_token = '24.4fccd940570684564d194d9fd1bd92e4.2592000.1606118256.282335-22869548' 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()) # 返回结果 score即为两张人脸的相似度 {'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 1018410145252, 'timestamp': 1603526714, 'cached': 0, 'result': {'score': 94.4076004, 'face_list': [{'face_token': 'ae407fb0ae5f4efe58f3d0fac00a139b'}, {'face_token': 'ea1f2d40cd38038fcbbcfaebac4698e1'}]}} ``` ---

二、计算机视觉

参考:[Azure 计算机视觉文档](https://docs.microsoft.com/zh-cn/azure/cognitive-services/computer-vision/) ### 1. 分析远程图像 #### 1.分析远程图片 ```python import requests %matplotlib inline import matplotlib.pyplot as plt import json from PIL import Image from io import BytesIO # 从官网获取 endpoint = "https://api-hr01.cognitiveservices.azure.com/" subscription_key = "7f058435c70043758c63fca8b017c360" # base url analyze_url = endpoint+ "vision/v3.1/analyze" # 一张图片的链接 image_url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603540193573&di=c0dbf74e9ecf88285a2e4fdd6a1d1017&imgtype=0&src=http%3A%2F%2Fs3.lvjs.com.cn%2Fuploads%2Fpc%2Fplace2%2F2017-08-10%2Fabdf7dcc-ca9a-4000-b94f-38ef745ceaba.jpg" headers = {'Ocp-Apim-Subscription-Key': subscription_key} # 参数 params = {'visualFeatures': 'Categories,Description,Color'} # 请求主体body 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() ``` ![api-1](https://images.gitee.com/uploads/images/2020/1025/000345_e52ce4ba_5329208.png "api-1.png") #### 2.分析本地图片 ```python import os import sys import requests %matplotlib inline import matplotlib.pyplot as plt from PIL import Image from io import BytesIO # 本地图片地址 image_path = "C:/Users/13513/Pictures/Saved Pictures/liuhaoran.jpg" # Read the image into a byte array image_data = open(image_path, "rb").read() headers = {'Ocp-Apim-Subscription-Key': "7f058435c70043758c63fca8b017c360", '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) ``` ![api-2](https://images.gitee.com/uploads/images/2020/1025/000559_8c7dfff6_5329208.png "api-2.png") #### 3.生成缩略图 ```python import os import sys import requests %matplotlib inline import matplotlib.pyplot as plt from PIL import Image from io import BytesIO thumbnail_url = "https://api-hr01.cognitiveservices.azure.com/" + "vision/v3.1/generateThumbnail" # 图片链接 image_url = "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2406213756,3342094803&fm=26&gp=0.jpg" headers = {'Ocp-Apim-Subscription-Key': "7f058435c70043758c63fca8b017c360"} 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)) ``` ![api-3](https://images.gitee.com/uploads/images/2020/1025/000714_0fd2a3a6_5329208.png "api-3.png") #### 4.提取文本(读取API) ```python import json import os import sys import requests import time %matplotlib inline import matplotlib.pyplot as plt from matplotlib.patches import Polygon from PIL import Image from io import BytesIO text_recognition_url = "https://api-hr01.cognitiveservices.azure.com/vision/v3.0/read/analyze" # Set image_url to the URL of an image that you want to recognize. image_url = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3025575980,164542079&fm=26&gp=0.jpg" headers = {'Ocp-Apim-Subscription-Key': "e33e05cf8a914c73b9a740cab41e61cc"} data = {'url': image_url} response = requests.post(text_recognition_url, headers=headers, json=data) response.raise_for_status() # Extracting text requires two API calls: One call to submit the # image for processing, the other to retrieve the text found in the image. # Holds the URI used to retrieve the recognized text. operation_url = response.headers["Operation-Location"] # The recognized text isn't immediately available, so poll to wait for completion. analysis = {} poll = True while (poll): response_final = requests.get( response.headers["Operation-Location"], headers=headers) analysis = response_final.json() print(json.dumps(analysis, indent=4)) time.sleep(1) if ("analyzeResult" in analysis): poll = False if ("status" in analysis and analysis['status'] == 'failed'): poll = False polygons = [] if ("analyzeResult" in analysis): # Extract the recognized text, with bounding boxes. polygons = [(line["boundingBox"], line["text"]) for line in analysis["analyzeResult"]["readResults"][0]["lines"]] # Display the image and overlay it with the extracted text. image = Image.open(BytesIO(requests.get(image_url).content)) ax = plt.imshow(image) for polygon in polygons: vertices = [(polygon[0][i], polygon[0][i+1]) for i in range(0, len(polygon[0]), 2)] text = polygon[1] patch = Polygon(vertices, closed=True, fill=False, linewidth=2, color='y') ax.axes.add_patch(patch) plt.text(vertices[0][0], vertices[0][1], text, fontsize=20, va="top") plt.show() ``` #### 返回结果 ``` { "status": "running", "createdDateTime": "2020-10-24T09:18:11Z", "lastUpdatedDateTime": "2020-10-24T09:18:11Z" } { "status": "succeeded", "createdDateTime": "2020-10-24T09:18:11Z", "lastUpdatedDateTime": "2020-10-24T09:18:11Z", "analyzeResult": { "version": "3.0.0", "readResults": [ { "page": 1, "angle": -0.1174, "width": 311, "height": 209, "unit": "pixel", "lines": [ { "boundingBox": [ 5, 7, 253, 6, 253, 41, 5, 44 ], "text": "Life is the flower for", "words": [ { "boundingBox": [ 24, 7, 67, 9, 60, 44, 15, 44 ], "text": "Life", "confidence": 0.981 }, { "boundingBox": [ 74, 10, 90, 10, 84, 44, 68, 44 ], "text": "is", "confidence": 0.981 }, { "boundingBox": [ 97, 10, 135, 11, 132, 43, 92, 44 ], "text": "the", "confidence": 0.985 }, { "boundingBox": [ 143, 11, 203, 11, 203, 41, 140, 43 ], "text": "flower", "confidence": 0.652 }, { "boundingBox": [ 211, 11, 251, 10, 253, 38, 211, 40 ], "text": "for", "confidence": 0.981 } ] }, { "boundingBox": [ 16, 60, 262, 58, 263, 90, 16, 93 ], "text": "which love is the honey", "words": [ { "boundingBox": [ 23, 62, 83, 60, 82, 92, 22, 94 ], "text": "which", "confidence": 0.944 }, { "boundingBox": [ 89, 60, 128, 59, 128, 91, 89, 92 ], "text": "love", "confidence": 0.981 }, { "boundingBox": [ 134, 59, 153, 59, 153, 91, 134, 91 ], "text": "is", "confidence": 0.981 }, { "boundingBox": [ 159, 59, 201, 59, 200, 90, 159, 91 ], "text": "the", "confidence": 0.985 }, { "boundingBox": [ 207, 59, 263, 61, 262, 90, 206, 90 ], "text": "honey", "confidence": 0.736 } ] }, { "boundingBox": [ 12, 106, 310, 112, 309, 143, 12, 140 ], "text": "Fine words butter no parsnips", "words": [ { "boundingBox": [ 22, 107, 71, 107, 70, 141, 21, 141 ], "text": "Fine", "confidence": 0.913 }, { "boundingBox": [ 78, 107, 127, 109, 127, 141, 77, 141 ], "text": "words", "confidence": 0.983 }, { "boundingBox": [ 134, 109, 192, 111, 192, 141, 133, 141 ], "text": "butter", "confidence": 0.892 }, { "boundingBox": [ 198, 111, 223, 112, 223, 142, 198, 141 ], "text": "no", "confidence": 0.986 }, { "boundingBox": [ 230, 113, 308, 117, 308, 143, 230, 142 ], "text": "parsnips", "confidence": 0.911 } ] }, { "boundingBox": [ 7, 159, 251, 159, 251, 192, 7, 193 ], "text": "Bad neus travels fast", "words": [ { "boundingBox": [ 21, 159, 67, 160, 68, 194, 21, 194 ], "text": "Bad", "confidence": 0.979 }, { "boundingBox": [ 74, 160, 121, 160, 121, 193, 74, 194 ], "text": "neus", "confidence": 0.559 }, { "boundingBox": [ 127, 160, 194, 161, 195, 191, 128, 193 ], "text": "travels", "confidence": 0.69 }, { "boundingBox": [ 201, 161, 249, 163, 251, 189, 201, 191 ], "text": "fast", "confidence": 0.978 } ] } ] } ] } } ``` ![api-4](https://images.gitee.com/uploads/images/2020/1025/001053_888017bf_5329208.png "api-4.png") #### 5. 提取文本(OCR API) ```python import os import sys import requests %matplotlib inline import matplotlib.pyplot as plt from matplotlib.patches import Rectangle from PIL import Image from io import BytesIO ocr_url = "https://api-hr01.cognitiveservices.azure.com/vision/v3.1/ocr" # Set image_url to the URL of an image that you want to analyze. image_url = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3025575980,164542079&fm=26&gp=0.jpg" headers = {'Ocp-Apim-Subscription-Key': "7f058435c70043758c63fca8b017c360"} params = {'language': 'unk', 'detectOrientation': 'true'} data = {'url': image_url} response = requests.post(ocr_url, headers=headers, params=params, json=data) response.raise_for_status() analysis = response.json() # Extract the word bounding boxes and text. line_infos = [region["lines"] for region in analysis["regions"]] word_infos = [] for line in line_infos: for word_metadata in line: for word_info in word_metadata["words"]: word_infos.append(word_info) word_infos # Display the image and overlay it with the extracted text. plt.figure(figsize=(5, 5)) image = Image.open(BytesIO(requests.get(image_url).content)) ax = plt.imshow(image, alpha=0.5) for word in word_infos: bbox = [int(num) for num in word["boundingBox"].split(",")] text = word["text"] origin = (bbox[0], bbox[1]) patch = Rectangle(origin, bbox[2], bbox[3], fill=False, linewidth=2, color='y') ax.axes.add_patch(patch) plt.text(origin[0], origin[1], text, fontsize=20, weight="bold", va="top") plt.show() plt.axis("off") ``` ![api-5](https://images.gitee.com/uploads/images/2020/1025/001233_377d77dc_5329208.png "api-5.png") #### 6.使用域模型--识别地标 ```python import os import sys import requests %matplotlib inline import matplotlib.pyplot as plt from PIL import Image from io import BytesIO landmark_analyze_url = endpoint + "vision/v3.1/models/landmarks/analyze" # Set image_url to the URL of an image that you want to analyze. image_url = "https://images.unsplash.com/photo-1587825338028-f1d568e0dbb3?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2689&q=80" headers = {'Ocp-Apim-Subscription-Key': "7f058435c70043758c63fca8b017c360"} 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() ``` ![api-7](https://images.gitee.com/uploads/images/2020/1025/001509_2d2b8389_5329208.png "api-7.png") #### 7.使用域模型--名人 ```python import requests %matplotlib inline import matplotlib.pyplot as plt from PIL import Image from io import BytesIO # Replace with your valid subscription key. subscription_key = "7f058435c70043758c63fca8b017c360" assert subscription_key vision_base_url = "https://api-hr01.cognitiveservices.azure.com/vision/v2.1/" celebrity_analyze_url = vision_base_url + "models/celebrities/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=1603541905213&di=863e904e625f5302e6c8094d3f441fff&imgtype=0&src=http%3A%2F%2Fpic.baike.soso.com%2Fp%2F20140504%2F20140504131147-671941598.jpg" headers = {'Ocp-Apim-Subscription-Key': subscription_key} params = {'model': 'celebrities'} data = {'url': image_url} response = requests.post( celebrity_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 celebrity for the image is obtained from the 'result' property. analysis = response.json() assert analysis["result"]["celebrities"] is not [] print(analysis) celebrity_name = analysis["result"]["celebrities"][0]["name"].capitalize() # Display the image and overlay it with the celebrity name. image = Image.open(BytesIO(requests.get(image_url).content)) plt.imshow(image) plt.axis("off") _ = plt.title(celebrity_name, size="x-large", y=-0.1) plt.show() ``` ![api-6](https://images.gitee.com/uploads/images/2020/1025/001603_4e0b6f76_5329208.png "api-6.png") ---

三、学习心得

1. 在返回错误的结果时千万不要慌张、厌烦,运行报错一般会提示你哪里出错,一定要把那行代码出现到的值,从头找出逐次排查,一般就能检查出来,若是单纯显示某行信息运行错误,没有提示运行原因,查看是否是因为缩进或多打了空格或字符不对等基础性错误,不断尝试,不断修改问题总会解决的! 2. 对我来说API这门课程在前几周上课的过程中感到十分吃力,知识比较难懂,但是在渐渐的学习于练习探索中,发现api的学习还是蛮有趣的。API这门课程与Python相结合,学习各种网站的文档,多尝试,多练习。 3. 多看API文档,结合API文档写代码,api文档真的很有用!一开始看到老师给出的文档的时候根本看不懂是什么意思,一片密密麻麻的代码看得就头疼,盲目的操作,也不知道到底在改什么,所以也导致就算错了,给出错误信息了,也不知应如何修改。这时,好好阅读、对比官方网站给出的api文档就很重要,对比过在这次作业中使用到的azure、face++和百度官网除了能给我们提供免费的使用权限(key等)外,给出的api文档里都会包含功能描述、调用url、调用方法、请求方式、返回值的解释等信息,有些甚至会直接给出代码,根据这些信息我们就可以更好的理解每一行代码的意思,返回错误时,一查也能知道自己是哪里错了。 4. 积极与其他小伙伴交流经验,互相学习,减少不必要的错误。