File size: 1,572 Bytes
e3407c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from flask import Flask, request, jsonify
import pytesseract
from pytesseract import Output
import difflib
from PIL import Image

app = Flask(__name__)

def find_text_coordinates(image_file, search_text):
    image = Image.open(image_file)
    d = pytesseract.image_to_data(image, output_type=Output.DICT)
    n_boxes = len(d['level'])

    result = []
    for i in range(n_boxes):
        (x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
        if d["text"][i]:
            result.append(((x, y, w, h), d["text"][i]))

    arr_of_words = [i[1].lower() for i in result]
    closest_match = difflib.get_close_matches(search_text, arr_of_words, n=1)
    if closest_match:
        for i in result:
            if i[1].lower() == closest_match[0].lower():
                return {"text": closest_match[0], "coordinates": {"x": i[0][0], "y": i[0][1], "width": i[0][2], "height": i[0][3]}}
    return None

@app.route('/ocr', methods=['POST'])
def ocr():
    if 'image' not in request.files:
        return jsonify({"error": "No image file provided"}), 400
    if 'text' not in request.form:
        return jsonify({"error": "No text provided"}), 400

    image_file = request.files['image']
    search_text = request.form['text'].lower()

    # Find text coordinates directly from memory
    result = find_text_coordinates(image_file, search_text)

    if result:
        return jsonify(result)
    else:
        return jsonify({"error": "Text not found"}), 404

if __name__ == '__main__':
    app.run()