This commit is contained in:
sfop
2017-06-02 08:52:46 +02:00
parent feec6adefe
commit 0f87bec392
21 changed files with 843 additions and 884 deletions

View File

@@ -3,20 +3,16 @@
*/
import ch.psi.pshell.core.Context;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import javax.swing.DefaultComboBoxModel;
import ch.psi.pshell.ui.Panel;
import ch.psi.pshell.imaging.ImageListener;
import ch.psi.utils.State;
import ch.psi.utils.IO;
import ch.psi.utils.Chrono;
import ch.psi.utils.swing.SwingUtils;
import ch.psi.utils.swing.TextEditor;
import ch.psi.pshell.epics.PsiCamera;
import ch.psi.pshell.bs.Camtool;
import ch.psi.pshell.core.JsonSerializer;
import ch.psi.pshell.device.DescStatsDouble;
import ch.psi.pshell.device.Device;
import ch.psi.pshell.epics.ChannelInteger;
@@ -38,7 +34,6 @@ import ch.psi.pshell.imaging.Renderer;
import ch.psi.pshell.imaging.RendererListener;
import ch.psi.pshell.imaging.RendererMode;
import ch.psi.pshell.imaging.Source;
import ch.psi.pshell.imaging.Utils;
import ch.psi.pshell.scripting.InterpreterResult;
import ch.psi.pshell.scripting.ScriptManager;
import ch.psi.pshell.swing.ValueSelection;
@@ -96,7 +91,6 @@ import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
*/
public class ScreenPanel extends Panel {
final String CONFIG_FOLDER = "/afs/psi.ch/intranet/SF/Applications/config/camtool_n";
final String CAMERA_DEVICE_NAME = "CurrentCamera";
boolean useCamtoolStats = true;
@@ -104,7 +98,6 @@ public class ScreenPanel extends Panel {
ColormapSource camera;
String cameraName;
String cameraConfigJson;
CameraConfig config;
int polling = 1000;
Overlay marker = null;
JDialog histogramDialog;
@@ -354,7 +347,8 @@ public class ScreenPanel extends Panel {
//cmd = "source /opt/gfa/python\n" + cmd;
//privateServer = Runtime.getRuntime().exec(new String[]{"bash", "-c", cmd});
String cmd = "/opt/gfa/python-3.5/2.4.1/bin/python /opt/gfa/python-3.5/latest/bin/cam_server";
cmd = cmd + " -p " + localServerPort + " -b " + CONFIG_FOLDER;
String configFolder = (String) getContext().getClassByName("SfCamera").getMethod("getConfigFolder", new Class[]{}).invoke(null);
cmd = cmd + " -p " + localServerPort + " -b " + configFolder;
privateServer = Runtime.getRuntime().exec(cmd);
//System.out.println("pid = " + Sys.getPid(privateServer));
long start = System.currentTimeMillis();
@@ -447,16 +441,11 @@ public class ScreenPanel extends Panel {
startTimer(1000);
}
DefaultComboBoxModel getCameraListFromFolder() {
File[] cameraConfigFiles = new File[0];
cameraConfigFiles = IO.listFiles(CONFIG_FOLDER, new String[]{"json"});
Arrays.sort(cameraConfigFiles, (a, b) -> a.compareTo(b));
DefaultComboBoxModel getCameraListFromFolder() throws Exception {
ArrayList<String> cameras = (ArrayList<String>) getContext().getClassByName("SfCamera").getMethod("getCameras", new Class[]{}).invoke(null);
DefaultComboBoxModel model = new DefaultComboBoxModel();
for (File file : cameraConfigFiles) {
String prefix = IO.getPrefix(file);
if (!prefix.startsWith("#") && !prefix.endsWith("_parameters")) {
model.addElement(prefix);
}
for (String cam:cameras) {
model.addElement(cam);
}
return model;
}
@@ -551,116 +540,6 @@ public class ScreenPanel extends Panel {
protected void doUpdate() {
}
public static class CameraConfig {
public HashMap camera;
public HashMap getCalibration() {
return (HashMap) camera.get("calibration");
}
public ArrayList<Integer> getCalibrationRefMarker() {
return (ArrayList<Integer>) getCalibration().get("reference_marker");
}
public double getCalOffsetX() {
ArrayList<Integer> calibration = getCalibrationRefMarker();
double ret = -(calibration.get(0) + calibration.get(2)) / 2;
return ret;
}
public double getCalOffsetY() {
ArrayList<Integer> calibration = getCalibrationRefMarker();
double ret = -(calibration.get(1) + calibration.get(3)) / 2;
return ret;
}
public double getScaleX() {
ArrayList<Integer> calibration = getCalibrationRefMarker();
double width = Math.abs(calibration.get(2) - calibration.get(0));
return getCalibrationWidth() / width;
}
public double getScaleY() {
ArrayList<Integer> calibration = getCalibrationRefMarker();
double height = Math.abs(calibration.get(3) - calibration.get(1));
return getCalibrationHeight() / height;
}
public Double getCalibrationHeight() {
return (Double) getCalibration().get("reference_marker_height");
}
public Double getCalibrationWidth() {
return (Double) getCalibration().get("reference_marker_width");
}
public Double getCalibrationHorizontalAngle() {
return (Double) getCalibration().get("horizontal_camera_angle");
}
public Double getCalibrationVerticalAngle() {
return (Double) getCalibration().get("vertical_camera_angle");
}
public boolean getMirrorX() {
Boolean ret = (Boolean) camera.get("mirror_x");
return (ret == null) ? false : ret;
}
public boolean getMirrorY() {
Boolean ret = (Boolean) camera.get("mirror_y");
return (ret == null) ? false : ret;
}
public int getRotate() {
Integer ret = (Integer) camera.get("rotate");
return (ret == null) ? 0 : ret;
}
public ArrayList<Integer> getRoi() {
return (ArrayList<Integer>) camera.get("roi");
}
public Boolean getRoiEnable() {
if ((camera.get("roi_enable") == null) || (camera.get("roi") == null)) {
return false;
}
return (Boolean) camera.get("roi_enable");
}
}
public static class CameraPars {
public HashMap parameter;
public Boolean getBackgroundSubtraction() {
if (parameter.get("background_subtration") == null){
return false;
}
return (Boolean) parameter.get("background_subtration");
}
public ArrayList<Integer> getRoi() {
return (ArrayList<Integer>) parameter.get("region_of_interest");
}
public Boolean getRoiEnable() {
ArrayList<Integer> roi = getRoi();
return ((roi != null) && (roi.size()>=4));
}
public HashMap getGoodRegion() {
return (HashMap) parameter.get("good_region");
}
public Double getThresholde() {
return (Double) parameter.get("threshold");
}
}
Thread devicesInitTask;
void setCamera(String cameraName) throws IOException, InterruptedException {
@@ -712,7 +591,8 @@ public class ScreenPanel extends Panel {
}
try {
Path configFile = Paths.get(CONFIG_FOLDER, cameraName + ".json");
String configFolder = (String) getContext().getClassByName("SfCamera").getMethod("getConfigFolder", new Class[]{}).invoke(null);
Path configFile = Paths.get(configFolder, cameraName + ".json");
cameraConfigJson = configFile.toFile().exists() ? new String(Files.readAllBytes(configFile)) : null;
if (buttonCamtool.isSelected()) {
@@ -725,67 +605,8 @@ public class ScreenPanel extends Panel {
camera.getConfig().roiWidth = -1;
camera.getConfig().roiHeight = -1;
} else {
if ((cameraConfigJson == null) && (buttonDirect.isSelected())) {
throw new Exception("Cannot open camera config file: " + configFile.toFile());
}
camera = new PsiCamera(CAMERA_DEVICE_NAME, cameraName);
config = (CameraConfig) JsonSerializer.decode(cameraConfigJson, CameraConfig.class);
CameraPars cameraPars = null;
camera.getConfig().roiX = 0; camera.getConfig().roiY = 0;
camera.getConfig().roiWidth = -1; camera.getConfig().roiHeight =-1;
try{
Path parsFile = Paths.get(CONFIG_FOLDER, cameraName + "_parameters.json");
if (parsFile.toFile().exists()){
String cameraParsJson = new String(Files.readAllBytes(parsFile));
cameraPars = (CameraPars) JsonSerializer.decode(cameraParsJson, CameraPars.class);
if (cameraPars.getRoiEnable()){
camera.getConfig().roiX = cameraPars.getRoi().get(0);
camera.getConfig().roiY = cameraPars.getRoi().get(2);
camera.getConfig().roiWidth = cameraPars.getRoi().get(1);
camera.getConfig().roiHeight = cameraPars.getRoi().get(3);
}
}
} catch (Exception ex){
ex.printStackTrace();
}
camera.getConfig().flipHorizontally = config.getMirrorX();
camera.getConfig().flipVertically = config.getMirrorY();
//camera.getConfig().rotation = config.getRotate();
//camera.getConfig().rotationCrop = true;
switch (config.getRotate()) {
case 1:
camera.getConfig().rotation = 270;
break;
case 2:
camera.getConfig().rotation = 180;
break;
case 3:
camera.getConfig().rotation = 90;
break;
}
camera.getConfig().rotationCrop = false;
//if (config.getRoiEnable()){
// camera.getConfig().roiX = config.getRoi().get(0);
// camera.getConfig().roiY = config.getRoi().get(1);
// camera.getConfig().roiWidth = config.getRoi().get(2);
// camera.getConfig().roiHeight = config.getRoi().get(3);
//}
try {
camera.getConfig().spatialCalOffsetX = config.getCalOffsetX();
camera.getConfig().spatialCalOffsetY = config.getCalOffsetY();
camera.getConfig().spatialCalScaleX = -config.getScaleX();
camera.getConfig().spatialCalScaleY = -config.getScaleY();
} catch (Exception ex) {
camera.getConfig().spatialCalOffsetX = Double.NaN;
camera.getConfig().spatialCalOffsetY = Double.NaN;
camera.getConfig().spatialCalScaleX = Double.NaN;
camera.getConfig().spatialCalScaleY = Double.NaN;
}
//camera = new SfCamera(CAMERA_DEVICE_NAME, cameraName);
camera = (ColormapSource) getContext().getClassByName("SfCamera").getConstructor(new Class[]{String.class, String.class}).newInstance(new Object[]{CAMERA_DEVICE_NAME, cameraName});
}
camera.initialize();
camera.assertInitialized();
@@ -805,14 +626,6 @@ public class ScreenPanel extends Panel {
((Camtool) camera).startReceiver();
} else {
/*
if (camera.getConfig() instanceof PsiCamera.PsiCameraConfig){
if (camera.getConfig().isCalibrated()){
camera.getConfig().spatialCalOffsetX += ((PsiCamera.PsiCameraConfig)camera.getConfig()).regionStartX;
camera.getConfig().spatialCalOffsetY += ((PsiCamera.PsiCameraConfig)camera.getConfig()).regionStartY;
}
}
*/
if (polling <= 0) {
camera.setMonitored(true);
} else {