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): # Calculate the sum of x and y coordinates sum_x = sum(point[0] for point in points) sum_y = sum(point[1] for point in points) # Calculate the center coordinate center_x = sum_x / len(points) center_y = sum_y / len(points) return int(center_x), int(center_y) # Print the result 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': # Handle the uploaded image and perform OCR if 'image' in request.files: uploaded_image = request.files['image'] if uploaded_image: # Process the 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) # Return JSON response else: return json.dumps({"error": "No matching text found."}) # Return JSON error response else: return json.dumps({"error": "No image uploaded."}) # Return JSON error response return """