Create speckle rejection code and add to VisionProcess

Isn't yet added to the GUI
This commit is contained in:
Matt M
2019-10-23 21:39:47 +00:00
committed by ori agranat
parent 12c409b953
commit 910d377440
14 changed files with 37 additions and 124 deletions

View File

@@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.chameleon-vision.main</groupId> <groupId>org.chameleon-vision.main</groupId>
<artifactId>chameleon-vision</artifactId> <artifactId>chameleon-vision</artifactId>
<version>1.1.3-BETA</version> <version>1.1.4-BETA</version>
<build> <build>
<plugins> <plugins>
<!--setup for java jdk 12--> <!--setup for java jdk 12-->
@@ -14,7 +14,9 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version> <version>3.8.0</version>
<configuration> <configuration>
<release>12</release> <source>12</source>
<target>12</target>
<verbose>true</verbose>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>

View File

@@ -24,7 +24,6 @@ public class Main {
private static final int DEFAULT_PORT = 8888; private static final int DEFAULT_PORT = 8888;
private static int webserverPort = DEFAULT_PORT;
private static boolean ntServerMode = false; private static boolean ntServerMode = false;
private static boolean manageNetwork = true; private static boolean manageNetwork = true;
private static boolean ignoreRoot = false; private static boolean ignoreRoot = false;
@@ -43,7 +42,7 @@ public class Main {
} }
} }
public static final Platform CurrentPlatform = Platform.getCurrentPlatform(); private static final Platform CurrentPlatform = Platform.getCurrentPlatform();
private static void handleArgs(String[] args) { private static void handleArgs(String[] args) {
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
@@ -127,12 +126,10 @@ public class Main {
// Attempt to load the JNI Libraries // Attempt to load the JNI Libraries
try { try {
if (CurrentPlatform.equals(Platform.LINUX_ARM64))
CameraServerJNI.forceLoad(); CameraServerJNI.forceLoad();
CameraServerCvJNI.forceLoad(); CameraServerCvJNI.forceLoad();
} catch (IOException e) { } catch (IOException e) {
var errorStr = CurrentPlatform.equals(Platform.UNSUPPORTED) ? "Unsupported platform!" : "Failed to load JNI Libraries!"; throw new RuntimeException("Failed to load JNI Libraries!");
throw new RuntimeException(errorStr);
} }
if (CameraManager.initializeCameras()) { if (CameraManager.initializeCameras()) {
@@ -152,6 +149,7 @@ public class Main {
} }
} }
int webserverPort = DEFAULT_PORT;
System.out.printf("Starting Webserver at port %d\n", webserverPort); System.out.printf("Starting Webserver at port %d\n", webserverPort);
Server.main(webserverPort); Server.main(webserverPort);
} else { } else {

View File

@@ -1,22 +1,13 @@
package com.chameleonvision.network; package com.chameleonvision.network;
import com.chameleonvision.settings.NetworkSettings;
import com.chameleonvision.settings.SettingsManager;
import com.chameleonvision.util.ShellExec;
import java.io.BufferedOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException; import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
public class LinuxNetworking extends SysNetworking { public class LinuxNetworking extends SysNetworking {
private ShellExec shell = new ShellExec(true, true);
@Override @Override
public boolean setDHCP() { public boolean setDHCP() {
String[] clearArgs = { "addr", "flush", "dev", networkInterface.name }; String[] clearArgs = { "addr", "flush", "dev", networkInterface.name };

View File

@@ -1,10 +1,8 @@
package com.chameleonvision.network; package com.chameleonvision.network;
import com.chameleonvision.settings.GeneralSettings;
import java.net.InetAddress;
import java.net.InterfaceAddress; import java.net.InterfaceAddress;
@SuppressWarnings("WeakerAccess")
public class NetworkInterface { public class NetworkInterface {
public final String name; public final String name;
public final String displayName; public final String displayName;
@@ -43,9 +41,8 @@ public class NetworkInterface {
} }
// Transform the resulting value in xxx.xxx.xxx.xxx format, like if // Transform the resulting value in xxx.xxx.xxx.xxx format, like if
/// it was a standard address... /// it was a standard address...
String maskString = ((shiftby >> 24) & 255) + "." + ((shiftby >> 16) & 255) + "." + ((shiftby >> 8) & 255) + "." + (shiftby & 255); // Return the address thus created...
// Return the address thus created... return ((shiftby >> 24) & 255) + "." + ((shiftby >> 16) & 255) + "." + ((shiftby >> 8) & 255) + "." + (shiftby & 255);
return maskString;
// return InetAddress.getByName(maskString); // return InetAddress.getByName(maskString);
} }
catch(Exception e) { catch(Exception e) {

View File

@@ -1,7 +1,6 @@
package com.chameleonvision.network; package com.chameleonvision.network;
import com.chameleonvision.settings.NetworkSettings;
import com.chameleonvision.settings.Platform; import com.chameleonvision.settings.Platform;
import com.chameleonvision.settings.SettingsManager; import com.chameleonvision.settings.SettingsManager;
@@ -12,8 +11,7 @@ import java.util.List;
public class NetworkManager { public class NetworkManager {
private NetworkManager() {} private NetworkManager() {}
protected static SysNetworking networking; private static SysNetworking networking;
protected static NetworkInterface botInterface = null;
private static boolean isManaged = false; private static boolean isManaged = false;
public static void initialize(boolean manage) { public static void initialize(boolean manage) {
@@ -64,7 +62,7 @@ public class NetworkManager {
return; return;
} }
botInterface = goodInterfaces.get(0); NetworkInterface botInterface = goodInterfaces.get(0);
networking.setNetworkInterface(botInterface); networking.setNetworkInterface(botInterface);
} else { } else {
isManaged = false; isManaged = false;
@@ -78,6 +76,10 @@ public class NetworkManager {
} }
} }
private static byte[] GetTeamNumberIPBytes(int teamNumber) {
return new byte[]{(byte) (teamNumber / 100), (byte) (teamNumber % 100)};
}
private static boolean loadFromGeneralSettings() { private static boolean loadFromGeneralSettings() {
if (!isManaged) { if (!isManaged) {
return true; return true;

View File

@@ -5,7 +5,6 @@ import com.chameleonvision.util.ShellExec;
import java.io.IOException; import java.io.IOException;
import java.net.SocketException; import java.net.SocketException;
import java.util.List; import java.util.List;
import java.util.Scanner;
public abstract class SysNetworking { public abstract class SysNetworking {

View File

@@ -1,8 +1,5 @@
package com.chameleonvision.network; package com.chameleonvision.network;
import com.chameleonvision.settings.NetworkSettings;
import com.chameleonvision.settings.SettingsManager;
import java.net.SocketException; import java.net.SocketException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;

View File

@@ -1,80 +0,0 @@
package com.chameleonvision.settings;
import java.net.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import com.chameleonvision.util.Utilities;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
public class NetworkSettings {
public String connectionType, ip, netmask, gateway, hostname;
public void run() {
// String adapter = getAdapter();
if (SystemUtils.IS_OS_LINUX) {//TODO check linux commands
String adapter = getAdapter();
if (!adapter.equals("")) {
executeCommand("ifconfig " + adapter + " down");
if (connectionType.equals("DHCP"))
executeCommand("dhclient -r " + adapter);
else if (connectionType.equals("Static")) {
executeCommand("ifconfig " + adapter + " " + this.ip + " netmask " + this.netmask);
executeCommand("route add default gw " + this.gateway + " " + adapter);
}
executeCommand("ifconfig " + adapter + " up");
}
executeCommand("hostnamectl set-hostname " + this.hostname);
}
// //TODO check windows commands
// else if (SystemUtils.IS_OS_WINDOWS) {
// if (!adapter.equals("")) {
// if (connectionType.equals("DHCP")){
// executeCommand("cmd /c interface ip set address \"" + adapter + "\" dhcp");
// }
// else if (connectionType.equals("Static")) {
// executeCommand("cmd /c netsh interface ip set address \"" + adapter + "\" static " + this.ip + " " + this.netmask + " " + this.gateway + "1");
// }
// }
// //TODO find a way to change hostname in windows
// }
}
private void executeCommand(String command) {
try {
Process p = Runtime.getRuntime().exec(command);
System.out.println("Executing "+ command);
p.waitFor();
p.destroy();
} catch (Exception e) {
System.err.println("Error while executing command!");
e.printStackTrace();
}
}
public static byte[] GetTeamNumberIPBytes(int teamNumber) {
return new byte[]{(byte) (teamNumber / 100), (byte) (teamNumber % 100)};
}
public static String getAdapter() {
try {//TODO fix windows get adapter
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets)) {
Enumeration<InetAddress> ee = netint.getInetAddresses();
for (InetAddress addr : Collections.list(ee))
if (addr instanceof Inet4Address) {
var addrString = addr.toString();
if ((addr.getAddress()[0] & 0xFF) == 10 && (addr.getAddress()[1] & 0xFF) == 168) {
System.out.println("found robot network interface at " + netint.getName() + " ip: " + addr.getHostAddress());
return netint.getName();
}
}
}
} catch (SocketException e) {
System.err.println("Socket exception while trying to find current IP");
}
return "";
}
}

View File

@@ -45,7 +45,9 @@ public enum Platform {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
while (!shell.isOutputCompleted()) {} while (!shell.isOutputCompleted()) {
// ignored
}
if (shell.getExitCode() == 0) { if (shell.getExitCode() == 0) {
var out = shell.getOutput(); var out = shell.getOutput();
out = out.split("\n")[0]; out = out.split("\n")[0];

View File

@@ -1,6 +1,5 @@
package com.chameleonvision.settings; package com.chameleonvision.settings;
import com.chameleonvision.network.NetworkManager;
import com.chameleonvision.util.FileHelper; import com.chameleonvision.util.FileHelper;
import com.chameleonvision.vision.camera.CameraManager; import com.chameleonvision.vision.camera.CameraManager;
import com.google.gson.Gson; import com.google.gson.Gson;
@@ -21,16 +20,6 @@ public class SettingsManager {
public static void initialize() { public static void initialize() {
initGeneralSettings(); initGeneralSettings();
// if (manageNetwork) {
// NetworkSettings netSettings = new NetworkSettings();
// netSettings.hostname = GeneralSettings.hostname;
// netSettings.gateway = GeneralSettings.gateway;
// netSettings.netmask = GeneralSettings.netmask;
// netSettings.connectionType = GeneralSettings.connection_type;
// netSettings.ip = GeneralSettings.ip;
// netSettings.run();
// }
var allCameras = CameraManager.getAllCamerasByName(); var allCameras = CameraManager.getAllCamerasByName();
if (!allCameras.containsKey(GeneralSettings.currentCamera) && allCameras.size() > 0) { if (!allCameras.containsKey(GeneralSettings.currentCamera) && allCameras.size() > 0) {
var cam = allCameras.entrySet().stream().findFirst().get().getValue(); var cam = allCameras.entrySet().stream().findFirst().get().getValue();

View File

@@ -101,7 +101,7 @@ public class ShellExec {
* http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4 * http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4
*/ */
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
private class StreamGobbler extends Thread { private static class StreamGobbler extends Thread {
private InputStream is; private InputStream is;
private StringBuilder output; private StringBuilder output;
private volatile boolean completed; // mark volatile to guarantee a thread safety private volatile boolean completed; // mark volatile to guarantee a thread safety

View File

@@ -26,7 +26,7 @@ public class Utilities {
} }
public static List<Integer> getDigits(int num) { public static List<Integer> getDigits(int num) {
List<Integer> digits = new ArrayList<Integer>(); List<Integer> digits = new ArrayList<>();
collectDigits(num, digits); collectDigits(num, digits);
return digits; return digits;
} }

View File

@@ -24,6 +24,7 @@ public class CVProcess {
private Mat binaryMat = new Mat(); private Mat binaryMat = new Mat();
private List<MatOfPoint> filteredContours = new ArrayList<>(); private List<MatOfPoint> filteredContours = new ArrayList<>();
private Comparator<RotatedRect> sortByCentermostComparator = Comparator.comparingDouble(this::calcDistance); private Comparator<RotatedRect> sortByCentermostComparator = Comparator.comparingDouble(this::calcDistance);
private List<MatOfPoint> speckleRejectedContours = new ArrayList<>();
private Comparator<MatOfPoint> sortByMomentsX = Comparator.comparingDouble(this::calcMomentsX); private Comparator<MatOfPoint> sortByMomentsX = Comparator.comparingDouble(this::calcMomentsX);
private List<RotatedRect> finalCountours = new ArrayList<>(); private List<RotatedRect> finalCountours = new ArrayList<>();
private MatOfPoint2f intersectMatA = new MatOfPoint2f(); private MatOfPoint2f intersectMatA = new MatOfPoint2f();
@@ -86,6 +87,20 @@ public class CVProcess {
return filteredContours; return filteredContours;
} }
List<MatOfPoint> rejectSpeckles(List<MatOfPoint> inputContours, Double minimumPercentOfAverage) {
double averageArea = 0.0;
for(MatOfPoint c : inputContours) {
averageArea += Imgproc.contourArea(c);
}
averageArea /= inputContours.size();
var minimumAllowableArea = minimumPercentOfAverage / 100.0 * averageArea;
speckleRejectedContours.clear();
for(MatOfPoint c : inputContours) {
if(Imgproc.contourArea(c) >= minimumAllowableArea) speckleRejectedContours.add(c);
}
return speckleRejectedContours;
}
private double calcDistance(RotatedRect rect) { private double calcDistance(RotatedRect rect) {
return FastMath.sqrt(FastMath.pow(cameraValues.CenterX - rect.center.x, 2) + FastMath.pow(cameraValues.CenterY - rect.center.y, 2)); return FastMath.sqrt(FastMath.pow(cameraValues.CenterX - rect.center.x, 2) + FastMath.pow(cameraValues.CenterY - rect.center.y, 2));
} }

View File

@@ -2,6 +2,7 @@ package com.chameleonvision.vision.process;
import org.opencv.core.RotatedRect; import org.opencv.core.RotatedRect;
@SuppressWarnings("WeakerAccess")
public class PipelineResult { public class PipelineResult {
public boolean IsValid = false; public boolean IsValid = false;
public double CalibratedX = 0.0; public double CalibratedX = 0.0;