优化接口图片传输方式
This commit is contained in:
37
det_api.py
37
det_api.py
@@ -1,8 +1,7 @@
|
||||
import base64
|
||||
import os.path
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
from flask import Flask, request, jsonify, Blueprint
|
||||
from flask import Flask, request, Blueprint
|
||||
|
||||
from paddle_detection import detector
|
||||
from util.common_util import process_request
|
||||
@@ -12,25 +11,23 @@ det_bp = Blueprint('det_bp', __name__)
|
||||
app.register_blueprint(det_bp, url_prefix='/det')
|
||||
|
||||
|
||||
@det_bp.route("/books", methods=['POST'])
|
||||
@det_bp.route('/books', methods=['POST'])
|
||||
@process_request
|
||||
def books():
|
||||
try:
|
||||
file = request.files['image']
|
||||
image_data = file.read()
|
||||
nparr = np.frombuffer(image_data, np.uint8)
|
||||
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
||||
result = detector.get_book_areas(image)
|
||||
encoded_images = []
|
||||
for i in result:
|
||||
_, encoded_image = cv2.imencode('.jpg', i)
|
||||
byte_stream = encoded_image.tobytes()
|
||||
img_str = base64.b64encode(byte_stream).decode('utf-8')
|
||||
encoded_images.append(img_str)
|
||||
return jsonify(encoded_images), 200
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
img_path = request.form['img_path']
|
||||
image = cv2.imread(img_path)
|
||||
result = detector.get_book_areas(image)
|
||||
|
||||
dirname = os.path.dirname(img_path)
|
||||
img_name, ext = os.path.basename(img_path).rsplit('.', 1)
|
||||
books_path = []
|
||||
for i in range(len(result)):
|
||||
save_path = os.path.join(dirname, img_name + '_book_' + str(i) + '.' + ext)
|
||||
with open(save_path, 'wb') as file:
|
||||
file.write(result[i])
|
||||
books_path.append(save_path)
|
||||
return books_path
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run("0.0.0.0", 5000)
|
||||
app.run('0.0.0.0', 5000)
|
||||
|
||||
Reference in New Issue
Block a user