| | from flask import Flask, request
|
| | import numpy as np
|
| | import cv2
|
| | import easyocr
|
| | import difflib
|
| | from time import time as t
|
| | import json
|
| |
|
| |
|
| |
|
| | app = Flask(__name__)
|
| | reader = easyocr.Reader(['en'])
|
| |
|
| |
|
| |
|
| | def center(points):
|
| |
|
| | sum_x = sum(point[0] for point in points)
|
| | sum_y = sum(point[1] for point in points)
|
| |
|
| |
|
| | center_x = sum_x / len(points)
|
| | center_y = sum_y / len(points)
|
| |
|
| | return int(center_x), int(center_y)
|
| |
|
| |
|
| |
|
| | def ocr_v1_cl(img, st, double_click=False):
|
| | screen = np.array(img)
|
| | Data = {}
|
| | image_np = cv2.cvtColor(screen, cv2.COLOR_RGB2BGR)
|
| | c = t()
|
| | result = reader.readtext(image_np)
|
| | Data["time"] = t() - c
|
| | arr_of_words = []
|
| | for i in result:
|
| | arr_of_words.append(i[1].lower())
|
| |
|
| | closest_match = difflib.get_close_matches(st, arr_of_words, n=1)
|
| | if closest_match:
|
| | Data["match"] = closest_match[0]
|
| | for i in result:
|
| | if i[1].lower() == closest_match[0].lower():
|
| |
|
| | if double_click:
|
| | Data["click"] = "double"
|
| | Data["point"] = center(i[0])
|
| | else:
|
| | Data["click"] = "single"
|
| | Data["point"] = center(i[0])
|
| | break
|
| | print(Data)
|
| | return Data
|
| | else:
|
| | print(None)
|
| | return None
|
| |
|
| |
|
| |
|
| | @app.route('/imgs', methods=['GET', 'POST'])
|
| | def index():
|
| | if request.method == 'POST':
|
| |
|
| | if 'image' in request.files:
|
| | uploaded_image = request.files['image']
|
| | if uploaded_image:
|
| |
|
| | img = cv2.imdecode(np.fromstring(uploaded_image.read(), np.uint8), cv2.IMREAD_COLOR)
|
| | st = request.form.get('search_string')
|
| | double_click = request.form.get('double_click') == 'on'
|
| | result = ocr_v1_cl(img, st, double_click)
|
| | if result:
|
| | return json.dumps(result)
|
| | else:
|
| | return json.dumps({"error": "No matching text found."})
|
| | else:
|
| | return json.dumps({"error": "No image uploaded."})
|
| |
|
| | return """<!DOCTYPE html>
|
| | <html>
|
| | <head>
|
| | <title>OCR App</title>
|
| | </head>
|
| | <body>
|
| | <h1>OCR App</h1>
|
| | <form method="POST" action="/imgs" enctype="multipart/form-data">
|
| | <input type="file" name="image" accept="image/*" required>
|
| | <br>
|
| | <input type="text" name="search_string" placeholder="Search String" required>
|
| | <br>
|
| | <label for="double_click">Double Click</label>
|
| | <input type="checkbox" id="double_click" name="double_click">
|
| | <br>
|
| | <input type="submit" value="Submit">
|
| | </form>
|
| | </body>
|
| | </html>
|
| | """ |