flask快速搭建服务
1 文本请求
服务器端代码示例:
核心函数:request.form.get('house_code')
from split_address_ner import load_model,split_address
from up_three import AddressSplit_3
from flask import Flask
from flask import request
from flask import jsonify
import requests
import time
#加载模型
model,sess,tag_to_id,id_to_tag = load_model()
address_json_path = './json_file/adress_json.json'
province_json_path = './json_file/province_json.json'
AS = AddressSplit_3(address_json_path,province_json_path)
app = Flask(__name__)
@app.route('/ner_address', methods=['POST'])
def separate():
if request.method == 'POST':
res_3 = {'prov':'','city':'','area':''}
start_time = time.time()
result={'data':{'address':{}},'error_code':0,'time_cost':100}
address = request.form.get('address')
address = str(address)
house_code = request.form.get('house_code')
house_code = str(house_code)
#res = predict(name)
res = split_address(model,sess,tag_to_id,id_to_tag,address)
prov, city, area = AS.city_judge(address,house_code)
prov, city, area = AS.post_three_levels(prov, city, area, address)
res_3['prov'] = prov
res_3['city'] = city
res_3['area'] = area
res.update(res_3)
end_time = time.time()
result['data']['address'] = res
result['time_cost'] = int(round(end_time*1000)) - int(round(start_time*1000))
return jsonify(result)
客户端请求示例:
请求示例
data = {'address':'北京昌平区','house_code':''}
r = requests.post("http://127.0.0.1:5000/ner_address", data=data)
r.text
2 图片请求
2.1 图片本地请求
服务器端代码
app = Flask(__name__)
@app.route("/abnormal_detect", methods=['POST'])
def detect():
if request.method == 'POST':
# file = request.files['file']
# if file and allowed_file(file.filename):
# start_time = time.time()
# result = {'sim_score': 0,'is_abnormal':0, 'error_code': 0, 'time_cost': 100}
# end_time = time.time()
result = {'sim_score': 0,'struct_sim':0, 'is_abnormal': 0, 'error_code': 0, 'time_cost': 100}
start_time = time.time()
"""
本地请求方式
"""
img1 = base64.b64decode(str(request.form['img1']))
img2 = base64.b64decode(str(request.form['img2']))
image_data1 = np.fromstring(img1, np.uint8)
image_data1 = cv2.imdecode(image_data1, cv2.IMREAD_COLOR)
image_data2 = np.fromstring(img2, np.uint8)
image_data2 = cv2.imdecode(image_data2, cv2.IMREAD_COLOR)
img1_p='./1.jpg'
img2_p='./2.jpg'
cv2.imwrite(img1_p,image_data1)
cv2.imwrite(img2_p, image_data2)
sift_score = cal_sift2(img1_p,img2_p)
struct_score = sim_struct(img1_p,img2_p,root)
if sift_score<0.5:
result['is_abnormal'] = 1
end_time = time.time()
time_total = int(round(end_time*1000)) - int(round(start_time*1000))
result['sim_score'] = sift_score
result['struct_sim'] = struct_score
result['time_cost'] = time_total
return jsonify(result)
客户端请求代码
import requests
import base64
#将图片数据转成base64格式
path1 = '/Users/changzhanguo/Desktop/河床/0010.jpg'
path2 = '/Users/changzhanguo/Desktop/河床/0080.jpg'
with open(path1, 'rb') as f:
img = base64.b64encode(f.read()).decode()
img1 = []
img1.append(img)
with open(path2, 'rb') as f:
img = base64.b64encode(f.read()).decode()
img2 = []
img2.append(img)
res = {"img1":img1,'img2':img2}
print(img1)
#访问服务
r = requests.post("http://127.0.0.1:5000/abnormal_detect",data=res)
print(r.json())
2.2 图片网络请求
服务器端代码
app = Flask(__name__)
@app.route("/abnormal_detect", methods=['POST'])
def detect():
if request.method == 'POST':
# file = request.files['file']
# if file and allowed_file(file.filename):
# start_time = time.time()
# result = {'sim_score': 0,'is_abnormal':0, 'error_code': 0, 'time_cost': 100}
# end_time = time.time()
result = {'sim_score': 0,'struct_sim':0, 'is_abnormal': 0, 'error_code': 0, 'time_cost': 100}
start_time = time.time()
#网络请求方式
params = request.form.to_dict()
img1_path = params['img1']
img2_path = params['img2']
# img1 = base64.b64decode(str(request.form['img1']))
# img2 = base64.b64decode(str(request.form['img2']))
img1_bytes = requests.get(img1_path).content
img2_bytes = requests.get(img2_path).content
img1_p = './1.jpg'
with open(img1_p, 'wb') as f:
f.write(img1_bytes)
img2_p = './2.jpg'
with open(img2_p, 'wb') as f:
f.write(img2_bytes)
sift_score = cal_sift2(img1_p,img2_p)
struct_score = sim_struct(img1_p,img2_p,root)
if sift_score<0.5:
result['is_abnormal'] = 1
end_time = time.time()
time_total = int(round(end_time*1000)) - int(round(start_time*1000))
result['sim_score'] = sift_score
result['struct_sim'] = struct_score
result['time_cost'] = time_total
return jsonify(result)
客户端请求代码
res = {"img1":img1,'img2':img2}
print(img1)
#访问服务
r = requests.post("http://127.0.0.1:5000/abnormal_detect",data=res)