test_recogn.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. from flask import Flask, request, jsonify
  2. import base64
  3. import io
  4. from PIL import Image
  5. import time
  6. app = Flask(__name__)
  7. @app.route('/api/pressure', methods=['POST'])
  8. def recognize_pressure():
  9. try:
  10. # 获取文件和元数据
  11. image_file = request.files.get('image')
  12. metadata_json = request.form.get('metadata')
  13. if image_file is None:
  14. return jsonify({
  15. "result": 0,
  16. "message": "图像未上传",
  17. "data": None
  18. }), 400
  19. # 把上传图像数据缓存下来(避免流被读两次)
  20. image_bytes = image_file.read()
  21. image_stream = io.BytesIO(image_bytes)
  22. try:
  23. image = Image.open(image_stream);
  24. format = image.format
  25. print(f"image format is {format}")
  26. except Exception as e:
  27. print("图像格式无效:",e)
  28. # 读取图像内容并转为 base64
  29. # image_bytes = image_file.read()
  30. image_base64 = base64.b64encode(image_bytes).decode('utf-8')
  31. # 模拟生成日志
  32. log_content = f"处理图像: {image_file.filename}\n时间: {time.strftime('%Y-%m-%d %H:%M:%S')}\n状态: 成功"
  33. logs_base64 = base64.b64encode(log_content.encode('utf-8')).decode('utf-8')
  34. # 模拟读取的仪表值
  35. reading_value = 123.45 # 可以改为实际模型识别逻辑
  36. # response = {
  37. # "result": 1,
  38. # "message": "识别成功",
  39. # "data": {
  40. # "image": image_base64,
  41. # "logs": logs_base64,
  42. # "reading": reading_value
  43. # }
  44. # }
  45. # response = {
  46. # "result": 0,
  47. # "message": "x 图像处理失败:自动旋转未找到有效角度",
  48. # "data": {
  49. # "image": None,
  50. # "logs": "",
  51. # "reading": None
  52. # }
  53. # }
  54. # response = {
  55. # "result": 1,
  56. # "message": "成功处理",
  57. # "data": {
  58. # "meter_type":1,
  59. # "image": image_base64,
  60. # "logs": logs_base64,
  61. # "reading": 123.66,
  62. # "reading_unit":0.001,
  63. # "reading_str": '12.34.22',
  64. # "reading_str_flag":1
  65. # }
  66. # }
  67. response = {
  68. "result": 1,
  69. "message": "成功处理",
  70. "data": {
  71. "meter_type":1,
  72. "image": image_base64,
  73. "logs": logs_base64,
  74. "reading": 123.09,
  75. "reading_unit":0.001
  76. }
  77. }
  78. # response = {
  79. # "result": 1,
  80. # "message": "成功处理",
  81. # "data": {
  82. # "meter_type":1,
  83. # "image": image_base64,
  84. # "logs": logs_base64,
  85. # "reading": None,
  86. # "reading_unit":None,
  87. # "reading_str": '12.34.22',
  88. # "reading_str_flag":1
  89. # }
  90. # }
  91. # response = {
  92. # "result": 1,
  93. # "message": "成功处理",
  94. # "data": {
  95. # "meter_type":1,
  96. # "image": image_base64,
  97. # "logs": logs_base64,
  98. # "reading": 123.45,
  99. # "reading_unit":0.01,
  100. # "reading_str": None,
  101. # "reading_str_flag":None
  102. # }
  103. # }
  104. # response = {
  105. # "result": 0,
  106. # "message": "未成功处理",
  107. # "data": {
  108. # "meter_type":None,
  109. # "image": None,
  110. # "logs": "",
  111. # "reading": None,
  112. # "reading_unit":None
  113. # }
  114. # }
  115. return jsonify(response)
  116. except Exception as e:
  117. print(f"{e}")
  118. return jsonify({
  119. "result": 0,
  120. "message": f"服务端异常: {str(e)}",
  121. "data": None
  122. }), 500
  123. if __name__ == '__main__':
  124. app.run(host='0.0.0.0', port=5000)