Introduce beginnings of custom number reader / learner

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2, Sig, Sigo 2 years ago
parent d3c671a37a
commit 4037757857
  1. 9
      readers/Box.java
  2. 6
      readers/ColorRange.java
  3. 78
      readers/fonts/Font.java
  4. 7
      readers/fonts/Glyph.java
  5. 0
      readers/fonts/testFont
  6. 63
      sigPlace.java

@ -1,10 +1,13 @@
package readers; package readers;
class Box{ public class Box{
int x,y,w,h; public int x;
public int y;
public int w;
public int h;
boolean ja_required; boolean ja_required;
final static int BOX_THRESHOLD=8; //How many pixels outside the specified region the score can be. final static int BOX_THRESHOLD=8; //How many pixels outside the specified region the score can be.
Box(int x,int y,int w, int h) { public Box(int x,int y,int w, int h) {
this.x=x; this.x=x;
this.y=y; this.y=y;
this.w=w; this.w=w;

@ -1,11 +1,11 @@
package readers; package readers;
import java.awt.Color; import java.awt.Color;
class ColorRange{ public class ColorRange{
int r_h,r_l; int r_h,r_l;
int g_h,g_l; int g_h,g_l;
int b_h,b_l; int b_h,b_l;
ColorRange(int r_l,int r_h,int g_l,int g_h,int b_l,int b_h) { public ColorRange(int r_l,int r_h,int g_l,int g_h,int b_l,int b_h) {
this.r_l=r_l; this.r_l=r_l;
this.r_h=r_h; this.r_h=r_h;
this.g_l=g_l; this.g_l=g_l;
@ -13,7 +13,7 @@ class ColorRange{
this.b_l=b_l; this.b_l=b_l;
this.b_h=b_h; this.b_h=b_h;
} }
boolean colorInRange(Color col) { public boolean colorInRange(Color col) {
return col.getRed()>=r_l&&col.getRed()<=r_h&&col.getGreen()>=g_l&&col.getGreen()<=g_h&&col.getBlue()>=b_l&&col.getBlue()<=b_h; return col.getRed()>=r_l&&col.getRed()<=r_h&&col.getGreen()>=g_l&&col.getGreen()<=g_h&&col.getBlue()>=b_l&&col.getBlue()<=b_h;
} }
} }

@ -0,0 +1,78 @@
package readers.fonts;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.awt.Color;
public class Font {
Glyph[] data = new Glyph[9];
final static int TRANSPARENT = new Color(0,0,0,0).getRGB();
public static Font LoadFont(String fontName) {
Path f = Paths.get("readers","fonts",fontName);
if (Files.exists(f)) {
//TODO Read the file.
return new Font();
}
return new Font();
}
public static void TrainFont(String fontName,String expectedGlyphs,BufferedImage data) {
Path f = Paths.get("readers","fonts",fontName);
Font font = LoadFont(fontName);
try {
Files.createFile(f);
int startX=-1;
int endX=-1;
outer:
for (int x=0;x<data.getWidth();x++) {
for (int y=0;y<data.getHeight();y++) {
if (data.getRGB(x, y)!=TRANSPARENT) {
if (startX==-1) {
startX=x;
}
continue outer;
}
}
//If we got to here it means we found transparent on everything.
if (startX!=-1) {
endX=x;
//Create a Glyph out of this.
Glyph g = new Glyph();
g.width=endX-startX;
g.height=data.getHeight();
int[] arr = data.getRGB(startX,0,g.width,g.height,null,0,g.width);
//Find the min and max Y.
int startY=g.height;
int endY=0;
for (int X=0;X<g.width;X++) {
for (int Y=0;Y<g.height;Y++) {
if (arr[Y*g.width+X]!=TRANSPARENT&&Y<startY) {
startY=Y;
}
if (arr[Y*g.width+X]!=TRANSPARENT&&Y>endY) {
endY=Y;
}
}
}
g.height=endY-startY;
g.data = new boolean[g.width*g.height];
for (int X=0;X<g.width;X++) {
for (int Y=0;Y<g.height;Y++) {
g.data[Y*g.width+X]=arr[(Y+startY)*g.width+X]!=TRANSPARENT;
}
}
int index = expectedGlyphs.charAt(0)-'0';
font.data[index]=g;
System.out.println("Glyph for number "+index+" has been saved.");
expectedGlyphs=expectedGlyphs.substring(1);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,7 @@
package readers.fonts;
public class Glyph {
int width;
int height;
boolean[] data;
}

@ -14,6 +14,14 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Color;
import readers.Box;
import readers.fonts.Font;
import readers.ColorRange;
public class sigPlace { public class sigPlace {
final static String ROOTDIR = "sitefiles"; final static String ROOTDIR = "sitefiles";
@ -47,8 +55,61 @@ public class sigPlace {
new AbstractMap.SimpleEntry<>( new AbstractMap.SimpleEntry<>(
"%FOOTER", Paths.get(REFDIR,"FOOTER.html")) "%FOOTER", Paths.get(REFDIR,"FOOTER.html"))
)); ));
static void seek(int[]arr,int i,ColorRange SEEKCOLOR,Color FINALCOLOR,int width) {
seek(arr,i,SEEKCOLOR,FINALCOLOR,width,0);
}
static int seek(int[]arr,int i,ColorRange SEEKCOLOR,Color FINALCOLOR,int width,int farthestRight) {
arr[i]=FINALCOLOR.getRGB();
int X = i%width;
for (int x=-1;x<=1;x++) {
for (int y=-1;y<=1;y++) {
int newX=(i+x+y*width)%width;
int newY=(i+x+y*width)/width;
if (newX>=0&&newY>=0&&newX<=width&&newY<=arr.length/width) {
Color col = new Color(arr[i+x+y*width]);
if (!col.equals(Color.MAGENTA)&&SEEKCOLOR.colorInRange(col)) {
farthestRight=seek(arr,i+x+y*width,SEEKCOLOR,FINALCOLOR,width,farthestRight);
}
}
}
}
return X>farthestRight?X:farthestRight;
}
final static int TRANSPARENT = new Color(0,0,0,0).getRGB();
public static void main(String[] args) { public static void main(String[] args) {
ArcadeReader.retrieveData(Paths.get("sdvx1.png")); Path f = Paths.get("sdvx1.png");
BufferedImage img;
try {
img = ImageIO.read(f.toFile());
Box scoreBox = new Box(458,1075,240,57);
int[] arr = img.getRGB(scoreBox.x, scoreBox.y, scoreBox.w, scoreBox.h, null, 0, scoreBox.w);
final ColorRange TARGETCOLOR = new ColorRange(240,255,240,255,240,255);
final ColorRange SEEKINGCOLOR = new ColorRange(100,255,100,255,100,255);
final Color FINALCOLOR = Color.MAGENTA;
for (int i=0;i<arr.length;i++) {
Color col = new Color(arr[i],true);
if (TARGETCOLOR.colorInRange(col)) {
seek(arr,i,SEEKINGCOLOR,FINALCOLOR,scoreBox.w);
}
}
for (int i=0;i<arr.length;i++) {
Color col = new Color(arr[i],true);
if (!col.equals(Color.MAGENTA)) {
arr[i]=TRANSPARENT;
}
}
img.setRGB(0,0,scoreBox.w,scoreBox.h,arr,0,scoreBox.w);
Font.TrainFont("testFont","930",img);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//ArcadeReader.retrieveData(Paths.get("sdvx1.png"));
/* Path secretFile = Paths.get(".clientsecret"); /* Path secretFile = Paths.get(".clientsecret");
List<String> data; List<String> data;
try { try {

Loading…
Cancel
Save