from flask import Flask, request, jsonify from paddleocr import PaddleOCR import base64 from PIL import Image from io import BytesIO import traceback import numpy as np # Importieren von numpy app = Flask(__name__) ocr = PaddleOCR(use_angle_cls=True, lang='en') # Passen Sie die Sprache nach Bedarf an @app.route('/ocr', methods=['POST']) def ocr_endpoint(): try: if not request.is_json: return jsonify({'error': 'Content-Type must be application/json'}), 400 data = request.get_json() if not data or 'image' not in data: return jsonify({'error': 'No image provided'}), 400 image_b64 = data['image'] if not image_b64: return jsonify({'error': 'Empty image data'}), 400 try: image_data = base64.b64decode(image_b64) except Exception as decode_err: return jsonify({'error': 'Base64 decode error', 'details': str(decode_err)}), 400 try: image = Image.open(BytesIO(image_data)).convert('RGB') image = np.array(image) # Konvertieren zu numpy.ndarray except Exception as img_err: return jsonify({'error': 'Invalid image data', 'details': str(img_err)}), 400 # Optional: Bildgröße anpassen, falls erforderlich # PaddleOCR kann große Bilder verarbeiten, aber zur Effizienz können Sie eine maximale Größe setzen max_width = 1920 max_height = 1080 height, width, _ = image.shape if width > max_width or height > max_height: aspect_ratio = width / height if aspect_ratio > 1: new_width = max_width new_height = int(max_width / aspect_ratio) else: new_height = max_height new_width = int(max_height * aspect_ratio) image = np.array(Image.fromarray(image).resize((new_width, new_height))) result = ocr.ocr(image, rec=True, cls=True) return jsonify(result) except Exception as e: traceback.print_exc() return jsonify({'error': str(e)}), 500 if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=True)