forked from ecthros/uncaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ris.py
262 lines (241 loc) · 8.88 KB
/
ris.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# import image_slicer
import requests
import os
import time
import random
import json
import threading
import multiprocessing
import time
import copy
import pickle
from PIL import Image
from thesaurus import get_synonyms
from clarifai.rest import ClarifaiApp
from clarifai.rest import Image as ClImage
from os import walk
from selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
TASK = "task2"
IMAGE_NAME = 'image2'
FILE_TYPE = "jpeg"
TEST_IMAGE = "images/task2/image2_04_01.png"
CLARIFAI_APP_ID="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
CLARIFAI_APP_SECRET="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
DEBUG=0
# image_slicer.slice(IMAGE_NAME + "." + FILE_TYPE, 16)
# os.system("mv %s_* images/%s" % (IMAGE_NAME, TASK))
# images = []
# f = []
# for (_, _, filenames) in walk("images/"+TASK+"/"):
# f.extend(filenames)
def parse_test_file(test_filename):
return json.loads(open(test_filename, "r").read())
def test_all():
dirs = list()
test_root = "images"
for (dirName, subDir, _) in walk(test_root):
dirs.extend(subDir)
break
for d in dirs:
full_path = os.path.join(test_root, d)
try:
search_directory(full_path)
except Exception as exc:
print("test %s failed" % full_path)
print(exc.message)
def search_directory(directory, target_keyword=None, width=4):
f = []
trues = 0
threads = []
oracle = None
try:
oracle = parse_test_file(os.path.join(directory, "oracle.json"))
if target_keyword == None:
target_keyword = oracle["target_keyword"]
except IOError as err:
print("no oracle file found")
manage_vars = multiprocessing.Manager()
for (_, _, filenames) in walk(directory):
f.extend([file for file in filenames if "image" in file or "output" in file])
i = 0
ret_vals = manage_vars.dict()
target_syns = list()
for targ_key in target_keyword.split():
target_syns.extend(get_synonyms(targ_key))
target_syns.append(targ_key)
print("testing " + directory)
for img_file in f:
t = multiprocessing.Process(target=reverse_search2, args=(os.path.join(directory, img_file), img_file, ret_vals, target_syns))
threads.append(t)
t.start()
i+=1
for j in range(0, i-1):
threads[j].join()
print("")
# print ret_vals
# print oracle
if oracle: # local testing only
for img_file in ret_vals.keys():
# print str(ret_vals[img_file]) + " " + str(oracle[img_file])
if(ret_vals[img_file] == oracle[img_file]):
trues += 1
print(" %s correct out of %s" % (str(trues), len(ret_vals)))
return ret_vals
else: # live testing only
return get_coor(ret_vals, width)
def reverse_search2(img_file, filename, ret_vals, target_keyword="vehicle"):
ret_vals[filename] = reverse_search(img_file, target_keyword)
def start_captcha():
driver = webdriver.Firefox()
driver.get("http://reddit.com")
driver.find_element(By.XPATH, "//*[@id=\"header-bottom-right\"]/span[1]/a").click()
time.sleep(1)
driver.find_element(By.ID, "user_reg").send_keys("qwertyuiop091231")
driver.find_element(By.ID, "passwd_reg").send_keys("THISISMYPASSWORD!!$")
driver.find_element(By.ID, "passwd2_reg").send_keys("THISISMYPASSWORD!!$")
driver.find_element(By.ID, "email_reg").send_keys("[email protected]")
#driver.find_element_by_tag_name("body").send_keys(Keys.COMMAND + Keys.ALT + 'k')
iframeSwitch = driver.find_element(By.XPATH, "//*[@id=\"register-form\"]/div[6]/div/div/div/iframe")
driver.switch_to.frame(iframeSwitch)
driver.find_element(By.ID, "recaptcha-anchor").click()
# download captcha image
#
# split payload
#
# reverse_search
#
# driver.quit()
# determines if an image keywords matches the target keyword
# uses the synonyms of the image keyword
def check_image(img_keywords, target_syns, syn_image=False):
#print ("Checking keywords against: " + target_keyword)
for k in img_keywords:
#print(k)
if syn_image:
image_syns = get_synonyms(k)
if image_syns:
for image_s in image_syns:
for target_s in target_syns:
# print("- %s" % (target_s))
if target_s == image_s:
return True
else:
for target_s in target_syns:
# print("- %s" % (target_s))
if target_s == k:
if (DEBUG > 0):
print("Found " + target_s + " equal to " + k)
return True
return False
def get_coor(click_dict, width=4):
x = 1
y = 1
coor_dict = dict()
for key in sorted(click_dict.keys()):
coor_dict[(x, y)] = click_dict[key]
y += 1
if y > width:
x += 1
y = 1
return coor_dict
# returns an array of possible subjects of the image
def reverse_search(img_file, target_syns):
json_arr = clarifai(img_file)
keyword_arr = parse_clarifai(json_arr)
correct_image = check_image(keyword_arr, target_syns)
#print(correct_image)
return correct_image
# sumbit a request to clarifai for reverse image search
# returns json object
def clarifai(img_file):
# print("Querying clarifai..." + img_file)
success = False
model = None
while not success:
try:
img = ClImage(filename=img_file)
app = ClarifaiApp(CLARIFAI_APP_ID, CLARIFAI_APP_SECRET)
model = app.models.get('general-v1.3')
success = True
except Exception as e:
time.sleep(0.5)
print(e)
print("."),
return model.predict([img])
def pprint(matrix):
s = [[str(e) for e in row] for row in matrix]
lens = [max(map(len, col)) for col in zip(*s)]
fmt = '\t'.join('{{:{}}}'.format(x) for x in lens)
table = [fmt.format(*row) for row in s]
print '\n'.join(table)
# parse the json array from clarifai into a single dict of names->values
# returns array sorted by rank (value)
def parse_clarifai(json_arr):
# array = json.loads(json_arr)
# print("- parsing json")
ret_arr = list()
#outputs > data > concepts > name & value
for data in json_arr["outputs"][0]["data"]["concepts"]:
# ret_arr[data["name"]] = data["value"]
ret_arr.append(data["name"])
return ret_arr
if __name__ == "__main__":
x_sections = 4
y_sections = 4
TASK_PATH = "images/taskg"
IMAGE_PATH = "images/taskg/payload2.jpeg"
CACHE_PATH = TASK_PATH+"/cache2.pickle"
FROM_CACHE = False
if os.path.exists(CACHE_PATH):
print("Found saved cache, reading")
with open(CACHE_PATH, 'rb') as handle:
cache = pickle.load(handle)
FROM_CACHE = True
else:
cache = []
with Image.open(IMAGE_PATH) as img:
width, height = img.size
x_window = width/x_sections
y_window = height/y_sections
winstep = 1
ans = [[False for z in range(0,int(x_sections*winstep*2))] for z in range(0,int(y_sections*winstep*2))]
toclick = [[False for z in range(0,x_sections)] for z in range(0,y_sections)]
pprint(ans)
curx, cury = 0, 0
pid = 0
xidx, yidx = 0, 0
for curx in range(0, width - int(x_window*winstep), x_window/2):
yidx = 0
for cury in range(0, height-int(y_window*winstep), y_window/2):
major_box = (curx % x_window == 0 and cury % y_window == 0)
i = img.crop((curx, cury, curx + int(x_window*winstep), cury + int(y_window*winstep)))
i.save(TASK_PATH+"/slice2_%03d.jpeg"%pid, "jpeg")
if FROM_CACHE:
answers = cache[pid]
else:
answers = parse_clarifai(clarifai(TASK_PATH+"/slice2_%03d.jpeg"%pid))
cache.append(answers)
print answers
decision = "symbol" in answers or "signalise" in answers or "picture frame" in answers
ans[yidx][xidx] = decision
if major_box:
toclick[yidx/2][xidx/2] = decision
print "%d: %s" % (pid, decision)
pprint(toclick)
#i.show()
#time.sleep(0.5)
#raw_input("")
print "IDX %dx%d [pid %d]: IMAGE %dx%d (%d by %d sections) with window sizes %d x %d" % (xidx, yidx, pid, width, height, x_sections, y_sections, x_window, y_window)
yidx += 1
pid += 1
xidx += 1
with open(CACHE_PATH, 'wb') as handle:
pickle.dump(cache, handle)
pprint(toclick)
print("")
pprint(ans)