#!/usr/bin/env python3 import cgi import os import subprocess UPLOAD_DIR = './uploads/' def main(): print("Content-Type: application/json\n") form = cgi.FieldStorage() fileitem = form['file'] if fileitem.filename: fn = os.path.basename(fileitem.filename) file_path = os.path.join(UPLOAD_DIR, fn) open(file_path, 'wb').write(fileitem.file.read()) result = run_ocr_script(file_path) print(result) os.remove(file_path) else: print("No file was uploaded.") def run_ocr_script(file_path): try: completed_process = subprocess.run(['./ocr.sh', file_path], check=True, text=True, capture_output=True) return completed_process.stdout except subprocess.CalledProcessError as e: return f"An error occurred: {e}" except Exception as e: return f"Unexpected error: {e}" if __name__ == '__main__': main()