Mercurial > ift6266
comparison demo/Test1.mxml @ 540:269c39f55134
Added demo source files
author | boulanni <nicolas_boulanger@hotmail.com> |
---|---|
date | Wed, 02 Jun 2010 01:34:49 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
539:84f42fe05594 | 540:269c39f55134 |
---|---|
1 <?xml version="1.0" encoding="utf-8"?> | |
2 | |
3 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()"> | |
4 <mx:Image x="432" y="394" width="300" height="300" id="img" autoLoad="true" scaleContent="true"/> | |
5 <mx:Image x="116" y="197" width="200" height="200" id="imdraw"/> | |
6 <mx:HSlider x="503" y="150" id="rot" minimum="-180" maximum="180" value="0" liveDragging="true" change="changeImage()"/> | |
7 <mx:HSlider x="503" y="120" minimum="-2" maximum="2" value="1" liveDragging="true" id="scalex" change="changeImage()"/> | |
8 <mx:HSlider x="687" y="120" minimum="-2" maximum="2" value="1" liveDragging="true" id="scaley" change="changeImage()"/> | |
9 <mx:HSlider x="503" y="90" minimum="-10" maximum="10" value="0" liveDragging="true" id="transx" change="changeImage()"/> | |
10 <mx:HSlider x="683" y="90" minimum="-10" maximum="10" value="0" liveDragging="true" id="transy" change="changeImage()"/> | |
11 <mx:HSlider x="503" y="178" id="skew" liveDragging="true" minimum="-1" maximum="1" change="changeImage()" value="0"/> | |
12 <mx:Label x="839" y="414" text="X" id="lab0" width="43" height="36" fontSize="12" fontWeight="bold" textAlign="left" color="#0B333C" fontFamily="Verdana"/> | |
13 <mx:Button x="128" y="405" label="Erase" click="can2.graphics.clear();changeDrawing();changeImage()"/> | |
14 <mx:Label x="371" y="90" text="Translation"/> | |
15 <mx:Label x="839" y="285" text="Restrict to:"/> | |
16 <mx:Label x="371" y="116" text="Scale"/> | |
17 <mx:Label x="371" y="148" text="Rotation"/> | |
18 <mx:Label x="371" y="175" text="Skew"/> | |
19 <mx:Label x="371" y="206" text="Salt & Pepper Noise" width="134" height="20"/> | |
20 <mx:HSlider x="503" y="208" id="spnoise" liveDragging="true" minimum="0" maximum="1" change="changeImage()" value="0"/> | |
21 <mx:Label x="371" y="232" text="Gaussian Noise" width="108" height="20"/> | |
22 <mx:HSlider x="503" y="234" id="gnoise" liveDragging="true" minimum="0" maximum="1" change="changeImage()" value="0"/> | |
23 <mx:Label x="118" y="175" text="Draw here:" fontSize="10" fontWeight="bold"/> | |
24 <mx:Label x="371" y="64" text="Transforms:" fontSize="10" fontWeight="bold"/> | |
25 <mx:Label x="433" y="368" text="Resulting image:" fontSize="10" fontWeight="bold"/> | |
26 <mx:Label x="835" y="236" text="Classifier:" fontSize="10" fontWeight="bold"/> | |
27 | |
28 <mx:Script> | |
29 <![CDATA[ | |
30 import mx.effects.Blur; | |
31 import flash.utils.*; | |
32 import flash.display.*; | |
33 import flash.geom.*; | |
34 private var can2:Sprite = new Sprite() | |
35 | |
36 import cmodule.mlp_conv.CLibInit; | |
37 | |
38 [Embed(source="mlp_w1", mimeType="application/octet-stream")] private static var mlp_w1:Class; | |
39 [Embed(source="mlp_b1", mimeType="application/octet-stream")] private static var mlp_b1:Class; | |
40 [Embed(source="mlp_w2", mimeType="application/octet-stream")] private static var mlp_w2:Class; | |
41 [Embed(source="mlp_b2", mimeType="application/octet-stream")] private static var mlp_b2:Class; | |
42 | |
43 [Embed(source="sda_w1", mimeType="application/octet-stream")] private static var sda_w1:Class; | |
44 [Embed(source="sda_b1", mimeType="application/octet-stream")] private static var sda_b1:Class; | |
45 [Embed(source="sda_w2", mimeType="application/octet-stream")] private static var sda_w2:Class; | |
46 [Embed(source="sda_b2", mimeType="application/octet-stream")] private static var sda_b2:Class; | |
47 [Embed(source="sda_w3", mimeType="application/octet-stream")] private static var sda_w3:Class; | |
48 [Embed(source="sda_b3", mimeType="application/octet-stream")] private static var sda_b3:Class; | |
49 [Embed(source="sda_w4", mimeType="application/octet-stream")] private static var sda_w4:Class; | |
50 [Embed(source="sda_b4", mimeType="application/octet-stream")] private static var sda_b4:Class; | |
51 | |
52 private var loader:CLibInit; | |
53 private var lib:Object; | |
54 | |
55 private var carac:String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | |
56 | |
57 private function init():void{ | |
58 var mlp_w1_data:ByteArray = new mlp_w1(); | |
59 var mlp_b1_data:ByteArray = new mlp_b1(); | |
60 var mlp_w2_data:ByteArray = new mlp_w2(); | |
61 var mlp_b2_data:ByteArray = new mlp_b2(); | |
62 | |
63 var sda_w1_data:ByteArray = new sda_w1(); | |
64 var sda_b1_data:ByteArray = new sda_b1(); | |
65 var sda_w2_data:ByteArray = new sda_w2(); | |
66 var sda_b2_data:ByteArray = new sda_b2(); | |
67 var sda_w3_data:ByteArray = new sda_w3(); | |
68 var sda_b3_data:ByteArray = new sda_b3(); | |
69 var sda_w4_data:ByteArray = new sda_w4(); | |
70 var sda_b4_data:ByteArray = new sda_b4(); | |
71 | |
72 mlp_w1_data.endian = Endian.LITTLE_ENDIAN; | |
73 mlp_b1_data.endian = Endian.LITTLE_ENDIAN; | |
74 mlp_w2_data.endian = Endian.LITTLE_ENDIAN; | |
75 mlp_b2_data.endian = Endian.LITTLE_ENDIAN; | |
76 | |
77 sda_w1_data.endian = Endian.LITTLE_ENDIAN; | |
78 sda_b1_data.endian = Endian.LITTLE_ENDIAN; | |
79 sda_w2_data.endian = Endian.LITTLE_ENDIAN; | |
80 sda_b2_data.endian = Endian.LITTLE_ENDIAN; | |
81 sda_w3_data.endian = Endian.LITTLE_ENDIAN; | |
82 sda_b3_data.endian = Endian.LITTLE_ENDIAN; | |
83 sda_w4_data.endian = Endian.LITTLE_ENDIAN; | |
84 sda_b4_data.endian = Endian.LITTLE_ENDIAN; | |
85 | |
86 mlp_w1_data.position = 0; | |
87 mlp_b1_data.position = 0; | |
88 mlp_w2_data.position = 0; | |
89 mlp_b2_data.position = 0; | |
90 | |
91 sda_w1_data.position = 0; | |
92 sda_b1_data.position = 0; | |
93 sda_w2_data.position = 0; | |
94 sda_b2_data.position = 0; | |
95 sda_w3_data.position = 0; | |
96 sda_b3_data.position = 0; | |
97 sda_w4_data.position = 0; | |
98 sda_b4_data.position = 0; | |
99 | |
100 loader = new CLibInit; | |
101 lib = loader.init(); | |
102 lib.initparam(mlp_w1_data, mlp_b1_data, mlp_w2_data, mlp_b2_data, sda_w1_data, sda_b1_data, sda_w2_data, sda_b2_data, sda_w3_data, sda_b3_data, sda_w4_data, sda_b4_data); | |
103 lib.choosemodel(0); | |
104 | |
105 changeDrawing(); | |
106 changeImage(); | |
107 | |
108 imdraw.addEventListener(MouseEvent.MOUSE_DOWN, begin_drawing); | |
109 //imdraw.addEventListener(MouseEvent.ROLL_OUT, stop_drawing); | |
110 imdraw.addEventListener(MouseEvent.MOUSE_UP, stop_drawing); | |
111 | |
112 calc_pred(); | |
113 } | |
114 | |
115 private function calc_done(out:Array):void { | |
116 var i:uint, s:Number = 0; | |
117 if (!ch_dig.selected) for(i=0; i<10; i++) out[i] = 0; | |
118 if (!ch_upper.selected) for(i=10; i<36; i++) out[i] = 0; | |
119 if (!ch_lower.selected) for(i=36; i<62; i++) out[i] = 0; | |
120 if(modelnum.selectedIndex == 0) { | |
121 for(i=0; i<62; i++) s += out[i]; | |
122 if(s) for(i=0; i<62; i++) out[i] /= s; | |
123 } | |
124 | |
125 var ind:Array = out.sort(Array.DESCENDING | Array.RETURNINDEXEDARRAY | Array.NUMERIC); | |
126 lab0.text = carac.charAt(ind[0]) | |
127 lab1.text = carac.charAt(ind[1]) | |
128 lab2.text = carac.charAt(ind[2]) | |
129 rec0.width = out[ind[0]]*130 | |
130 rec1.width = out[ind[1]]*130 | |
131 rec2.width = out[ind[2]]*130 | |
132 //rec0.setStyle("backgroundColor", 0xff0000); | |
133 | |
134 setTimeout(calc_pred, 10); | |
135 } | |
136 | |
137 private function calc_pred():void { | |
138 var bd:BitmapData = imnorm.source.bitmapData; | |
139 var rect:Rectangle = new Rectangle(0, 0, 32, 32); | |
140 var bytes:ByteArray = bd.getPixels(rect); | |
141 bytes.position = 0; | |
142 lib.choosemodel(modelnum.selectedIndex); | |
143 lib.prediction(calc_done, bytes); | |
144 } | |
145 | |
146 private function begin_drawing(event:MouseEvent):void { | |
147 can2.graphics.lineStyle(thickness.value * imdraw.width/32, 0xFFFFFFFF); | |
148 can2.graphics.moveTo(imdraw.mouseX, imdraw.mouseY) | |
149 imdraw.addEventListener(MouseEvent.MOUSE_MOVE, draw_line); | |
150 } | |
151 private function stop_drawing(e:MouseEvent):void | |
152 { | |
153 imdraw.removeEventListener(MouseEvent.MOUSE_MOVE, draw_line); | |
154 } | |
155 | |
156 private function draw_line(e:MouseEvent):void | |
157 { | |
158 var x:Number = imdraw.mouseX, y:Number = imdraw.mouseY | |
159 /*if (x<0 || x>imdraw.width || y<0 || y>imdraw.height) { | |
160 stop_drawing(e) | |
161 return | |
162 }*/ | |
163 can2.graphics.lineTo(x, y); | |
164 changeDrawing() | |
165 changeImage() | |
166 e.updateAfterEvent(); | |
167 } | |
168 | |
169 private function changeDrawing():void{ | |
170 var bitmapDataObject2:BitmapData = new BitmapData(32, 32, false, 0x00000000); | |
171 var rect:Rectangle = new Rectangle(0, 0, 32, 32); | |
172 var m:Matrix = new Matrix() | |
173 m.scale(32/imdraw.width, 32/imdraw.height) | |
174 bitmapDataObject2.draw(can2,m,null,null,null,true); | |
175 var myBitmap:Bitmap = new Bitmap(bitmapDataObject2); | |
176 imdraw.source = myBitmap; | |
177 } | |
178 | |
179 private var ready:Boolean = false; | |
180 private var cache:Number; | |
181 public function RandNormal(sigma:Number):Number { | |
182 ready = !ready; | |
183 if (!ready) return cache * sigma; | |
184 var x:Number = Math.sqrt(-2 * Math.log(Math.random())), y:Number = Math.random() | |
185 cache = x * Math.cos(2*Math.PI*y); | |
186 return x * Math.sin(2*Math.PI*y) * sigma; | |
187 } | |
188 | |
189 private function changeImage():void{ | |
190 | |
191 var bitmapDataObject2:BitmapData = new BitmapData(32, 32, false, 0x00000000); | |
192 | |
193 var can3:Sprite = new Sprite() | |
194 var x:Number, y:Number, l:int, t:Number, w:int, c:uint, n:uint | |
195 n = int(0.999 + 3*rat.value) | |
196 for(var j:uint=0; j<n; j++) { | |
197 x = -10+ Math.random()*52 | |
198 y = -10 + Math.random()*52 | |
199 l = 5 + 30*rat.value*Math.random() | |
200 t = 2*Math.PI * Math.random() | |
201 w = 1 + 3*rat.value*Math.random() | |
202 c = 50+ 250*rat.value*Math.random() | |
203 can3.graphics.lineStyle(w, c | c<<8 | c<<16 | c<<24); | |
204 can3.graphics.moveTo(x, y) | |
205 can3.graphics.lineTo(x + l*Math.cos(t), y + l*Math.sin(t)); | |
206 } | |
207 bitmapDataObject2.draw(can3,null,null,null,null,true); | |
208 | |
209 var m:Matrix = new Matrix() | |
210 m.scale(32/imdraw.width, 32/imdraw.height) | |
211 m.translate(-16,-16) | |
212 m.concat(new Matrix(1,0,-skew.value,1)) | |
213 m.scale(scalex.value, scaley.value) | |
214 m.rotate(Math.PI* rot.value/180) | |
215 m.translate(transx.value, transy.value) | |
216 m.translate(16,16) | |
217 | |
218 bitmapDataObject2.draw(can2,m,null,null,null,true); | |
219 | |
220 var rect:Rectangle = new Rectangle(0, 0, 32, 32); | |
221 var bytes:ByteArray = bitmapDataObject2.getPixels(rect); | |
222 | |
223 bytes.position = 0; | |
224 for(var i:uint=0; i<4*32*32; i+=4) { | |
225 //bytes[i]=0 | |
226 var g:int = bytes[i+1] | |
227 | |
228 if (Math.random() < spnoise.value/5) | |
229 g = Math.random() * 255 | |
230 | |
231 g += RandNormal(gnoise.value*55) | |
232 g = Math.min(Math.max(g,0),255) | |
233 | |
234 g = 127.5 + (g-127.5)*contr.value | |
235 bytes[i+1]=bytes[i+2]=bytes[i+3] = g | |
236 } | |
237 | |
238 bitmapDataObject2.setPixels(rect, bytes); | |
239 | |
240 var myBitmap:Bitmap = new Bitmap(bitmapDataObject2); | |
241 img.source = myBitmap; | |
242 | |
243 NormImage(); | |
244 } | |
245 | |
246 private function NormImage():void{ | |
247 var rect:Rectangle = new Rectangle(0, 0, 32, 32); | |
248 | |
249 /*var f:BlurFilter = new BlurFilter(5.0, 5.0, 1); | |
250 var b2:BitmapData = new BitmapData(32, 32, false, 0); | |
251 b2.applyFilter(img.source.bitmapData, rect, new Point(0,0), f);*/ | |
252 //var b2:BitmapData = img.source.bitmapData.clone(); | |
253 | |
254 var bytes:ByteArray = img.source.bitmapData.getPixels(rect); | |
255 bytes.position = 0; | |
256 | |
257 var pix:Array = new Array(32*32); | |
258 var i:int, j:int; | |
259 for(i=0; i<32*32; i++) | |
260 pix[i] = bytes[4*i+1]; | |
261 | |
262 var ind:Array = pix.sort(Array.RETURNINDEXEDARRAY | Array.NUMERIC); | |
263 var med:Number = pix[ind[32*32/2]]; | |
264 | |
265 var ci:Number = 0, cj:Number = 0, s:Number = 0, mtot:Number=0; | |
266 for(i=0; i<32; i++) | |
267 for(j=0; j<32; j++) { | |
268 var g:int = pix[j*32+i]; | |
269 g = Math.abs(g-med); | |
270 mtot += g; | |
271 ci += g*i; | |
272 cj += g*j; | |
273 } | |
274 ci /= mtot; | |
275 cj /= mtot; | |
276 | |
277 for(i=0; i<32; i++) | |
278 for(j=0; j<32; j++) { | |
279 g = pix[j*32+i]; | |
280 g = Math.abs(g-med); | |
281 s += g* ((i-ci)*(i-ci) + (j-cj)*(j-cj)); | |
282 } | |
283 s = Math.sqrt(s / mtot); | |
284 s = Math.max(s, 3) | |
285 s = 9/s; | |
286 | |
287 var bitmapDataObject2:BitmapData = new BitmapData(32, 32, false, med | med<<8 | med<<16); | |
288 | |
289 var m:Matrix = new Matrix() | |
290 m.translate(-ci, -cj) | |
291 m.scale(s, s) | |
292 m.translate(16, 16) | |
293 | |
294 bitmapDataObject2.draw(img.source.bitmapData,m,null,null,null,true); | |
295 | |
296 var myBitmap:Bitmap = new Bitmap(bitmapDataObject2); | |
297 imnorm.source = myBitmap; | |
298 } | |
299 ]]> | |
300 </mx:Script> | |
301 <mx:CheckBox x="840" y="309" label="Digits" selected="true" id="ch_dig"/> | |
302 <mx:CheckBox x="840" y="338" label="Uppercase letters" selected="true" id="ch_upper"/> | |
303 <mx:CheckBox x="840" y="368" label="Lowercase letters" selected="true" id="ch_lower"/> | |
304 <mx:Canvas x="863" y="419" width="130" height="8" borderStyle="solid" borderColor="#B1C6CA" backgroundColor="#567B83" id="rec0"> | |
305 </mx:Canvas> | |
306 <mx:Label x="839" y="442" text="Y" id="lab1" width="43" height="36" fontSize="12" fontWeight="bold" textAlign="left" color="#0B333C" fontFamily="Verdana"/> | |
307 <mx:Canvas x="863" y="448" width="130" height="8" borderStyle="solid" borderColor="#B1C6CA" backgroundColor="#567B83" id="rec1"> | |
308 </mx:Canvas> | |
309 <mx:Label x="839" y="471" text="Z" id="lab2" width="43" height="36" fontSize="12" fontWeight="bold" textAlign="left" color="#0B333C" fontFamily="Verdana"/> | |
310 <mx:Canvas x="863" y="477" width="130" height="8" borderStyle="solid" borderColor="#B1C6CA" backgroundColor="#567B83" id="rec2"> | |
311 </mx:Canvas> | |
312 <mx:ComboBox x="912" y="234" editable="false" id="modelnum" selectedIndex="1"><mx:ArrayCollection> | |
313 <mx:String>Shallow MLP 500 h.u. /NIST</mx:String> | |
314 <mx:String>Deep SDA 3x1000 h.u. /P07</mx:String> | |
315 </mx:ArrayCollection></mx:ComboBox> | |
316 <mx:Button x="475" y="60" label="Reset" click="transx.value=transy.value=0;scalex.value=scaley.value=1; rot.value=skew.value=spnoise.value=gnoise.value=rat.value=0; contr.value=1; changeImage();"/> | |
317 <mx:Label x="371" y="260" text="Polarity / Contrast" width="108" height="20"/> | |
318 <mx:HSlider x="503" y="262" id="contr" liveDragging="true" minimum="-1" maximum="1" change="changeImage()" value="1"/> | |
319 <mx:Label x="371" y="288" text="Crossings Amount" width="108" height="20"/> | |
320 <mx:Label x="115" y="444" text="Thickness" width="108" height="20"/> | |
321 <mx:HSlider x="503" y="290" id="rat" liveDragging="true" minimum="0" maximum="1" change="changeImage()" value="0"/> | |
322 <mx:Image x="992" y="285" width="100" height="100" id="imnorm" autoLoad="true" scaleContent="true"/> | |
323 <mx:HSlider x="194" y="443" id="thickness" liveDragging="true" minimum="0.1" maximum="10" value="1.9" width="121"/> | |
324 <mx:Button x="671" y="221" label="Shuffle" click="changeImage()" width="71"/> | |
325 | |
326 </mx:Application> | |
327 | |
328 |