package com.kuldiegor.recognize; /** * Created by aeterneus on 17.03.2017. */ public class ConvolutionCore { public int unitMin; // -1 public int unitMax; // 1 public int matrix[][]; public ConvolutionCore(int width,int height,int haar){ matrix = new int[height][width]; unitMax=0; unitMin=0; switch (haar){ case 0:{ // -1 = // 1 = /* -1 -1 -1 1 1 1 -1 -1 -1 1 1 1 -1 -1 -1 1 1 1 */ for (int y=0;y<height;y++){ for (int x=0;x<(width/2);x++){ matrix[y][x]=-1; unitMin++; } for (int x=width/2;x<width;x++){ matrix[y][x]=1; unitMax++; } } break; } case 1:{ // -1 = // 1 = /* 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 */ for (int y=0;y<(height/2);y++){ for (int x=0;x<width;x++){ matrix[y][x]=1; unitMax++; } } for (int y=(height/2);y<height;y++){ for (int x=0;x<width;x++){ matrix[y][x]=-1; unitMin++; } } break; } case 2:{ // -1 = // 1 = /* 1 1 -1 -1 -1 1 1 1 1 -1 -1 -1 1 1 1 1 -1 -1 -1 1 1 */ for (int y=0;y<height;y++){ for (int x=0;x<(width/3);x++){ matrix[y][x]=1; unitMax++; } for (int x=(width/3);x<(width*2/3);x++){ matrix[y][x]=-1; unitMin++; } for (int x=(width*2/3);x<width;x++){ matrix[y][x]=1; unitMax++; } } break; } case 3:{ // -1 = // 1 = /* 1 1 1 1 1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 1 1 1 1 1 1 1 */ for (int y=0;y<(height/3);y++){ for (int x=0;x<width;x++){ matrix[y][x]=1; unitMax++; } } for (int y=(height/3);y<(height*2/3);y++){ for (int x=0;x<width;x++){ matrix[y][x]=-1; unitMin++; } } for (int y=(height*2/3);y<height;y++){ for (int x=0;x<width;x++){ matrix[y][x]=1; unitMax++; } } break; } case 4:{ // -1 = // 1 = /* 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -1 -1 -1 1 1 -1 -1 -1 1 1 -1 -1 -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 */ for (int y=0;y<(height/3);y++){ for (int x=0;x<width;x++){ matrix[y][x]=1; unitMax++; } } for (int y=(height/3);y<(height*2/3);y++){ for (int x=0;x<(width/3);x++){ matrix[y][x]=1; unitMax++; } for (int x=(width/3);x<(width*2/3);x++){ matrix[y][x]=-1; unitMin++; } for (int x=(width*2/3);x<width;x++){ matrix[y][x]=1; unitMax++; } } for (int y=(height*2/3);y<height;y++){ for (int x=0;x<width;x++){ matrix[y][x]=1; unitMax++; } } break; } case 5:{ // -1 = // 1 = /* 1 1 1 -1 -1 -1 1 1 1 -1 -1 -1 -1 -1 -1 1 1 1 -1 -1 -1 1 1 1 */ for (int y=0;y<(height/2);y++){ for (int x=0;x<(width/2);x++){ matrix[y][x]=1; unitMax++; } for (int x=width/2;x<width;x++){ matrix[y][x]=-1; unitMin++; } } for (int y=(height/2);y<height;y++){ for (int x=0;x<(width/2);x++){ matrix[y][x]=-1; unitMin++; } for (int x=width/2;x<width;x++){ matrix[y][x]=1; unitMax++; } } break; } } } }
package com.kuldiegor.recognize; import java.awt.image.BufferedImage; import java.util.ArrayList; /** * Created by aeterneus on 17.03.2017. */ public class Convolution { static ConvolutionCore convolutionCores[]; // static { // convolutionCores = new ConvolutionCore[6]; for (int i=0;i<6;i++){ convolutionCores[i] = new ConvolutionCore(10,10,i); } } private int matrixx[][]; // 0 .. 255 public Convolution(BufferedImage image){ matrixx = getReadyMatrix(image); } private int[][] getReadyMatrix(BufferedImage bufferedImage){ // int width = bufferedImage.getWidth(); int heigth = bufferedImage.getHeight(); int[] lineData = new int[width * heigth]; bufferedImage.getRaster().getPixels(0, 0, width, heigth, lineData); int[][] res = new int[heigth][width]; int shift = 0; for (int row = 0; row < heigth; ++row) { System.arraycopy(lineData, shift, res[row], 0, width); shift += width; } return res; } private double[] ColapseMatrix(int[][] matrix,ConvolutionCore convolutionCore){ // int cmh=convolutionCore.matrix.length; // int cmw=convolutionCore.matrix[0].length; // int mh=matrix.length; // int mw=matrix[0].length; // int addWidth = cmw - (mw%cmw); // , int addHeight = cmh - (mh%cmh); int nmatrix[][]=new int[mh+addHeight][mw+addWidth]; for (int row = 0; row < mh; row++) { System.arraycopy(matrix[row], 0, nmatrix[row], 0, mw); } int nw = nmatrix[0].length/cmw; int nh = nmatrix.length/cmh; double result[] = new double[nh*nw]; int dmin = -convolutionCore.unitMin*255; // int dm = convolutionCore.unitMax*255-dmin; int q=0; for (int ny=0;ny<nh;ny++){ for (int nx=0;nx<nw;nx++){ int sum=0; for (int y=0;y<cmh;y++){ for (int x=0;x<cmw;x++){ sum += nmatrix[ny*cmh+y][nx*cmw+x]*convolutionCore.matrix[y][x]; } } result[q++]=((double)sum-dmin)/dm; } } return result; } public ArrayList<double[]> getConvolutionMatrix(){ // ArrayList<double[]> result = new ArrayList<>(); for (int i=0;i<convolutionCores.length;i++){ result.add(ColapseMatrix(matrixx,convolutionCores[i])); } return result; } }
package com.kuldiegor.recognize; import java.util.ArrayList; /** * Created by aeterneus on 17.03.2017. */ public class Hero { public String name; public ArrayList<double[]> convolutions; public Hero(String Name){ name = Name; } }
package com.kuldiegor.recognize; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; /** * Created by aeterneus on 17.03.2017. */ public class DefaultHero { public ArrayList<Hero> heroes; public String path; public DefaultHero(String path,int tload){ heroes = new ArrayList<>(); this.path = path; switch (tload){ case 0:{ LoadFromFolder(path); break; } case 1:{ LoadFromFile(path); } } } private void LoadFromFolder(String path){ // File folder = new File(path); File[] folderEntries = folder.listFiles(); for (File entry : folderEntries) { if (!entry.isDirectory()) { BufferedImage image = null; try { image = ImageIO.read(entry); } catch (IOException e) { e.printStackTrace(); } Hero hero = new Hero(StringTool.parse(entry.getName(),"",".png")); hero.convolutions = new Convolution(image).getConvolutionMatrix(); heroes.add(hero); } } } private void LoadFromFile(String name){ // try { BufferedReader bufferedReader = new BufferedReader(new FileReader(name)); String str; while ((str = bufferedReader.readLine())!= null){ Hero hero =new Hero(StringTool.parse(str,"",":")); String s = StringTool.parse(str,":",""); String mas[] = s.split(";"); int n=mas.length/48; hero.convolutions = new ArrayList<>(n); for (int c=0;c<n;c++) { double dmas[] = new double[48]; for (int i = 0; i < 48; i++) { dmas[i] = Double.parseDouble(mas[i+c*48]); } hero.convolutions.add(dmas); } heroes.add(hero); } } catch (IOException e){ e.printStackTrace(); } } public void SaveToFile(String name){ // Collections.sort(heroes, Comparator.comparing(o -> o.name)); FileWriter fileWriter = null; try { fileWriter = new FileWriter(name); } catch (IOException e){ e.printStackTrace(); } for (int i=0;i<heroes.size();i++){ Hero hero = heroes.get(i); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(hero.name).append(":"); for (int i2=0;i2<hero.convolutions.size();i2++){ double matrix[] = hero.convolutions.get(i2); for (int i3=0;i3<matrix.length;i3++){ stringBuilder.append(matrix[i3]).append(";"); } } try { fileWriter.write(stringBuilder.append("\r\n").toString()); } catch (IOException e){ e.printStackTrace(); } } try { fileWriter.close(); } catch (IOException e){ e.printStackTrace(); } } public String getSearhHeroName(Hero hero){ // for (int i=0;i<heroes.size();i++){ if (equalsHero(hero,heroes.get(i))){ return heroes.get(i).name; } } return "0"; } public boolean equalsHero(Hero hero1,Hero hero2){ // 2 int min=0; int max=0; for (int i=0;i<hero1.convolutions.size();i++){ double average=0; for (int i1=0;i1<hero1.convolutions.get(i).length;i1++){ // 2 average += Math.abs(hero1.convolutions.get(i)[i1]-hero2.convolutions.get(i)[i1]); } average /=hero1.convolutions.get(0).length; if (average<0.02){ // min++; } else { max++; } } return (min>=max); } }
try { // image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); } catch (AWTException e) { e.printStackTrace(); }
package com.kuldiegor.recognize; import java.awt.image.BufferedImage; import java.util.ArrayList; /** * Created by aeterneus on 17.03.2017. */ public class HRecognize { private DefaultHero defaultHero; public String heroes[]; // public HRecognize(BufferedImage screen, DefaultHero defaultHero){ heroes = new String[10]; ArrayList<Hero> heroArrayList = new ArrayList<>(); this.defaultHero = defaultHero; for (int i=0;i<5;i++){ // BufferedImage bufferedImage = new BufferedImage(78,53,BufferedImage.TYPE_BYTE_GRAY); // bufferedImage.getGraphics().drawImage(screen.getSubimage(43+i*96,6,78,53),0,0,null); Hero hero = new Hero(""); // hero.convolutions = new Convolution(bufferedImage).getConvolutionMatrix(); heroArrayList.add(hero); } for (int i=0;i<5;i++) { // BufferedImage bufferedImage = new BufferedImage(78, 53, BufferedImage.TYPE_BYTE_GRAY); // bufferedImage.getGraphics().drawImage(screen.getSubimage(777 + i * 96, 6, 78, 53), 0, 0, null); Hero hero = new Hero(""); // hero.convolutions = new Convolution(bufferedImage).getConvolutionMatrix(); heroArrayList.add(hero); } for (int i=0;i<10;i++){ // heroes[i] = defaultHero.getSearhHeroName(heroArrayList.get(i)); } } }
try { DatagramSocket socket = new DatagramSocket(6001); byte buffer[] = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, 1024); InetAddress localIP= InetAddress.getLocalHost(); while (!Thread.currentThread().isInterrupted()) { // socket.receive(packet); String s=new String(packet.getData(),0,packet.getLength()); if (StringTool.parse(s,"",":").equals("BroadCastFastDefinition")){ String str = "OK:"+localIP.getHostAddress(); byte buf[] = str.getBytes(); DatagramPacket p = new DatagramPacket(buf,buf.length,packet.getAddress(),6001); // socket.send(p); } } }catch (Exception e) { e.printStackTrace(); }
try { // Socket client = socket.accept(); final String ipclient = client.getInetAddress().getHostAddress(); Platform.runLater(new Runnable() { @Override public void run() { label1.setText(""); label1.setTextFill(Color.GREEN); textfield2.setText(ipclient); } }); DataOutputStream streamWriter = new DataOutputStream(client.getOutputStream()); BufferedImage image = null; while (client.isConnected()){ try { // image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); } catch (AWTException e) { e.printStackTrace(); } // HRecognize hRecognize = new HRecognize(image,defaultHero); StringBuilder stringBuilder = new StringBuilder(); for (int i=0;i<5;i++){ stringBuilder.append(hRecognize.heroes[i]).append(";"); } stringBuilder.append(":"); for (int i=5;i<10;i++){ stringBuilder.append(hRecognize.heroes[i]).append(";"); } stringBuilder.append("\n"); String str = stringBuilder.toString(); // streamWriter.writeUTF(str); try { Thread.sleep(300); } catch (InterruptedException e){ threadSocket.interrupt(); } } }catch (IOException e){ }
Source: https://habr.com/ru/post/325526/
All Articles