DIVY118 commited on
Commit
ad751bd
1 Parent(s): 52ded87

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request
2
+ import numpy as np
3
+ import cv2
4
+ import easyocr
5
+ import difflib
6
+ from time import time as t
7
+ import json
8
+
9
+
10
+
11
+ app = Flask(__name__)
12
+ reader = easyocr.Reader(['en'])
13
+
14
+
15
+
16
+ def center(points):
17
+ # Calculate the sum of x and y coordinates
18
+ sum_x = sum(point[0] for point in points)
19
+ sum_y = sum(point[1] for point in points)
20
+
21
+ # Calculate the center coordinate
22
+ center_x = sum_x / len(points)
23
+ center_y = sum_y / len(points)
24
+
25
+ return int(center_x), int(center_y)
26
+
27
+
28
+ # Print the result
29
+ def ocr_v1_cl(img, st, double_click=False):
30
+ screen = np.array(img)
31
+ Data = {}
32
+ image_np = cv2.cvtColor(screen, cv2.COLOR_RGB2BGR)
33
+ c = t()
34
+ result = reader.readtext(image_np)
35
+ Data["time"] = t() - c
36
+ arr_of_words = []
37
+ for i in result:
38
+ arr_of_words.append(i[1].lower())
39
+
40
+ closest_match = difflib.get_close_matches(st, arr_of_words, n=1)
41
+ if closest_match:
42
+ Data["match"] = closest_match[0]
43
+ for i in result:
44
+ if i[1].lower() == closest_match[0].lower():
45
+
46
+ if double_click:
47
+ Data["click"] = "double"
48
+ Data["point"] = center(i[0])
49
+ else:
50
+ Data["click"] = "single"
51
+ Data["point"] = center(i[0])
52
+ break
53
+ print(Data)
54
+ return Data
55
+ else:
56
+ print(None)
57
+ return None
58
+
59
+
60
+
61
+ @app.route('/imgs', methods=['GET', 'POST'])
62
+ def index():
63
+ if request.method == 'POST':
64
+ # Handle the uploaded image and perform OCR
65
+ if 'image' in request.files:
66
+ uploaded_image = request.files['image']
67
+ if uploaded_image:
68
+ # Process the uploaded image
69
+ img = cv2.imdecode(np.fromstring(uploaded_image.read(), np.uint8), cv2.IMREAD_COLOR)
70
+ st = request.form.get('search_string')
71
+ double_click = request.form.get('double_click') == 'on'
72
+ result = ocr_v1_cl(img, st, double_click)
73
+ if result:
74
+ return json.dumps(result) # Return JSON response
75
+ else:
76
+ return json.dumps({"error": "No matching text found."}) # Return JSON error response
77
+ else:
78
+ return json.dumps({"error": "No image uploaded."}) # Return JSON error response
79
+
80
+ return """<!DOCTYPE html>
81
+ <html>
82
+ <head>
83
+ <title>OCR App</title>
84
+ </head>
85
+ <body>
86
+ <h1>OCR App</h1>
87
+ <form method="POST" action="/imgs" enctype="multipart/form-data">
88
+ <input type="file" name="image" accept="image/*" required>
89
+ <br>
90
+ <input type="text" name="search_string" placeholder="Search String" required>
91
+ <br>
92
+ <label for="double_click">Double Click</label>
93
+ <input type="checkbox" id="double_click" name="double_click">
94
+ <br>
95
+ <input type="submit" value="Submit">
96
+ </form>
97
+ </body>
98
+ </html>
99
+ """