-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanswer_bot_test_funzionante.py
228 lines (200 loc) · 7.16 KB
/
answer_bot_test_funzionante.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import cv2
import pytesseract
import os
import requests
from bs4 import BeautifulSoup
import multiprocessing
import wx
import pyscreenshot as Imagegrab
""" QUESITO 3 RIGHE:
# SCREEN CONSTANTS
LEFT_QUESTION = 40
RIGHT_QUESTION = 1035
QUESTION_TOP = 500
QUESTION_BOTTOM = 760
LEFT_OPTION = 106
RIGHT_OPTION = 970
OPTION_HEIGHT = 190
ROW_HEIGHT = 36
OPTION_POSITION = [800, 1082, 1274]
"""
""" QUESITO 2 RIGHE:
# SCREEN CONSTANTS
LEFT_QUESTION = 40
RIGHT_QUESTION = 1035
QUESTION_TOP = 500
QUESTION_BOTTOM = 760
LEFT_OPTION = 106
RIGHT_OPTION = 970
OPTION_HEIGHT = 190
ROW_HEIGHT = 36
OPTION_POSITION = [800, 1021, 1274]
"""
# PATH
SCREENSHOT = "screenshot.png"
DOMAIN = "https://www.google.it/search?oe=utf8&ie=utf8&source=uds&start=0&hl=it&q="
INDICE = 0
Debug = False
# COLORS
class colors:
GREEN = '\033[32m'
YELLOW = '\033[33m'
RED = '\033[31m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
STOP_UNDERLINE = '\033[24m'
END = '\033[0m'
# SCREEN CONSTANTS
LEFT_QUESTION = 40
RIGHT_QUESTION = 1035
QUESTION_TOP = 500
QUESTION_BOTTOM = 760
LEFT_OPTION = 106
RIGHT_OPTION = 970
OPTION_HEIGHT = 190
ROW_HEIGHT = 36
OPTION_POSITION = [800, 1021, 1274]
def get_number_of_results_1(data):
image = data[0]
position = data[1]
row_count = data[2]
question_text = data[3]
# Crop image
top = position + row_count * ROW_HEIGHT
bottom = top + OPTION_HEIGHT
option = image[top:bottom, LEFT_OPTION:RIGHT_OPTION]
# Read option text
option_text = pytesseract.image_to_string(option, lang='ita').replace('\n', ' ')
# Create query
#query = DOMAIN + (question_text + ' ' + option_text + ' ' + 'wiki&tbs=li:1').replace(' ', '+')
query = DOMAIN + (question_text + ' ' + option_text + ' ' + 'wiki').replace(' ', '+')
if Debug:
print(query)
# Perform request and read results
r = requests.get(query)
soup = BeautifulSoup(r.text, 'lxml')
results = soup.find('div',{'id':'resultStats'}).text.split()[1].replace('.', '')
#print(int(results))
return option_text, int(results)
def get_number_of_results_2(data):
image = data[0]
position = data[1]
row_count = data[2]
question_text = data[3]
# Crop image
top = position + row_count * ROW_HEIGHT
bottom = top + OPTION_HEIGHT
option = image[top:bottom, LEFT_OPTION:RIGHT_OPTION]
# Read option text
option_text = pytesseract.image_to_string(option, lang='ita').replace('\n', ' ')
# Create query
#query = DOMAIN + (question_text + ' ' + option_text + '&tbs=li:1').replace(' ', '+')
query = DOMAIN + (question_text + ' ' + option_text).replace(' ', '+')
if Debug:
print(query)
# Perform request and read results
r = requests.get(query)
soup = BeautifulSoup(r.text, 'lxml')
results = soup.find('div',{'id':'resultStats'}).text.split()[1].replace('.', '')
#print(int(results))
return option_text, int(results)
def maximum(a, b, c):
if (a >= b) and (a >= b):
largest = a
elif (b >= a) and (b >= a):
largest = b
else:
largest = c
return largest
def minium(a, b, c):
if (a <= b) and (a <= b):
largest = a
elif (b <= a) and (b <= a):
largest = b
else:
largest = c
return largest
def print_results(result_list_1, result_list_2, negative):
x = -1
total_1 = sum(n_1 for _, n_1 in result_list_1)
total_2 = sum(n_2 for _, n_2 in result_list_2)
total = total_1 + total_2
sum_option_1 = (((result_list_1[0][1])*2) + result_list_2[0][1])
sum_option_2 = (((result_list_1[1][1])*2) + result_list_2[1][1])
sum_option_3 = (((result_list_1[2][1])*2) + result_list_2[2][1])
sum_option = [sum_option_1, sum_option_2, sum_option_3]
if Debug:
print(sum_option_1)
print(sum_option_2)
print(sum_option_3)
print(sum_option)
if negative:
guess = minium(sum_option_1, sum_option_2, sum_option_3)
else:
guess = maximum(sum_option_1, sum_option_2, sum_option_3)
if Debug:
print()
print(guess)
print("\n")
for key in result_list_1:
x = x + 1
if guess == sum_option[x]:
print(colors.BOLD + " • ", end='')
print(colors.GREEN + colors.BOLD, end='')
print(result_list_1[x][0], end=' ')
print(colors.END + colors.BOLD, end='')
print("{ punti: " + "%d" % sum_option[x] + " }\n")
print(colors.END, end='')
else:
print(colors.BOLD + " • ", end='')
print(result_list_1[x][0], end=' ')
print("{ punti: " + "%d" % sum_option[x] + " }\n")
print("-----------------------------------------------------------------------")
def manage_question():
# Import image and remove screenshot from directory
image = cv2.imread(r"C:\Users\Gabriele\Desktop\Screenshots\3.png", cv2.IMREAD_GRAYSCALE)
# os.remove(SCREENSHOT)
# Threshold images for OCR
ret, question = cv2.threshold(image[QUESTION_TOP:QUESTION_BOTTOM,
LEFT_QUESTION:RIGHT_QUESTION], 200, 255, cv2.THRESH_BINARY_INV)
ret, options = cv2.threshold(image, 200, 255, cv2.THRESH_BINARY)
# Read question text (determining number of rows)
question_text = pytesseract.image_to_string(question, lang='ita')
if not question_text:
print(colors.RED + colors.BOLD + "Something went wrong reading the screenshot!"
+ colors.YELLOW + " Please try again." + colors.END)
else:
global INDICE
INDICE = INDICE + 1
negative = "NON" in question_text
row_count = question_text.count('\n')
question_text = question_text.replace('\n', ' ')
print("\n" + str(INDICE) + ". " + colors.YELLOW + colors.UNDERLINE + question_text + colors.STOP_UNDERLINE + colors.END + "\n")
# Get number of results of each option in parallel
pool = multiprocessing.Pool(processes=3)
data = []
for i in range(0, 3):
data.append((options, OPTION_POSITION[i], row_count, question_text))
try:
result_list_1 = pool.map(get_number_of_results_1, data)
result_list_2 = pool.map(get_number_of_results_2, data)
# Print results
if Debug:
print()
print(result_list_1)
print(result_list_2)
print_results(result_list_1, result_list_2, negative)
except:
print("\n")
print(colors.RED + colors.BOLD + "The query produced no result."
+ colors.YELLOW + " Please try again." + colors.END)
if __name__ == "__main__":
os.system("")
while True:
key = input(colors.BOLD + "\nPress " + colors.BOLD + colors.GREEN + "ENTER" + colors.END + colors.BOLD + " to take a screenshot" +
" of the question or press " + colors.BOLD + colors.RED + "q" + colors.END + colors.BOLD + " to quit: ")
if not key:
print()
manage_question()
elif key == 'q':
break