Uses 1059K of libraries. Click here for Pure Java version (12197L/65K).
1 | !7 |
2 | |
3 | replace Submission with BackgroundPlus<BoxWithColor>. |
4 | |
5 | // TODO: print log problem |
6 | cmodule2 BoxFinder > DynImageSurface { |
7 | int nBoxes = 1; |
8 | bool keepGoing; |
9 | new Best<Submission> best; |
10 | |
11 | transient RGBImage original; |
12 | transient Thread recognizerThread; |
13 | transient ImageSurface isBoxes; |
14 | transient Submission rendered; |
15 | |
16 | S switchableFields() { ret "nBoxes keepGoing"; } |
17 | |
18 | visualize { |
19 | JSpinner spinner = liveValueSpinner(dm_fieldLiveValue('nBoxes), 1, 50); |
20 | main.onChange(spinner, r startRecognition ); |
21 | ret jvsplit( |
22 | vgrid(super.visualize(), jscroll_centered(isBoxes = imageSurface())), |
23 | northAndCenter( |
24 | withSideMargin(rightAlignedLine( |
25 | dm_fieldCheckBox('keepGoing), |
26 | jbutton("Start", r startRecognition), |
27 | jbutton("Stop", r { cancelThread(recognizerThread) }), |
28 | withLabel("Boxes:", spinner))), |
29 | dm_printLogComponent())); |
30 | } |
31 | |
32 | start { |
33 | if (!hasImage()) setImage(whiteImage(50, 50)); // so we can paste... |
34 | onNewImage = r startRecognition; |
35 | doEvery(1.0, r renderBoxes); |
36 | } |
37 | |
38 | double scoreImage(RGBImage image) { |
39 | ret 100*(1.0-rgbDistance(image, original)); |
40 | } |
41 | |
42 | void startRecognition enter { |
43 | cancelThread(recognizerThread); |
44 | recognizerThread = startThread(r { |
45 | BufferedImage image = getImage(); |
46 | final int w = image.getWidth(), h = image.getHeight(); |
47 | print("Starting recognition on " + w + "*" + h + " image"); |
48 | best = new Best; |
49 | best.stringifier = f sfu; |
50 | original = RGBImage(image); |
51 | //new AIStrategy_RandomWithVariation<Submission> strategy; |
52 | new AIStrategy_Racer_RandomWithVary<Submission> strategy; |
53 | strategy.verbose = true; |
54 | strategy.best = best; |
55 | strategy.submit = func(Submission s) -> double { scoreImage(rgbRenderRenderable(w, h, s)) }; |
56 | strategy.random = func -> Submission { randomBackgroundPlusBoxes(w, h, nBoxes) }; |
57 | strategy.vary = func(Submission s) -> Submission { varyBackgroundPlusBoxes(s, w, h) }; |
58 | |
59 | if (keepGoing) |
60 | runStrategyForever(best, strategy); |
61 | else |
62 | runStrategyWhileImprovement(best, strategy); |
63 | print("Done"); |
64 | persistMe(); |
65 | }); |
66 | } |
67 | |
68 | void renderBoxes enter { |
69 | if (original != null && best! != rendered) { |
70 | rendered = best!; |
71 | isBoxes.setImage(renderRenderable(original.w(), original.h(), rendered)); |
72 | } |
73 | } |
74 | } |
download show line numbers debug dex old transpilations
Travelled to 9 computer(s): bhatertpkbcr, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1019834 |
Snippet name: | Box Finder v1 [Dyn Module] |
Eternal ID of this version: | #1019834/35 |
Text MD5: | 193e0d3e3a6268c943214c274aca107c |
Transpilation MD5: | 44ae641db66427507c1f1df291b2052e |
Author: | stefan |
Category: | javax / image recognition |
Type: | JavaX source code (Dynamic Module) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-01-04 00:29:08 |
Source code size: | 2449 bytes / 74 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 416 / 1077 |
Version history: | 34 change(s) |
Referenced in: | [show references] |