123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- from flask import Flask, request, jsonify
- import base64
- import io
- from PIL import Image
- import time
- app = Flask(__name__)
- @app.route('/api/pressure', methods=['POST'])
- def recognize_pressure():
- try:
- # 获取文件和元数据
- image_file = request.files.get('image')
- metadata_json = request.form.get('metadata')
- if image_file is None:
- return jsonify({
- "result": 0,
- "message": "图像未上传",
- "data": None
- }), 400
- # 把上传图像数据缓存下来(避免流被读两次)
- image_bytes = image_file.read()
- image_stream = io.BytesIO(image_bytes)
- try:
- image = Image.open(image_stream);
- format = image.format
- print(f"image format is {format}")
- except Exception as e:
- print("图像格式无效:",e)
- # 读取图像内容并转为 base64
- # image_bytes = image_file.read()
- image_base64 = base64.b64encode(image_bytes).decode('utf-8')
- # 模拟生成日志
- log_content = f"处理图像: {image_file.filename}\n时间: {time.strftime('%Y-%m-%d %H:%M:%S')}\n状态: 成功"
- logs_base64 = base64.b64encode(log_content.encode('utf-8')).decode('utf-8')
- # 模拟读取的仪表值
- reading_value = 123.45 # 可以改为实际模型识别逻辑
- # response = {
- # "result": 1,
- # "message": "识别成功",
- # "data": {
- # "image": image_base64,
- # "logs": logs_base64,
- # "reading": reading_value
- # }
- # }
- # response = {
- # "result": 0,
- # "message": "x 图像处理失败:自动旋转未找到有效角度",
- # "data": {
- # "image": None,
- # "logs": "",
- # "reading": None
- # }
- # }
- # response = {
- # "result": 1,
- # "message": "成功处理",
- # "data": {
- # "meter_type":1,
- # "image": image_base64,
- # "logs": logs_base64,
- # "reading": 123.66,
- # "reading_unit":0.001,
- # "reading_str": '12.34.22',
- # "reading_str_flag":1
- # }
- # }
- response = {
- "result": 1,
- "message": "成功处理",
- "data": {
- "meter_type":1,
- "image": image_base64,
- "logs": logs_base64,
- "reading": 123.09,
- "reading_unit":0.001
- }
- }
- # response = {
- # "result": 1,
- # "message": "成功处理",
- # "data": {
- # "meter_type":1,
- # "image": image_base64,
- # "logs": logs_base64,
- # "reading": None,
- # "reading_unit":None,
- # "reading_str": '12.34.22',
- # "reading_str_flag":1
- # }
- # }
- # response = {
- # "result": 1,
- # "message": "成功处理",
- # "data": {
- # "meter_type":1,
- # "image": image_base64,
- # "logs": logs_base64,
- # "reading": 123.45,
- # "reading_unit":0.01,
- # "reading_str": None,
- # "reading_str_flag":None
- # }
- # }
- # response = {
- # "result": 0,
- # "message": "未成功处理",
- # "data": {
- # "meter_type":None,
- # "image": None,
- # "logs": "",
- # "reading": None,
- # "reading_unit":None
- # }
- # }
- return jsonify(response)
- except Exception as e:
- print(f"{e}")
- return jsonify({
- "result": 0,
- "message": f"服务端异常: {str(e)}",
- "data": None
- }), 500
- if __name__ == '__main__':
- app.run(host='0.0.0.0', port=5000)
|