from flask
import Flask, render_template, Response,jsonify,request
import config
import json
import os.path
import requests
url =
'http://10.9.102.133:8061'
app = Flask(
__name__)
app.config.from_object(config)
@app.route(
'/')
def
index():
return render_template(
'index.html')
@app.route(
'/upload/',
methods=[
'POST'])
def
uploadiamge():
'''
save a pic on py project from local pc
:return:
'''
file = request.files[
'filechoose']
file.save(
'image/test.jpg')
return use_detect_api()
def
use_detect_api():
module_path = os.path.dirname(
__file__)
f =
open(module_path +
'/image/test.jpg',
'rb')
#如果不加b,则会报转码错误
file = {
'image': f}
#r 是个response对象
r = requests.post(url +
'/detect',
files=
file)
#print(type(r.text)) #str
resJson = json.loads(r.text)
#print(type(resJson)) #dict
#1.会将内容转换为json,
#2.修改Content-Type为application/json。
return jsonify(resJson)
app.run()
前端:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Testing Page</title> </head> <body> <form action="{{ url_for('uploadiamge') }}" method="POST" enctype="multipart/form-data"> <input type="file" name="filechoose"> <input type="submit" value="Upload"> </form> </body> </html>