优化文档检测为接口
This commit is contained in:
32
det_api.py
Normal file
32
det_api.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import base64
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
from flask import Flask, request, jsonify
|
||||
|
||||
from paddle_detection import detector
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.route("/det/detect_books", methods=['POST'])
|
||||
def detect_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
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
Reference in New Issue
Block a user