33 lines
885 B
Python
33 lines
885 B
Python
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()
|