flaskweb.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import json
  4. import base64
  5. from flask import Flask,request,Response
  6. from image_detect import ImageDetect
  7. from ibm_ai import ai_detect
  8. app = Flask(__name__)
  9. @app.route('/api/test', methods=['GET'])
  10. def apitest():
  11. return Response(response='Rest server is running...',status=200,mimetype="application/json")
  12. @app.route('/', methods=['GET'])
  13. def test():
  14. return Response(response='Rest server is running...',status=200,mimetype="application/json")
  15. #get the result image
  16. @app.route("/api/get_jpg", methods=['POST'])
  17. def get_jpg():
  18. result_json={"error":""}
  19. jpg_name=request.json['jpg_name']
  20. if jpg_name is None or jpg_name=="":
  21. result_json["error"]="jpg_name is null!"
  22. else:
  23. jpg_path=r'.\web\images\{0}'.format(jpg_name)
  24. if not os.path.exists(jpg_path):
  25. result_json["error"]="%s not found" % jpg_name
  26. else:
  27. with open(r'.\web\images\{0}'.format(jpg_name), 'rb') as f:
  28. result_json["image"]= base64.b64encode(f.read())
  29. return json.dumps( result_json)
  30. @app.route('/api/detect', methods=['POST'])
  31. def detect():
  32. in_args = {
  33. 'args': request.json['args'],
  34. 'data': request.json['data'],
  35. 'params': request.json['params']
  36. }
  37. detect = ImageDetect()
  38. result = json.loads(detect.init(json.dumps(in_args['args'])))
  39. status=200
  40. if result['success'] == False:
  41. status=400
  42. result = detect.do_pred_by_data(in_args['data'], in_args['params'])
  43. return Response(response=result,status=status,mimetype="application/json")
  44. # ai analysis of jpgs
  45. # get the image names to analysize local images
  46. # the results of images will be saved in the AI directory of the local directory
  47. @app.route('/api/detect_jpgs', methods=['POST'])
  48. def jzw_detect():
  49. json1=request.json['json1']
  50. json2=request.json['json2']
  51. thread_count=3
  52. if json1['func']=='zzsb':
  53. thread_count=1
  54. # json2.update({'zoom_key':200,'um_per_pix':0.2744})
  55. print('json1:',json1)
  56. print('json2:',json2)
  57. print('thread_count:',thread_count)
  58. drive='X'
  59. if json1.has_key('path'):
  60. path=json1['path']
  61. json1['path_client']=path
  62. pos=path.find(':')
  63. if pos>0 :
  64. path=drive+path[pos:]
  65. json1['path']=path
  66. status=200
  67. result={'success':True,'count':0}
  68. try:
  69. count=ai_detect(thread_count,json1,json2)
  70. result['count']=count
  71. except Exception as e:
  72. print("exception:",e.message)
  73. status=400
  74. result['success']=False
  75. print('response:',result)
  76. return Response(response=json.dumps(result),status=status,mimetype="application/json")
  77. if __name__ == "__main__":
  78. print("OPTON VI Edge Server be started...")
  79. #app.run(host="0.0.0.0", port=3000,debug=True)
  80. #print("OPTON VI Edge Server be shutdown")