1,
2,
flaskFile.py
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/')
def hello():
from 人脸搜索MN import main
response = main().json()
return jsonify(response)
if __name__ == '__main__':
app.run(port=8080)
3,
人脸注册.py
import base64
import urllib
import requests
import json
API_KEY = "填自己的"
SECRET_KEY = "填自己的"
def main():
url = "https://aip.baidubce.com/rest/2.0/face/v3/multi-search?access_token=" + get_access_token()
# image 可以通过 get_file_content_as_base64("C:\fakepath\66.jpg",False) 方法获取
content = get_file_content_as_base64('./66.jpg', False)
payload = json.dumps({
"group_id_list": "11",
"image":content,
"image_type": "BASE64"
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
print('我是这个类型',type(response))
return response
def get_file_content_as_base64(path, urlencoded=False):
"""
获取文件base64编码
:param path: 文件路径
:param urlencoded: 是否对结果进行urlencoded
:return: base64编码信息
"""
with open(path, "rb") as f:
content = base64.b64encode(f.read()).decode("utf8")
if urlencoded:
content = urllib.parse.quote_plus(content)
return content
def get_access_token():
"""
使用 AK,SK 生成鉴权签名(Access Token)
:return: access_token,或是None(如果错误)
"""
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
return str(requests.post(url, params=params).json().get("access_token"))
if __name__ == '__main__':
main()
4,
人脸搜索MN.py
import base64
import urllib
import requests
import json
API_KEY = ""
SECRET_KEY = ""
def main():
url = "https://aip.baidubce.com/rest/2.0/face/v3/multi-search?access_token=" + get_access_token()
# image 可以通过 get_file_content_as_base64("C:\fakepath\66.jpg",False) 方法获取
content = get_file_content_as_base64('./66.jpg', False)
payload = json.dumps({
"group_id_list": "11",
"image":content,
"image_type": "BASE64"
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
print('我是这个类型',type(response))
return response
def get_file_content_as_base64(path, urlencoded=False):
"""
获取文件base64编码
:param path: 文件路径
:param urlencoded: 是否对结果进行urlencoded
:return: base64编码信息
"""
with open(path, "rb") as f:
content = base64.b64encode(f.read()).decode("utf8")
if urlencoded:
content = urllib.parse.quote_plus(content)
return content
def get_access_token():
"""
使用 AK,SK 生成鉴权签名(Access Token)
:return: access_token,或是None(如果错误)
"""
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
return str(requests.post(url, params=params).json().get("access_token"))
if __name__ == '__main__':
main()