app.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding:utf-8 -*-
  2. from flask import Flask, render_template, request, redirect, url_for, make_response, jsonify,Markup
  3. from werkzeug.utils import secure_filename
  4. from tools import get_text,get_image
  5. from datetime import timedelta
  6. import os
  7. from dev_image import run_cut
  8. import json
  9. ALLOWED_EXTENSIONS = set(['png', 'jpg', 'JPG', 'PNG', 'bmp'])
  10. def allowed_file(filename):
  11. return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
  12. app = Flask(__name__)
  13. # 设置静态文件缓存过期时间
  14. app.send_file_max_age_default = timedelta(seconds=1)
  15. # @app.route('/upload', methods=['POST', 'GET'])
  16. @app.route('/', methods=['POST', 'GET']) # 添加路由
  17. def upload():
  18. if request.method == 'POST':
  19. print(request.files)
  20. f = request.files['file']
  21. if not (f and allowed_file(f.filename)):
  22. return jsonify({"error": 1001, "msg": "请检查上传的图片类型,仅限于png、PNG、jpg、JPG、bmp"})
  23. # user_input = request.form.get("name")
  24. basepath = os.path.dirname(__file__) # 当前文件所在路径
  25. upload_path = os.path.join('./image_dir', secure_filename(f.filename)) # 注意:没有的文件夹一定要先创建,不然会提示没有该路径
  26. # upload_path = os.path.join(basepath, 'static/images','test.jpg') #注意:没有的文件夹一定要先创建,不然会提示没有该路径
  27. print(upload_path)
  28. f.save(upload_path)
  29. # 使用Opencv转换一下图片格式和名称
  30. # img = cv2.imread(upload_path)
  31. # cv2.imwrite(os.path.join(basepath, 'static/images', 'test.jpg'), img)
  32. # maple = requests.post('http://127.0.0.1:6666/static_pic',json={'path':upload_path}).text
  33. # print(maple)
  34. run_cut(upload_path,tabel=False)
  35. text = get_text()
  36. images = get_image()
  37. print(text)
  38. return render_template('show.html', w=500, contents=text,
  39. images=images
  40. )
  41. return render_template('upload.html')
  42. @app.route('/online', methods=['POST', 'GET']) # 添加路由
  43. def upload_online():
  44. if request.method == 'POST':
  45. f = request.files['file']
  46. if not (f and allowed_file(f.filename)):
  47. return jsonify({"error": 1001, "msg": "请检查上传的图片类型,仅限于png、PNG、jpg、JPG、bmp"})
  48. # user_input = request.form.get("name")
  49. basepath = os.path.dirname(__file__) # 当前文件所在路径
  50. upload_path = os.path.join('./image_dir', secure_filename(f.filename)) # 注意:没有的文件夹一定要先创建,不然会提示没有该路径
  51. # upload_path = os.path.join(basepath, 'static/images','test.jpg') #注意:没有的文件夹一定要先创建,不然会提示没有该路径
  52. print(upload_path)
  53. f.save(upload_path)
  54. # 使用Opencv转换一下图片格式和名称
  55. # img = cv2.imread(upload_path)
  56. # cv2.imwrite(os.path.join(basepath, 'static/images', 'test.jpg'), img)
  57. # maple = requests.post('http://127.0.0.1:6666/static_pic',json={'path':upload_path}).text
  58. # print(maple)
  59. run_cut(upload_path,tabel=False)
  60. text = get_text(online=False)
  61. images = get_image()
  62. return render_template('show.html', w=500, contents=text,
  63. images=images
  64. )
  65. return render_template('upload.html')
  66. if __name__ == '__main__':
  67. app.run('0.0.0.0',port=12535)