-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
173 lines (115 loc) · 4.2 KB
/
main.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
import pyscreenshot as imagegrab
import pytesseract
from PIL import Image
import PIL.ImageOps
import webbrowser
def screen_grab_loco_ques(to_save):
# Screen cap the left side of the desktop for loco
im = imagegrab.grab(bbox=(37, 306, 359, 389))
im.save(to_save)
return
def screen_grab_hq_ques(to_save):
# Screen cap the left side of the desktop for HQ Trivia
im = imagegrab.grab(bbox=(42, 209, 354, 325))
im.save(to_save)
return
def screen_grab_cashshow(to_save):
# Screen cap the left side of the desktop for The Cash Show
im = imagegrab.grab(bbox=(28, 203, 346, 284))
im.save(to_save)
return
def screen_grab_brainbazzi(to_save):
# Screen cap the left side of the desktop for Brain Bazzi
im = imagegrab.grab(bbox=(26, 273, 367, 367))
im.save(to_save)
return
def screen_grab_theq(to_save):
# Screen cap the left side of the desktop for The Q
im = imagegrab.grab(bbox=(11, 440, 379, 534))
im.save(to_save)
return
def screen_grab_qureka(to_save):
# Screen cap the left side of the desktop for Quereka
im = imagegrab.grab(bbox=(47, 284, 346, 355))
im.save(to_save)
return
# Since loco has a dark background and light text the performance of OCR
# isn't great. So by inverting the color of the screen cap OCR performs
# better
def invert_ques_loco(filename):
image = Image.open(filename)
inverted_image = PIL.ImageOps.invert(image)
inverted_image_loc = "Screens/Inverted_images/invert.png"
inverted_image.save(inverted_image_loc)
return inverted_image_loc
def read_screen(filename):
# Call pytesseract image to string function
text = pytesseract.image_to_string(Image.open(filename))
return text
def perform_search(query):
# Do Google search of the question
url = "https://www.google.co.in/search?q=" + query
webbrowser.open_new(url)
def perform_search_wolframalpha(query):
# Search the question on Wolframalpha
# Wolframalpha is much slower than google so it's not prefered
url = "http://www.wolframalpha.com/input/?i=" + query
webbrowser.open_new(url)
def parse_question(filename):
text = read_screen(filename)
# Take the raw output from tesseract and make it more search friendly
lines = text.splitlines()
question = ""
options = list()
flag = False
for line in lines:
if not flag:
question = question+" "+line
if '?' in line:
flag = True
continue
if flag:
if line != '':
options.append(line)
new_question = question.replace('"', '')
return new_question
if __name__ == '__main__':
screenshot_file = "Screens/to_ocr.png"
prompt = '> '
print("Select An Option:\n1-Loco 2-HQ Trivia 3-The Cash Show 4-Brain Bazzi 5-The Q 6-Qureka")
op = int(input(prompt))
print("Press s to Screenshot and q to quit")
res = ''
while res != 'q':
res = input(prompt)
question = ''
if res == 's':
if op == 1:
screen_grab_loco_ques(screenshot_file)
inverted_loc = invert_ques_loco(screenshot_file)
question = parse_question(inverted_loc)
if op == 2:
screen_grab_hq_ques(screenshot_file)
question = parse_question(screenshot_file)
if op == 3:
screen_grab_cashshow(screenshot_file)
question = parse_question(screenshot_file)
if op == 4:
screen_grab_brainbazzi(screenshot_file)
question = parse_question(screenshot_file)
if op == 5:
screen_grab_theq(screenshot_file)
inverted_loc = invert_ques_loco(screenshot_file)
question = parse_question(inverted_loc)
if op == 6:
screen_grab_qureka(screenshot_file)
inverted_loc = invert_ques_loco(screenshot_file)
question = parse_question(inverted_loc)
print("Question : ")
print(question)
print()
perform_search(question)
elif res == 'q':
break
else:
print("Wrong option try again")