|
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()
|
|
|
|
|
|
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()
|
|
|