OCR / api.py
Rzx008's picture
Update api.py
0b82dfb verified
raw
history blame contribute delete
953 Bytes
from flask import Flask, request, jsonify
from rapidocr_onnxruntime import RapidOCR
from PIL import Image
import numpy as np
app = Flask(__name__)
engine = RapidOCR()
@app.route('/')
def index():
return "Welcome to the OCR API"
@app.route('/ocr', methods=['POST'])
def process_ocr():
if 'image' not in request.files:
return jsonify({'error': 'No image file provided'}), 400
text_results = []
for img_file in request.files.getlist('image'):
img = Image.open(img_file)
lebar, tinggi = img.size
potong_atas = 201
area_crop = (0, potong_atas, lebar, tinggi // 2)
img_crop = img.crop(area_crop)
img_array = np.array(img_crop)
result, _ = engine(img_array)
text_array = [item[1] for item in result]
text_results.append(text_array)
return jsonify({'text': text_results})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=True)