File size: 3,070 Bytes
ad751bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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 """<!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>

    """