49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
from flask import Flask, request, jsonify
|
|
import base64
|
|
import io
|
|
import cv2
|
|
import traceback
|
|
import os
|
|
import logging
|
|
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(levelname)s] %(message)s', handlers=[logging.StreamHandler()])
|
|
from cv_processor import process
|
|
|
|
app = Flask(__name__)
|
|
|
|
@app.route('/process', methods=['POST'])
|
|
def process_image():
|
|
logging.info("POST /process")
|
|
# Json
|
|
data = request.get_json()
|
|
# Valid data?
|
|
if not data or 'image' not in data:
|
|
return jsonify({"error": "No image data provided"}), 400
|
|
|
|
try:
|
|
image_data = data['image']
|
|
|
|
# Decode base64 string
|
|
image_bytes = base64.b64decode(image_data)
|
|
image_stream = io.BytesIO(image_bytes)
|
|
|
|
# Process the image
|
|
results = process(image_stream)
|
|
|
|
# Encode processed image to base64
|
|
_, buffer = cv2.imencode('.jpg', results.get("image"), [cv2.IMWRITE_JPEG_QUALITY, 100])
|
|
processed_image_base64 = base64.b64encode(buffer).decode('utf-8')
|
|
|
|
# Update image with base64 encoded
|
|
results["image_b64"] = processed_image_base64
|
|
# Pop image (not serializable)
|
|
results.pop("image")
|
|
# Jsonify
|
|
return jsonify(results)
|
|
|
|
except Exception as e:
|
|
logging.warning("Exception: {}".format(traceback.format_exc()))
|
|
return jsonify({"error": traceback.format_exc()}), 400
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=os.getenv("DEBUG_MODE", False))
|