-
Notifications
You must be signed in to change notification settings - Fork 0
/
Egoshare_4_GUI.py
executable file
·349 lines (267 loc) · 16 KB
/
Egoshare_4_GUI.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!coding: utf-8
import thread
import os
import time
import wx
import traceback
import Image
import wx.lib.newevent
import Break_Egoshare_Captcha
#TRACEBACK
import traceback
import sys
def Myexcepthook(type, value, tb):
lines=traceback.format_exception(type, value, tb)
f=open('log.txt', 'a')
f.write("\n".join(lines))
f.close()
sys.excepthook=Myexcepthook
MODEL_FOLDER = "Egoshare/Models"
DEFAULT_MODEL_FILE="model.svm"
CAPTCHA_FOLDER = "Egoshare/Rough Captchas"
DEFAULT_CAPTCHA_FILE = "Image011.jpg"
CAPTCHA_WIDTH = 80
CAPTCHA_HEIGHT = 25
PATCH_WIDTH = 20
class MyFrame(wx.Frame):
def __init__(self, zoom):
self.zoom = zoom
self.model = 1
wx.Frame.__init__(self, None, -1, "Egoshare Captcha Breaker", size=(600, 420))
taille = (CAPTCHA_WIDTH*zoom, CAPTCHA_HEIGHT*zoom)
self.image_input_window = wx.StaticBitmap(self, -1, size = taille, bitmap = wx.EmptyBitmap(*taille))
self.image_input_window.SetMinSize(taille)
##CHIFFRE1
taille = (PATCH_WIDTH*zoom,PATCH_WIDTH*zoom)
self.image_chiffre1 = wx.StaticBitmap(self, -1, size = taille, bitmap = wx.EmptyBitmap(*taille))
self.image_chiffre1.SetMinSize(taille)
self.text_resultat_chiffre1 = wx.StaticText(self, -1, "Resultat")
self.text_resultat_chiffre1.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.BOLD))
self.resultat_chiffre1 = wx.StaticText(self, -1)
self.resultat_chiffre1.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.NORMAL))
self.text_score_chiffre1 = wx.StaticText(self, -1, "Best score")
self.text_score_chiffre1.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.BOLD))
self.score_chiffre1 = wx.StaticText(self, -1)
self.score_chiffre1.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.NORMAL))
self.sizer_chiffre1 = wx.GridBagSizer()
self.sizer_chiffre1.Add(self.image_chiffre1, (1,1), span=(2,1), flag=wx.ALIGN_CENTER | wx.ALL, border=5)
self.sizer_chiffre1.Add(self.text_resultat_chiffre1, (1,2), flag=wx.ALIGN_CENTER | wx.ALL, border=5)
self.sizer_chiffre1.Add(self.text_score_chiffre1, (1,3), flag=wx.ALIGN_CENTER | wx.ALL, border=5)
self.sizer_chiffre1.Add(self.resultat_chiffre1, (2,2), flag=wx.ALIGN_CENTER | wx.ALL, border=5)
self.sizer_chiffre1.Add(self.score_chiffre1, (2,3), flag=wx.ALIGN_CENTER | wx.ALL, border=5)
##chiffre2
taille = (PATCH_WIDTH*zoom,PATCH_WIDTH*zoom)
self.image_chiffre2 = wx.StaticBitmap(self, -1, size = taille, bitmap = wx.EmptyBitmap(*taille))
self.image_chiffre2.SetMinSize(taille)
self.text_resultat_chiffre2 = wx.StaticText(self, -1, "Resultat")
self.text_resultat_chiffre2.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.BOLD))
self.resultat_chiffre2 = wx.StaticText(self, -1)
self.resultat_chiffre2.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.NORMAL))
self.text_score_chiffre2 = wx.StaticText(self, -1, "Best score")
self.text_score_chiffre2.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.BOLD))
self.score_chiffre2 = wx.StaticText(self, -1)
self.score_chiffre2.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.NORMAL))
self.sizer_chiffre2 = wx.GridBagSizer()
self.sizer_chiffre2.Add(self.image_chiffre2, (1,1), span=(2,1), flag=wx.ALIGN_CENTER | wx.ALL, border=5)
self.sizer_chiffre2.Add(self.text_resultat_chiffre2, (1,2), flag=wx.ALIGN_CENTER | wx.ALL, border=5)
self.sizer_chiffre2.Add(self.text_score_chiffre2, (1,3), flag=wx.ALIGN_CENTER | wx.ALL, border=5)
self.sizer_chiffre2.Add(self.resultat_chiffre2, (2,2), flag=wx.ALIGN_CENTER | wx.ALL, border=5)
self.sizer_chiffre2.Add(self.score_chiffre2, (2,3), flag=wx.ALIGN_CENTER | wx.ALL, border=5)
##chiffre3
taille = (PATCH_WIDTH*zoom,PATCH_WIDTH*zoom)
self.image_chiffre3 = wx.StaticBitmap(self, -1, size = taille, bitmap = wx.EmptyBitmap(*taille))
self.image_chiffre3.SetMinSize(taille)
self.text_resultat_chiffre3 = wx.StaticText(self, -1, "Resultat")
self.text_resultat_chiffre3.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.BOLD))
self.resultat_chiffre3 = wx.StaticText(self, -1)
self.resultat_chiffre3.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.NORMAL))
self.text_score_chiffre3 = wx.StaticText(self, -1, "Best score")
self.text_score_chiffre3.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.BOLD))
self.score_chiffre3 = wx.StaticText(self, -1)
self.score_chiffre3.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.NORMAL))
self.sizer_chiffre3 = wx.GridBagSizer()
self.sizer_chiffre3.Add(self.image_chiffre3, (1,1), span=(2,1), flag=wx.ALIGN_CENTER | wx.ALL, border=5)
self.sizer_chiffre3.Add(self.text_resultat_chiffre3, (1,2), flag=wx.ALIGN_CENTER | wx.ALL, border=5)
self.sizer_chiffre3.Add(self.text_score_chiffre3, (1,3), flag=wx.ALIGN_CENTER | wx.ALL, border=5)
self.sizer_chiffre3.Add(self.resultat_chiffre3, (2,2), flag=wx.ALIGN_CENTER | wx.ALL, border=5)
self.sizer_chiffre3.Add(self.score_chiffre3, (2,3), flag=wx.ALIGN_CENTER | wx.ALL, border=5)
self.main_sizer = wx.GridBagSizer()
self.main_sizer.Add(self.image_input_window, (1,1), span = (1,2), flag=wx.ALIGN_CENTER | wx.ALL, border=10)
self.main_sizer.Add(self.sizer_chiffre1, (2,1), flag=wx.ALIGN_CENTER | wx.ALL)
self.main_sizer.Add(self.sizer_chiffre2, (3,1), flag=wx.ALIGN_CENTER | wx.ALL)
self.main_sizer.Add(self.sizer_chiffre3, (4,1), flag=wx.ALIGN_CENTER | wx.ALL)
## PARTIE DE GAUCHE
self.sizer_model = wx.FlexGridSizer(rows = 1)
self.open_image = wx.Bitmap("open.bmp", wx.BITMAP_TYPE_BMP)
self.text_model = wx.StaticText(self, -1, "Ouvrir le modele")
self.text_model.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.BOLD))
self.path_model = wx.TextCtrl(self, -1, size = (100, -1))
self.path_model.SetEditable(False)
self.bouton_model = wx.BitmapButton(self, -1, self.open_image)
self.bouton_model.Bind(wx.EVT_BUTTON, self.OnSelectModel)
self.sizer_model.Add(self.text_model, flag = wx.ALIGN_CENTER | wx.RIGHT, border = 12)
self.sizer_model.Add(self.path_model, flag = wx.ALIGN_CENTER)
self.sizer_model.Add(self.bouton_model, flag = wx.ALIGN_CENTER)
self.sizer_captcha = wx.FlexGridSizer(rows = 1)
self.text_captcha = wx.StaticText(self, -1, "Selectionner le captcha")
self.text_captcha.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.BOLD))
self.bouton_captcha = wx.BitmapButton(self, -1, self.open_image)
self.bouton_captcha.Bind(wx.EVT_BUTTON, self.OnSelectCaptcha)
self.sizer_captcha.Add(self.text_captcha, flag = wx.ALIGN_CENTER | wx.RIGHT, border = 12)
self.sizer_captcha.Add(self.bouton_captcha, flag = wx.ALIGN_CENTER)
self.launchButton = wx.Button(self, -1, "Lancer le calcul")
self.launchButton.Bind(wx.EVT_BUTTON, self.OnLaunch)
self.sizer_params = wx.FlexGridSizer(cols = 1)
self.sizer_params.Add(self.sizer_model, flag = wx.ALIGN_CENTER | wx.BOTTOM | wx.UP, border = 3)
self.sizer_params.Add(self.sizer_captcha, flag = wx.ALIGN_CENTER | wx.BOTTOM | wx.UP, border = 3)
self.sizer_params.Add(self.launchButton, flag = wx.ALIGN_CENTER | wx.UP, border = 40)
self.sizer = wx.FlexGridSizer(rows = 1)
self.sizer.Add(self.sizer_params, flag = wx.ALIGN_CENTER | wx.ALL, border = 5)
self.sizer.Add(self.main_sizer, flag = wx.ALIGN_CENTER | wx.ALL, border = 5)
self.SetSizer(self.sizer)
self.Fit()
self.captcha_selected = False
self.model_selected = False
###############################################################################
############################# CREATION EVENEMENTS #############################
###############################################################################
self.SomeNewSetPathLabelEvent, self.EVT_SET_PATH_EVENT = wx.lib.newevent.NewEvent()
self.Bind(self.EVT_SET_PATH_EVENT, self.OnSetPathLabel)
self.SomeNewSetCaptchaImageEvent, self.EVT_SET_CAPTCHA_IMAGE_EVENT = wx.lib.newevent.NewEvent()
self.Bind(self.EVT_SET_CAPTCHA_IMAGE_EVENT, self.OnsetCaptchaImage)
self.SomeNewSetResultsEvent, self.EVT_SET_RESULTS_EVENT = wx.lib.newevent.NewEvent()
self.Bind(self.EVT_SET_RESULTS_EVENT, self.OnSetResults)
self.SomeNewSetThumbsEvent, self.EVT_SET_THUMBS_EVENT = wx.lib.newevent.NewEvent()
self.Bind(self.EVT_SET_THUMBS_EVENT, self.OnSetThumbs)
###############################################################################
###############################################################################
###############################################################################
############################# COMPATIBILITE LINUX #############################
###############################################################################
def setThumbs(self, pil_image1, pil_image2, pil_image3):
#create the event
evt = self.SomeNewSetThumbsEvent(image1=self.PIL_to_WX(pil_image1).ConvertToBitmap(),
image2=self.PIL_to_WX(pil_image2).ConvertToBitmap(),
image3=self.PIL_to_WX(pil_image3).ConvertToBitmap())
#post the event
wx.PostEvent(self, evt)
def OnSetThumbs(self, evt):
self.image_chiffre1.SetBitmap(evt.image1)
self.image_chiffre2.SetBitmap(evt.image2)
self.image_chiffre3.SetBitmap(evt.image3)
###############################################################################
def setResults(self, resultat1, score1, resultat2, score2, resultat3, score3, dico1={}, dico2={}, dico3={}):
#create the event
evt = self.SomeNewSetResultsEvent(resultat1=resultat1, score1=score1, resultat2=resultat2,
score2=score2, resultat3=resultat3, score3=score3,
dico1=dico1, dico2=dico2, dico3=dico3)
#post the event
wx.PostEvent(self, evt)
def OnSetResults(self, evt):
self.score_chiffre1.SetLabel(evt.score1)
self.score_chiffre2.SetLabel(evt.score2)
self.score_chiffre3.SetLabel(evt.score3)
self.resultat_chiffre1.SetLabel(evt.resultat1)
self.resultat_chiffre2.SetLabel(evt.resultat2)
self.resultat_chiffre3.SetLabel(evt.resultat3)
items1 = evt.dico1.items()
items1 = map(lambda (a,b) : (b,a), items1)
items1.sort(reverse=True)
self.score_chiffre1.SetToolTipString("\n".join(map(lambda (b,a) : chr(65+int(a))+" : "+str(b), items1)))
items2 = evt.dico2.items()
items2 = map(lambda (a,b) : (b,a), items2)
items2.sort(reverse=True)
self.score_chiffre2.SetToolTipString("\n".join(map(lambda (b,a) : chr(65+int(a))+" : "+str(b), items2)))
items3 = evt.dico3.items()
items3 = map(lambda (a,b) : (b,a), items3)
items3.sort(reverse=True)
self.score_chiffre3.SetToolTipString("\n".join(map(lambda (b,a) : chr(65+int(a))+" : "+str(b), items3)))
self.resultat_chiffre1.SetToolTipString("zefzhefzhefz\t:\tzjfgzhegfz\nzefzhefzhefz\t:\tzjfgzhegfz")
###############################################################################
def setCaptchaImage(self, pil_image):
#create the event
evt = self.SomeNewSetCaptchaImageEvent(image=self.PIL_to_WX(pil_image).ConvertToBitmap())
#post the event
wx.PostEvent(self, evt)
def OnsetCaptchaImage(self, evt):
self.image_input_window.SetBitmap(evt.image)
###############################################################################
def SetPathLabel(self, filename):
#create the event
evt = self.SomeNewSetPathLabelEvent(name=filename)
#post the event
wx.PostEvent(self, evt)
def OnSetPathLabel(self, evt):
self.path_model.SetLabel(evt.name)
###############################################################################
###############################################################################
###############################################################################
########################### EVEMENEMENTS GRAPHIQUES ###########################
###############################################################################
def OnLaunch(self, evt):
if not self.captcha_selected:
wx.MessageBox("Selectionner le Captcha !", "Donnee manquante")
return
if not self.model_selected:
wx.MessageBox("Selectionner le modele SVM !", "Donnee manquante")
return
thread.start_new_thread(Break_Egoshare_Captcha.break_captcha,
(self.model, self.letter1_algo, self.letter2_algo, self.letter3_algo, self))
def OnSelectModel(self, evt):
dlg = wx.FileDialog(self, "Selectionnez le modele", os.path.join(os.getcwd(),MODEL_FOLDER), DEFAULT_MODEL_FILE,
wildcard = "Model files (*.svm)|*.svm",
style = wx.OPEN)
retour = dlg.ShowModal()
self.chemin = dlg.GetPath().encode("latin-1")
fichier = dlg.GetFilename().encode("latin-1")
dlg.Destroy()
if retour == wx.ID_OK and fichier != "":
self.model = Break_Egoshare_Captcha.load_model(self.chemin, self, fichier)
self.Update()
if self.captcha_selected and self.model_selected:
#Lancement du calcul
self.OnLaunch(None)
def OnSelectCaptcha(self, evt):
dlg = wx.FileDialog(self, "Selectionnez l'image", os.path.join(os.getcwd(), CAPTCHA_FOLDER), DEFAULT_CAPTCHA_FILE,
wildcard = "Image files (*.jpg;)|*.jpg",
style = wx.OPEN)
retour = dlg.ShowModal()
self.chemin = dlg.GetPath().encode("latin-1")
self.folder = dlg.GetDirectory().encode("latin-1")
fichier = dlg.GetFilename()
dlg.Destroy()
if retour == wx.ID_OK and fichier != "":
self.beau_captcha, self.letter1, self.letter2, self.letter3, self.letter1_algo, self.letter2_algo, self.letter3_algo = Break_Egoshare_Captcha.preprocess_captcha_part(self.chemin, self.folder, self)
self.setCaptchaImage(self.beau_captcha)
self.setThumbs(self.letter1, self.letter2, self.letter3)
self.captcha_selected = True
self.setResults("", "", "", "", "", "")
self.Update()
if self.captcha_selected and self.model_selected:
#Lancement du calcul
self.OnLaunch(None)
###############################################################################
###############################################################################
def PIL_to_WX(self, pil):
image = wx.EmptyImage(pil.size[0], pil.size[1])
data = pil.tostring()
image.SetData(data)
return image
class MyApp(wx.App):
def OnInit(self):
self.MyFrame = MyFrame(2)
self.MyFrame.Center(wx.BOTH)
self.MyFrame.Show()
return True
app = MyApp(False)
#TRACEBACK
import traceback
import sys
def Myexcepthook(type, value, tb):
lines=traceback.format_exception(type, value, tb)
## f=open('log.txt', 'a')
## f.write("\n".join(lines))
## f.close()
wx.MessageBox("\n".join(lines), "Traceback Error")
sys.excepthook=Myexcepthook
app.MainLoop()