Search code examples
pythonandroidkotlinimage-processingocr

How do I link a Kotlin and Python project?


I have a python project with OCR, Image Processing steps. I want to do OCR operations by sending the photo I took with the Kotlin project to this Python file. How do I connect these two projects?

Can I use flask, django, cloud technologies etc. Which method would be easier?


Solution

  • I recommend using Flask, Its pretty easy to setup and straight forward.

    Script for Flask:

    from flask import Flask, request, jsonify
    import cv2
    import numpy as np
    
    app = Flask(__name__)
    
    @app.route('/process_image', methods=['POST'])
    def process_image():
        try:
            if 'input_image' not in request.files:
                return jsonify({'error': 'No file part'})
            
            file = request.files['input_image']
            imgarray = cv2.imdecode(np.fromstring(file.read(), np.uint8), cv2.IMREAD_COLOR)
    
            # Do anything you want with it
    
            cv2.destroyAllWindows()
    
            # Return JSON output to kotlin file
            return jsonify({'output_image': 'Put the output image here'})
        except Exception as e:
            return jsonify({'error': str(e)})
    
    if __name__ == '__main__':
        # Turn off debug in production
        app.run(debug=True)