public class signal_identifier {
private static final Log LOG = LogFactory.getLog(signal_identifier.class);
public static void main(String[] args) throws Exception {
long t = cvGetTickCount();
Configuration conf = new Configuration();
long milliSeconds = 1800000;
conf.setLong("mapred.task.timeout", milliSeconds);
Job job = new Job(conf, "TrafficSignalProcessing");
job.setJarByClass(signal_identifier.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(signal_mapper.class);
job.setReducerClass(signal_reducer.class);
job.setInputFormatClass(VideoInputFormat.class);
job.setOutputValueClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/tmp/traffic_signal.mp4"));
FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/tmp/ouputv"));
job.waitForCompletion(true);
}
}
Mapper Class:
public class signal_mapper extends Mapper<Text, VideoObject, Text, IntWritable> {
private static final Log LOG = LogFactory.getLog(signal_mapper.class);
private static OpenCVFrameGrabber grabber;
private static IplImage currentFrame;
private static IplImage frame;
private static IplImage imgHSV;
private static IplImage imgThresholdr;
private static IplImage imgThresholdg;
private static IplImage imgC;
static int LowerRed = 160;
static int UpperRed = 180;
static int LowerGreen = 40;
static int UpperGreen = 80;
CvArr mask;
//private static final int FOURCC = CV_FOURCC('X', 'V', 'I', 'D');
public void map(Text key, VideoObject value, Context context, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException, InterruptedException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(value.getVideoByteArray());
LOG.info("Log__VideoConverter__byteArray: "+ byteArrayInputStream.available());
String fileName = key.toString();
int id = value.getId();
long differencer = 0;
long differenceg = 0;
long lStartTime = 0;
String flag = "start";
//LocalFileSystem fs = FileSystem.getLocal(context.getConfiguration());
Path filePath = new Path("/tmp", fileName);
//Path resFile = new Path("/tmp", "res_"+fileName);
System.out.println("File to Process :"+filePath.toString());
//FSDataOutputStream out = fs.create(filePath, true);
//out.write(value.getVideoByteArray());
//out.close();
try{
grabber = new OpenCVFrameGrabber(filePath.toString());
grabber.start();
CvMemStorage storage = CvMemStorage.create();
CvSize frameSize = new CvSize(grabber.getImageWidth(), grabber.getImageHeight());
currentFrame = cvCreateImage(frameSize, 8, 3);
IplImage cropped;// = cvCreateImage(frameSize, 8, 3);
CvRect r = new CvRect(250, 40, 350, 350);
System.out.println("Video processing .........started");
while(queryFrame()) {
cvClearMemStorage(storage);
if(flag.equals("start")){
lStartTime = new Date().getTime();
}
cvSetImageROI(currentFrame, r);
cropped = cvCreateImage(cvGetSize(currentFrame), currentFrame.depth(),currentFrame.nChannels());
// Copy original image (only ROI) to the cropped image
cvCopy(currentFrame, cropped);
imgHSV = cvCreateImage(cvGetSize(cropped), 8, 3);
cvCvtColor(cropped, imgHSV, CV_BGR2HSV);
imgThresholdr = cvCreateImage(cvGetSize(cropped), 8, 1);
imgThresholdg = cvCreateImage(cvGetSize(cropped), 8, 1);
imgC = cvCreateImage(cvGetSize(cropped),8,1);
cvInRangeS(imgHSV, cvScalar(LowerRed,150,75,0), cvScalar(UpperRed, 255, 255, 0), imgThresholdr);
cvInRangeS(imgHSV, cvScalar(LowerGreen,150,75,0), cvScalar(UpperGreen, 255, 255, 0), imgThresholdg);
Dimension positionr = getCoordinates(imgThresholdr);
int posr = positionr.width+positionr.height;
Dimension positiong = getCoordinates(imgThresholdg);
int posg = positiong.width+positiong.height;
//&& !flag.equalsIgnoreCase("red") && !flag.equalsIgnoreCase("green")
if(posr > 255 && posr < 265 ){
flag = "red";
}else {
long lEndTime = new Date().getTime();
differenceg = (lEndTime - lStartTime) - differencer;
output.collect(new Text("Green Color found at second- => "),new IntWritable((int)differenceg/1000));
//System.out.println("Green Color found at second: " + differenceg/1000);
}
if(posg > 430 && posg < 440){
flag = "green";
}else{
long lEndTime = new Date().getTime();
differencer = (lEndTime - lStartTime) - differenceg;
output.collect(new Text("Red Color found at second- => "),new IntWritable((int)differencer/1000));
//System.out.println("Red Color found at second: " + differencer/1000);
}
}
grabber.stop();
System.out.println("Video processing .........Completed");
}catch(Exception e) {
e.printStackTrace();
}
}
private static boolean queryFrame() throws Exception {
try {
IplImage frame = grabber.grab();
if (frame != null) {
cvConvertImage(frame, currentFrame, 0);
return true;
} else {
return false;
}
}catch(com.googlecode.javacv.FrameGrabber.Exception fge) {
return false;
}
catch(Exception e) {
return false;
}
}
static Dimension getCoordinates(IplImage thresholdImage) {
int posX = 0;
int posY = 0;
CvMoments moments = new CvMoments();
cvMoments(thresholdImage, moments, 1);
double momX10 = cvGetSpatialMoment(moments, 1, 0);
double momY01 = cvGetSpatialMoment(moments, 0, 1);
double area = cvGetCentralMoment(moments, 0, 0);
posX = (int) (momX10 / area);
posY = (int) (momY01 / area);
return new Dimension(posX, posY);
}
}
Reducer Class:
public class signal_reducer extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}
Wait I'll post my stack trace for Exception
Related
Currently, I've been asked to write CNN code using DL4J using YOLOv2 architecture. But the problem is after the model has complete, I do a simple GUI for validation testing then the image shown is too bright and sometimes the image can be displayed. Im not sure where does this problem comes from whether at earliest stage of training or else. Here, I attach the code that I have for now. For Iterator:
public class faceMaskIterator {
private static final Logger log = org.slf4j.LoggerFactory.getLogger(faceMaskIterator.class);
private static final int seed = 123;
private static Random rng = new Random(seed);
private static String dataDir;
private static Path pathDirectory;
private static InputSplit trainData, testData;
private static final String[] allowedFormats = NativeImageLoader.ALLOWED_FORMATS;
private static final double splitRatio = 0.8;
private static final int nChannels = 3;
public static final int gridWidth = 13;
public static final int gridHeight = 13;
public static final int yolowidth = 416;
public static final int yoloheight = 416;
private static RecordReaderDataSetIterator makeIterator(InputSplit split, Path dir, int batchSize) throws Exception {
ObjectDetectionRecordReader recordReader = new ObjectDetectionRecordReader(yoloheight, yolowidth, nChannels,
gridHeight, gridWidth, new VocLabelProvider(dir.toString()));
recordReader.initialize(split);
RecordReaderDataSetIterator iter = new RecordReaderDataSetIterator(recordReader, batchSize, 1, 1,true);
iter.setPreProcessor(new ImagePreProcessingScaler(0, 1));
return iter;
}
public static RecordReaderDataSetIterator trainIterator(int batchSize) throws Exception {
return makeIterator(trainData, pathDirectory, batchSize);
}
public static RecordReaderDataSetIterator testIterator(int batchSize) throws Exception {
return makeIterator(testData, pathDirectory, batchSize);
}
public static void setup() throws IOException {
log.info("Load data...");
dataDir = Paths.get(
System.getProperty("user.home"),
Helper.getPropValues("dl4j_home.data")
).toString();
pathDirectory = Paths.get(dataDir,"face_mask_dataset");
FileSplit fileSplit = new FileSplit(new File(pathDirectory.toString()),allowedFormats,rng);
PathFilter pathFilter = new RandomPathFilter(rng,allowedFormats);
InputSplit[] sample = fileSplit.sample(pathFilter, splitRatio,1-splitRatio);
trainData = sample[0];
testData = sample[1];
}}
For training:
public class faceMaskPreTrained {
private static final Logger log = LoggerFactory.getLogger(ai.certifai.groupProjek.faceMaskPreTrained.class);
private static int seed = 420;
private static double detectionThreshold = 0.9;
private static int nBoxes = 3;
private static double lambdaNoObj = 0.7;
private static double lambdaCoord = 1.0;
private static double[][] priorBoxes = {{1, 1}, {2, 1}, {1, 2}};
private static int batchSize = 3;
private static int nEpochs = 1;
private static double learningRate = 1e-4;
private static int nClasses = 3;
private static List<String> labels;
private static File modelFilename = new File(System.getProperty("user.dir"), "generated-models/facemask_detector.zip");
private static ComputationGraph model;
private static Frame frame = null;
private static final Scalar GREEN = RGB(0, 255.0, 0);
private static final Scalar YELLOW = RGB(255, 255, 0);
private static final Scalar RED = RGB(255, 0, 0);
private static Scalar[] colormap = {GREEN, YELLOW, RED};
private static String labeltext = null;
public static void main(String[] args) throws Exception {
faceMaskIterator.setup();
RecordReaderDataSetIterator trainIter = faceMaskIterator.trainIterator(batchSize);
RecordReaderDataSetIterator testIter = faceMaskIterator.testIterator(1);
labels = trainIter.getLabels();
if (modelFilename.exists()) {
Nd4j.getRandom().setSeed(seed);
log.info("Load model...");
model = ModelSerializer.restoreComputationGraph(modelFilename);
} else {
Nd4j.getRandom().setSeed(seed);
INDArray priors = Nd4j.create(priorBoxes);
log.info("Build model...");
ComputationGraph pretrained = (ComputationGraph) YOLO2.builder().build().initPretrained();
FineTuneConfiguration fineTuneConf = getFineTuneConfiguration();
model = getComputationGraph(pretrained, priors, fineTuneConf);
System.out.println(model.summary(InputType.convolutional(
faceMaskIterator.yoloheight,
faceMaskIterator.yolowidth,
nClasses)));
log.info("Train model...");
UIServer server = UIServer.getInstance();
StatsStorage storage = new InMemoryStatsStorage();
server.attach(storage);
model.setListeners(new ScoreIterationListener(5), new StatsListener(storage,5));
for (int i = 1; i < nEpochs + 1; i++) {
trainIter.reset();
while (trainIter.hasNext()) {
model.fit(trainIter.next());
}
log.info("*** Completed epoch {} ***", i);
}
ModelSerializer.writeModel(model, modelFilename, true);
System.out.println("Model saved.");
}
// Evaluate the model's accuracy by using the test iterator.
OfflineValidationWithTestDataset(testIter);
// Inference the model and process the webcam stream and make predictions.
doInference();
}
private static ComputationGraph getComputationGraph(ComputationGraph pretrained, INDArray priors, FineTuneConfiguration fineTuneConf) {
return new TransferLearning.GraphBuilder(pretrained)
.fineTuneConfiguration(fineTuneConf)
.removeVertexKeepConnections("conv2d_23")
.removeVertexKeepConnections("outputs")
.addLayer("conv2d_23",
new ConvolutionLayer.Builder(1, 1)
.nIn(1024)
.nOut(nBoxes * (5 + nClasses))
.stride(1, 1)
.convolutionMode(ConvolutionMode.Same)
.weightInit(WeightInit.XAVIER)
.activation(Activation.IDENTITY)
.build(),
"leaky_re_lu_22")
.addLayer("outputs",
new Yolo2OutputLayer.Builder()
.lambdaNoObj(lambdaNoObj)
.lambdaCoord(lambdaCoord)
.boundingBoxPriors(priors.castTo(DataType.FLOAT))
.build(),
"conv2d_23")
.setOutputs("outputs")
.build();
}
private static FineTuneConfiguration getFineTuneConfiguration() {
return new FineTuneConfiguration.Builder()
.seed(seed)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.gradientNormalization(GradientNormalization.RenormalizeL2PerLayer)
.gradientNormalizationThreshold(1.0)
.updater(new Adam.Builder().learningRate(learningRate).build())
.l2(0.00001)
.activation(Activation.IDENTITY)
.trainingWorkspaceMode(WorkspaceMode.ENABLED)
.inferenceWorkspaceMode(WorkspaceMode.ENABLED)
.build();
}
// Evaluate visually the performance of the trained object detection model
private static void OfflineValidationWithTestDataset(RecordReaderDataSetIterator test) throws InterruptedException {
NativeImageLoader imageLoader = new NativeImageLoader();
CanvasFrame canvas = new CanvasFrame("Validate Test Dataset");
OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
org.deeplearning4j.nn.layers.objdetect.Yolo2OutputLayer yout = (org.deeplearning4j.nn.layers.objdetect.Yolo2OutputLayer) model.getOutputLayer(0);
Mat convertedMat = new Mat();
Mat convertedMat_big = new Mat();
while (test.hasNext() && canvas.isVisible()) {
org.nd4j.linalg.dataset.DataSet ds = test.next();
INDArray features = ds.getFeatures();
INDArray results = model.outputSingle(features);
List<DetectedObject> objs = yout.getPredictedObjects(results, detectionThreshold);
YoloUtils.nms(objs, 0.4);
Mat mat = imageLoader.asMat(features);
mat.convertTo(convertedMat, CV_8U, 255, 0);
int w = mat.cols() * 2;
int h = mat.rows() * 2;
resize(convertedMat, convertedMat_big, new Size(w, h));
convertedMat_big = drawResults(objs, convertedMat_big, w, h);
canvas.showImage(converter.convert(convertedMat_big));
canvas.waitKey();
}
canvas.dispose();
}
// Stream video frames from Webcam and run them through YOLOv2 model and get predictions
private static void doInference() {
String cameraPos = "front";
int cameraNum = 0;
Thread thread = null;
NativeImageLoader loader = new NativeImageLoader(
faceMaskIterator.yolowidth,
faceMaskIterator.yoloheight,
3,
new ColorConversionTransform(COLOR_BGR2RGB));
ImagePreProcessingScaler scaler = new ImagePreProcessingScaler(0, 1);
if (!cameraPos.equals("front") && !cameraPos.equals("back")) {
try {
throw new Exception("Unknown argument for camera position. Choose between front and back");
} catch (Exception e) {
e.printStackTrace();
}
}
FrameGrabber grabber = null;
try {
grabber = FrameGrabber.createDefault(cameraNum);
} catch (FrameGrabber.Exception e) {
e.printStackTrace();
}
OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
try {
grabber.start();
} catch (FrameGrabber.Exception e) {
e.printStackTrace();
}
CanvasFrame canvas = new CanvasFrame("Object Detection");
int w = grabber.getImageWidth();
int h = grabber.getImageHeight();
canvas.setCanvasSize(w, h);
while (true) {
try {
frame = grabber.grab();
} catch (FrameGrabber.Exception e) {
e.printStackTrace();
}
//if a thread is null, create new thread
if (thread == null) {
thread = new Thread(() ->
{
while (frame != null) {
try {
Mat rawImage = new Mat();
//Flip the camera if opening front camera
if (cameraPos.equals("front")) {
Mat inputImage = converter.convert(frame);
flip(inputImage, rawImage, 1);
} else {
rawImage = converter.convert(frame);
}
Mat resizeImage = new Mat();
resize(rawImage, resizeImage, new Size(faceMaskIterator.yolowidth, faceMaskIterator.yoloheight));
INDArray inputImage = loader.asMatrix(resizeImage);
scaler.transform(inputImage);
INDArray outputs = model.outputSingle(inputImage);
org.deeplearning4j.nn.layers.objdetect.Yolo2OutputLayer yout = (org.deeplearning4j.nn.layers.objdetect.Yolo2OutputLayer) model.getOutputLayer(0);
List<DetectedObject> objs = yout.getPredictedObjects(outputs, detectionThreshold);
YoloUtils.nms(objs, 0.4);
rawImage = drawResults(objs, rawImage, w, h);
canvas.showImage(converter.convert(rawImage));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
thread.start();
}
KeyEvent t = null;
try {
t = canvas.waitKey(33);
} catch (InterruptedException e) {
e.printStackTrace();
}
if ((t != null) && (t.getKeyCode() == KeyEvent.VK_Q)) {
break;
}
}
}
private static Mat drawResults(List<DetectedObject> objects, Mat mat, int w, int h) {
for (DetectedObject obj : objects) {
double[] xy1 = obj.getTopLeftXY();
double[] xy2 = obj.getBottomRightXY();
String label = labels.get(obj.getPredictedClass());
int x1 = (int) Math.round(w * xy1[0] / faceMaskIterator.gridWidth);
int y1 = (int) Math.round(h * xy1[1] / faceMaskIterator.gridHeight);
int x2 = (int) Math.round(w * xy2[0] / faceMaskIterator.gridWidth);
int y2 = (int) Math.round(h * xy2[1] / faceMaskIterator.gridHeight);
//Draw bounding box
rectangle(mat, new Point(x1, y1), new Point(x2, y2), colormap[obj.getPredictedClass()], 2, 0, 0);
//Display label text
labeltext = label + " " + String.format("%.2f", obj.getConfidence() * 100) + "%";
int[] baseline = {0};
Size textSize = getTextSize(labeltext, FONT_HERSHEY_DUPLEX, 1, 1, baseline);
rectangle(mat, new Point(x1 + 2, y2 - 2), new Point(x1 + 2 + textSize.get(0), y2 - 2 - textSize.get(1)), colormap[obj.getPredictedClass()], FILLED, 0, 0);
putText(mat, labeltext, new Point(x1 + 2, y2 - 2), FONT_HERSHEY_DUPLEX, 1, RGB(0, 0, 0));
}
return mat;
}
CanvasFrame tries to do gamma correction by default because it's typically needed by cameras used for CV, but cheap webcams usually output gamma corrected images, so make sure to let CanvasFrame know about it this way:
// We should also specify the relative monitor/camera response for proper gamma correction.
CanvasFrame frame = new CanvasFrame("Some Title", CanvasFrame.getDefaultGamma()/grabber.getGamma());
https://github.com/bytedeco/javacv/
I want to make an imitative hack System. There will be 3 windows displayed on screen. Each window will show some string Constantly (like some movie scene) . However only the third (the last) window works. So how to make every window show string at the same time?
the frame class as follow:
import java.awt.*;
import java.awt.event.*;
class sys extends Thread {
private static Frame frm;
private static TextArea txa;
private int fsx, fsy, flx, fly, tsx, tsy, tlx, tly;
private String strarr[] = new String[7];
private String frmName;
public sys(String str, String SAEC[]) {
frmName = str;
strarr = SAEC;
}
public void SettingFRM(int sx, int sy, int lx, int ly) {
//frame's location and size
fsx = sx;
fsy = sy;
flx = lx;
fly = ly;
}
public void SettingTXA(int sx, int sy, int lx, int ly) {
//textArea's location and size
tsx = sx;
tsy = sy;
tlx = lx;
tly = ly;
}
public void run() {
frm = new Frame(frmName);
//the exterior design
txa = new TextArea("", 100, 100, TextArea.SCROLLBARS_BOTH);
txa.setBounds(tlx, tly, tsx, tsy);
txa.setBackground(Color.darkGray);
txa.setFont(new Font("Arial", Font.PLAIN, 16));
txa.setForeground(Color.green);
frm.setLayout(null);
frm.setSize(fsx, fsy);
frm.setLocation(flx, fly);
frm.setVisible(true);
frm.setBackground(Color.darkGray);
frm.add(txa);
while (1 != 0) {
txa.append(strarr[(int) (Math.random() * 7)]);// to obtain new string
frm.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
try {
sleep(200);
} catch (InterruptedException e) {
}
}
}
}
main program as follow:
public class ImitativeHackSystem {
public static void main(String[] args) throws InterruptedException {
//specific string
String strarr[] = new String[9];
strarr[0] = new String("Hacking... Time estimate 25 seconds...\n");
strarr[1] = new String("Retrying...\n");
strarr[2] = new String("Error! Code:e2130443523\n");
strarr[3] = new String("Success! IP:192.168.0.1\n");
strarr[4] = new String("picking datas...\n");
strarr[5] = new String("Anti-system started\n");
strarr[6] = new String("Has been discovering... fake random IP address\n");
strarr[7] = new String("01011010000011001000000011111101010");
strarr[8] = new String("111101010101001101101101010011010");
//object array
sys fhs[] = new sys[3];
for (int i = 0; i < 3; i++)
fhs[i] = new sys("Fake Hacking System", strarr);
fhs[0].SettingTXA(635, 690, 5, 30);
fhs[0].SettingFRM(640, 720, 0, 0);
fhs[1].SettingTXA(635, 330, 5, 30);
fhs[1].SettingFRM(640, 360, 645, 0);
fhs[2].SettingTXA(635, 330, 5, 30);
fhs[2].SettingFRM(640, 360, 645, 365);
//to execute
for (int i = 0; i < 3; i++) {
fhs[i].start();
Thread.sleep(500);
}
}
}
I face a problem with getting data from CandleStickChart and adding this into LineChart.
Ok, so look at this method. I got data from CandleStick and configure data to LineChart. Everything work perfectly. I have generated 2 chars like you guys see on Image:
But the problem is when i trying to add dynamicData to chars. My method only add data to CandleStickChart.
Here is method to setData on LineChart, here CandleEntry lastEntry = set1.getEntryForIndex(i); i know what value is on bars.
private void setData() {
int prog = 50;
for (int i = 0; i < prog; i++) {
CandleEntry lastEntry = set1.getEntryForIndex(i);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
entries.add(new Entry(i, lastOpenCloseMax));
}
XAxis xAxis = lineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextColor(Color.TRANSPARENT);
xAxis.setDrawGridLines(false);
YAxis leftAxis = lineChart.getAxisLeft();
leftAxis.setLabelCount(12, false);
leftAxis.setDrawGridLines(false);
leftAxis.setAxisMaximum(1.6f);
leftAxis.setAxisMinimum(0.4f);
leftAxis.setDrawAxisLine(true);
leftAxis.setTextColor(Color.TRANSPARENT);
YAxis rightAxis = lineChart.getAxisRight();
rightAxis.setEnabled(false);
Collections.sort(entries, new EntryXComparator());
lineDataSet = new LineDataSet(entries, "# of Calls");
lineData = new LineData(lineDataSet);
lineDataSet.setColor(Color.GRAY);
lineDataSet.setDrawCircles(false);
// dataset.setDrawFilled(true);
lineData.setValueTextColor(Color.TRANSPARENT);
lineChart.getLegend().setEnabled(false);
lineChart.getDescription().setEnabled(false);
lineChart.setBackgroundColor(Color.TRANSPARENT);
lineChart.setData(lineData);
lineChart.animateX(4000);
}
Here i addLineEntry, but this not work and i dont know why, do you guys have any idea?
private void addLineEntry() {
lineData = lineChart.getData();
if (lineDataSet == null) {
lineDataSet = createLineSet();
lineData.addDataSet(lineDataSet);
}
int prog = 1;
for (int i = 0; i < prog; i++) {
CandleEntry lastEntry = set1.getEntryForIndex(yVals1.size() - 1);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
entries.add(new Entry(lineDataSet.getXMax() +1, lastOpenCloseMax));
}
lineDataSet.notifyDataSetChanged();
lineChart.notifyDataSetChanged();
lineChart.invalidate();
mChart.moveViewTo(mChart.getXChartMax(), 2f, YAxis.AxisDependency.RIGHT);
}
And the last is similar method from CandleStickChart to addEntry.
private void addEntry(boolean start) {
data = mChart.getData();
if (set1 == null) {
set1 = createSet();
data.addDataSet(set1);
}
float highmax = 1.0700f;
float highlow = 1.1700f;
float lowmax = 0.5700f;
float lowlow = 0.63000f;
int prog = 1;
int xMax = (int) set1.getXMax();
CandleEntry lastEntry = set1.getEntryForIndex(yVals1.size() - 1);
for (int i = 0; i < prog; i++) {
float open = highlow + new Random().nextFloat() * (highmax - highlow);
float close = lowlow + new Random().nextFloat() * (lowmax - lowlow);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
float currentOpenCloseMax = Math.max(open, close);
float currentOpenCloseMin = Math.min(open, close);
float high = open + 0.3f;
float low = close - 0.3f;
if (currentOpenCloseMax < lastOpenCloseMax) {
yVals1.add(new CandleEntry(xMax + 1, high, low, currentOpenCloseMax, currentOpenCloseMin));
} else {
yVals1.add(new CandleEntry(xMax + 1, high, low, currentOpenCloseMin, currentOpenCloseMax));
}
mChart.notifyDataSetChanged();
mChart.invalidate();
mChart.moveViewTo(mChart.getXChartMax(), 2f, YAxis.AxisDependency.RIGHT);
}
}
And here is method to remove Entry from CandleStickChart and LineChart, and this work good.
private void removeLastEntry() {
CandleData data = mChart.getData();
if (data != null) {
ICandleDataSet set = data.getDataSetByIndex(0);
if (set != null) {
set.removeFirst();
data.notifyDataChanged();
mChart.notifyDataSetChanged();
mChart.invalidate();
}
}
LineData lineData = lineChart.getData();
if (lineData != null) {
ILineDataSet set = lineData.getDataSetByIndex(0);
if (set != null) {
set.removeFirst();
data.notifyDataChanged();
lineChart.notifyDataSetChanged();
lineChart.invalidate();
}
}
Do you guys have any idea whats wrong?
I started a bounty:
Here isfull class:
public class MainGameFragment extends Fragment {
#BindView(R.id.spinner_money)
Spinner spinnerData;
#BindView(R.id.text_profit_ill)
TextView text_profit;
#BindView(R.id.button_cash)
Button btnCashCurrency;
#BindView(R.id.restart_game)
Button restartGame;
#BindView(R.id.butonCurrency)
Button buttonCurrency;
#BindView(R.id.chart)
CandleStickChart mChart;
#BindView(R.id.progress_bar)
ProgressBar progress;
#BindView(R.id.btn_buy)
Button btnBuy;
#BindView(R.id.btn_sell)
Button btnSell;
#BindView(R.id.drawer_settings)
ImageButton openDrawerSettings;
#BindView(R.id.chartLine)
LineChart lineChart;
public static ArrayList<String> HISTORYTRANSACTION = new ArrayList<>();
public static ArrayList<String> LEADERBOARDUSER = new ArrayList<>();
public static String userNameAndScore;
private Handler handler;
private Handler handlerLast;
private String buttonPosition;
int pos = 0;
LostDialogFragment lostFragment = LostDialogFragment.newInstance(1);
WinDialogFragment winFragment = WinDialogFragment.newInstance(1);
SettingsFragment settingsFragment = SettingsFragment.newInstance(1);
String DIALOG_WIN = "WinDialogFragment";
String DIALOG_LOST = "LostDialogFragment";
String DIALOG_SETTINGS = "settingsFragment";
private CandleData data;
private LineData lineData;
private LineDataSet lineDataSet;
private CandleDataSet set1;
private Drawer result;
public static StorageReference storageReference;
private Runnable r;
private Runnable rLast;
ArrayList<CandleEntry> yVals1 = new ArrayList<>();
ArrayList<Entry> entries = new ArrayList<>();
public MainGameFragment() {
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
buttonPosition = getArguments().getString("button_position", "value");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_game, container, false);
ButterKnife.bind(this, view);
setText();
configureSpinnerDataAndLogic();
configureChart();
configureColorProgresBar();
openDrawer();
configureDrawer();
welcomeMessage();
configureHandler(5000);
storageReference = FirebaseStorage.getInstance().getReference();
setData();
return view;
}
private void setData() {
int prog = 50;
for (int i = 0; i < prog; i++) {
CandleEntry lastEntry = set1.getEntryForIndex(i);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
entries.add(new Entry(i, lastOpenCloseMax));
}
XAxis xAxis = lineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextColor(Color.TRANSPARENT);
xAxis.setDrawGridLines(false);
YAxis leftAxis = lineChart.getAxisLeft();
leftAxis.setLabelCount(12, false);
leftAxis.setDrawGridLines(false);
leftAxis.setAxisMaximum(1.6f);
leftAxis.setAxisMinimum(0.4f);
leftAxis.setDrawAxisLine(true);
leftAxis.setTextColor(Color.TRANSPARENT);
YAxis rightAxis = lineChart.getAxisRight();
rightAxis.setEnabled(false);
Collections.sort(entries, new EntryXComparator());
lineDataSet = new LineDataSet(entries, "# of Calls");
lineData = new LineData(lineDataSet);
lineDataSet.setColor(Color.GRAY);
lineDataSet.setDrawCircles(false);
// dataset.setDrawFilled(true);
lineData.setValueTextColor(Color.TRANSPARENT);
lineChart.getLegend().setEnabled(false);
lineChart.getDescription().setEnabled(false);
lineChart.setBackgroundColor(Color.TRANSPARENT);
lineChart.setData(lineData);
lineChart.animateX(4000);
}
private void welcomeMessage() {
Toast.makeText(getContext(), "Welcome " + MainActivity.getUsername(getContext()).trim() + "!"
+ " Getting data from last hour..",
Toast.LENGTH_LONG).show();
}
private void configureDrawer() {
AccountHeader headerResult = new AccountHeaderBuilder()
.withActivity(getActivity())
.withProfileImagesClickable(false)
.withHeaderBackground(R.drawable.logo_white)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
#Override
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
return false;
}
})
.build();
result = new DrawerBuilder()
.withSliderBackgroundColor(Color.GRAY)
.withAccountHeader(headerResult)
.withActivity(getActivity())
.withDisplayBelowStatusBar(false)
.withDrawerGravity(Gravity.LEFT)
.withHeaderPadding(true)
.addDrawerItems(
new SectionDrawerItem().withName("Options"),
new PrimaryDrawerItem().withName("Trading History").withIcon(R.drawable.trading_history).withIdentifier(2),
new PrimaryDrawerItem().withName("Leader Board").withIcon(R.drawable.leade_board).withIdentifier(3),
new PrimaryDrawerItem().withName("Special offer").withIcon(R.drawable.special_icon).withIdentifier(4),
new PrimaryDrawerItem().withName("Video tutorials").withIcon(R.drawable.video_tutorials).withIdentifier(5),
new PrimaryDrawerItem().withName("FAQ").withIcon(R.drawable.faq_icon).withIdentifier(6),
new PrimaryDrawerItem().withName("CONTACT").withIcon(R.drawable.contact_icon).withIdentifier(7)
)
.buildForFragment();
result.setOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.addToBackStack(null);
SettingsFragment.POSITION = position;
result.closeDrawer();
if (settingsFragment != null) {
settingsFragment.show(ft, DIALOG_SETTINGS);
}
return true;
}
});
result.getDrawerLayout().setFitsSystemWindows(false);
result.getSlider().setFitsSystemWindows(false);
}
private CandleDataSet createSet() {
set1 = new CandleDataSet(null, "DataSet 1");
set1.setColor(Color.rgb(240, 99, 99));
set1.setHighLightColor(Color.rgb(190, 190, 190));
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setValueTextSize(10f);
return set1;
}
private LineDataSet createLineSet() {
lineDataSet = new LineDataSet(null, "DataSet 1");
lineDataSet.setColor(Color.rgb(240, 99, 99));
lineDataSet.setHighLightColor(Color.rgb(190, 190, 190));
lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
lineDataSet.setValueTextSize(10f);
return lineDataSet;
}
public void openDrawer() {
openDrawerSettings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.openDrawer();
}
});
}
private void addLineEntry() {
lineData = lineChart.getData();
if (lineDataSet == null) {
lineDataSet = createLineSet();
lineData.addDataSet(lineDataSet);
}
int prog = 1;
int xMax = (int) set1.getXMax();
for (int i = 0; i < prog; i++) {
CandleEntry lastEntry = set1.getEntryForIndex(xMax -1);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
entries.add(new Entry(lineData.getDataSetCount() +1, lastOpenCloseMax));
}
lineDataSet.notifyDataSetChanged();
lineChart.notifyDataSetChanged();
lineChart.invalidate();
lineChart.moveViewTo(lineChart.getXChartMax(), 2f, YAxis.AxisDependency.RIGHT);
}
private void addEntry(boolean start) {
data = mChart.getData();
if (set1 == null) {
set1 = createSet();
data.addDataSet(set1);
}
float highmax = 1.0700f;
float highlow = 1.1700f;
float lowmax = 0.5700f;
float lowlow = 0.63000f;
int prog = 1;
int xMax = (int) set1.getXMax();
CandleEntry lastEntry = set1.getEntryForIndex(yVals1.size() - 1);
for (int i = 0; i < prog; i++) {
float open = highlow + new Random().nextFloat() * (highmax - highlow);
float close = lowlow + new Random().nextFloat() * (lowmax - lowlow);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
float currentOpenCloseMax = Math.max(open, close);
float currentOpenCloseMin = Math.min(open, close);
float high = open + 0.3f;
float low = close - 0.3f;
if (currentOpenCloseMax < lastOpenCloseMax) {
yVals1.add(new CandleEntry(xMax + 1, high, low, currentOpenCloseMax, currentOpenCloseMin));
} else {
yVals1.add(new CandleEntry(xMax + 1, high, low, currentOpenCloseMin, currentOpenCloseMax));
}
mChart.notifyDataSetChanged();
mChart.invalidate();
mChart.moveViewTo(mChart.getXChartMax(), 2f, YAxis.AxisDependency.RIGHT);
}
}
private void removeLastEntry() {
CandleData data = mChart.getData();
if (data != null) {
ICandleDataSet set = data.getDataSetByIndex(0);
if (set != null) {
set.removeFirst();
data.notifyDataChanged();
mChart.notifyDataSetChanged();
mChart.invalidate();
}
}
LineData lineData = lineChart.getData();
if (lineData != null) {
ILineDataSet set = lineData.getDataSetByIndex(0);
if (set != null) {
set.removeFirst();
data.notifyDataChanged();
lineChart.notifyDataSetChanged();
lineChart.invalidate();
}
}
}
public String getUserInfoAndSave() {
userNameAndScore = MainActivity.getUsername(getContext()).trim() + ": "
+ btnCashCurrency.getText().toString().trim();
return userNameAndScore;
}
private void configureHandlerWithoutRemoveLastEntry(final int time) {
handlerLast = new Handler();
rLast = new Runnable() {
public void run() {
// removeLastEntry();
addEntry(true);
handler.postDelayed(this, time);
}
};
handlerLast.postDelayed(rLast, time);
}
private void configureHandler(final int time) {
handler = new Handler();
r = new Runnable() {
public void run() {
removeLastEntry();
addEntry(true);
addLineEntry();
handler.postDelayed(this, time);
}
};
handler.postDelayed(r, time);
}
public void stopLast() {
handlerLast.removeCallbacks(rLast);
}
public void stop() {
handler.removeCallbacks(r);
}
private void configureChart() {
mChart.getDescription().setEnabled(false);
mChart.getLegend().setTextColor(Color.WHITE);
mChart.setMaxVisibleValueCount(50);
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextColor(Color.WHITE);
xAxis.setDrawGridLines(false);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setLabelCount(12, false);
leftAxis.setDrawGridLines(false);
leftAxis.setDrawAxisLine(true);
leftAxis.setTextColor(Color.WHITE);
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setEnabled(false);
float highmax = 1.0700f;
float highlow = 1.1700f;
float lowmax = 0.5700f;
float lowlow = 0.63000f;
int prog = 50;
float last = Float.NEGATIVE_INFINITY;
String date = String.valueOf(android.text.format.DateFormat.format("yyyy-MM-dd", new java.util.Date()));
for (int i = 0; i < prog; i++) {
float open = highlow + new Random().nextFloat() * (highmax - highlow);
float close = lowlow + new Random().nextFloat() * (lowmax - lowlow);
float max = Math.max(open, close);
if (last < max) {
float tmp = open;
open = close;
close = tmp;
}
last = max;
float high = open + 0.3f;
float low = close - 0.3f;
yVals1.add(new CandleEntry(i, high, low, open, close));
}
set1 = new CandleDataSet(yVals1, date);
data = new CandleData(set1);
mChart.setData(data);
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setColor(Color.rgb(80, 80, 80));
set1.setIncreasingColor(Color.GREEN);
set1.setIncreasingPaintStyle(Paint.Style.FILL);
set1.setDecreasingColor(Color.RED);
set1.setDecreasingPaintStyle(Paint.Style.FILL);
set1.setNeutralColor(Color.BLUE);
set1.setBarSpace(0.2f);
set1.setValueTextColor(Color.TRANSPARENT);
mChart.notifyDataSetChanged();
mChart.animateX(4000);
}
private void setText() {
buttonCurrency.setText("Assets: \n" + buttonPosition);
}
private void configureColorProgresBar() {
progress.getIndeterminateDrawable().setColorFilter(
getResources().getColor(R.color.white),
android.graphics.PorterDuff.Mode.SRC_IN);
}
#OnClick(R.id.invest_text)
public void invest() {
float highmax = 1.0700f;
float highlow = 1.1700f;
float lowmax = 0.5700f;
float lowlow = 0.63000f;
float open = highlow + new Random().nextFloat() * (highmax - highlow);
float close = lowlow + new Random().nextFloat() * (lowmax - lowlow);
float high = open + 0.3f;
float low = close - 0.3f;
if (pos == 0) {
yVals1.add(new CandleEntry(50, high, low, open, close));
pos++;
} else if (pos == 1) {
yVals1.add(new CandleEntry(51, high, low, open, close));
pos++;
} else if (pos == 2) {
yVals1.add(new CandleEntry(52, high, low, open, close));
pos++;
} else if (pos == 3) {
yVals1.add(new CandleEntry(53, high, low, open, close));
pos++;
} else if (pos == 4) {
yVals1.add(new CandleEntry(54, high, low, open, close));
pos++;
}
mChart.invalidate();
}
#OnClick({R.id.btn_buy, R.id.btn_sell})
public void onGameButtonsClicked() {
final FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.addToBackStack(null);
String cashText = btnCashCurrency.getText().toString();
final int[] cash = {Integer.valueOf(cashText)};
if (cash[0] <= 0) {
restartGame.setVisibility(View.VISIBLE);
Toast.makeText(getContext(), "Your cash is on -, u cant play. Please restart game.", Toast.LENGTH_SHORT).show();
} else if (Integer.valueOf(spinnerData.getSelectedItem().toString()) >= Integer.valueOf(btnCashCurrency.getText().toString())) {
Toast.makeText(getContext(), "You not have available cash, change the money in Invest Section.", Toast.LENGTH_SHORT).show();
} else {
int min = 0;
int max = 2;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
String text = spinnerData.getSelectedItem().toString();
final int temp = Integer.parseInt(text);
final int temp2 = temp * 2;
disableAndEnableButtons(false);
if (i1 == 0 || i1 == 1) {
progress.setVisibility(View.VISIBLE);
stop();
configureHandler(900);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
lostFragment.show(ft, DIALOG_LOST);
cash[0] -= temp2;
btnCashCurrency.setText(cash[0] + "");
progress.setVisibility(View.GONE);
disableAndEnableButtons(true);
String score = "LOSE " + MainActivity.getUsername(getContext()).trim() + ": "
+ btnCashCurrency.getText().toString().trim() + " -" + temp2;
HISTORYTRANSACTION.add(score);
getUserInfoAndSave();
stop();
configureHandler(5000);
}
}, 5000);
} else {
progress.setVisibility(View.VISIBLE);
stop();
configureHandler(900);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
winFragment.show(ft, DIALOG_WIN);
cash[0] += temp2;
btnCashCurrency.setText(cash[0] + "");
progress.setVisibility(View.GONE);
disableAndEnableButtons(true);
String score = "WIN " + MainActivity.getUsername(getContext()).trim()
+ ": " + btnCashCurrency.getText().toString().trim() + " +" + temp2;
HISTORYTRANSACTION.add(score);
getUserInfoAndSave();
stop();
configureHandler(5000);
}
}, 5000);
}
}
}
private void disableAndEnableButtons(boolean on) {
btnBuy.setEnabled(on);
btnSell.setEnabled(on);
}
#OnClick(R.id.restart_game)
public void restartGame() {
Intent intent = new Intent(getContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
MainGameFragment.LEADERBOARDUSER.add(MainGameFragment.userNameAndScore);
((CurrencySelectActivity) getContext()).hideGameFragment();
((CurrencySelectActivity) getContext()).closeCurrencyActivity();
SharedPreferences prefs = getContext().getSharedPreferences("app.forex", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
Set<String> set = new HashSet<>();
set.add(String.valueOf(MainGameFragment.LEADERBOARDUSER));
edit.putStringSet("user_and_score", set);
HISTORYTRANSACTION.clear();
edit.apply();
}
private void configureSpinnerDataAndLogic() {
String[] arraySpinner = new String[]{
"50", "100", "150", "200", "250", "300", "400", "500"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(),
android.R.layout.simple_list_item_1, arraySpinner);
spinnerData.setAdapter(adapter);
spinnerData.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);
String text = spinnerData.getSelectedItem().toString();
int temp = Integer.parseInt(text);
text_profit.setText((temp * 2) + " $ " + "100%");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
AAnd here is xml files
<com.github.mikephil.charting.charts.CandleStickChart
android:id="#+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/butonCurrency"
android:layout_margin="30dp" />
<com.github.mikephil.charting.charts.LineChart
android:id="#+id/chartLine"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/butonCurrency"
android:layout_margin="30dp" />
And thiswork like:
Highly help needed
Instead of adding the new entry to your entries array.. you could try using the method lineData.addEntry().
Something like this :
private void addEntry() {
LineData data = mChart.getData();
ILineDataSet set = data.getDataSetByIndex(0);
if (set == null) {
set = createSet();
data.addDataSet(set);
}
data.addEntry(new Entry(data.getDataSetByIndex(dataSetIndex).getEntryCount(), yValue), dataSetIndex);
data.notifyDataChanged();
mChart.notifyDataSetChanged();
}
your "dataSetIndex" will always be 0 if you only have a single dataset in your linechart.
For more information, you can check this class
Problem is here, after i change, everything work good
private void addLineEntry() {
lineData = lineChart.getData();
if (lineDataSet == null) {
lineDataSet = createLineSet();
lineData.addDataSet(lineDataSet);
}
int prog = 1;
int xMax = (int) lineDataSet.getXMax();
for (int i = 0; i < prog; i++) {
CandleEntry lastEntry = set1.getEntryForIndex(yVals1.size() - 1);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
entries.add(new Entry(xMax + 1, lastOpenCloseMax));
}
lineDataSet.notifyDataSetChanged();
lineChart.notifyDataSetChanged();
lineChart.invalidate();
lineChart.moveViewTo(xMax, 2f, YAxis.AxisDependency.RIGHT);
}
I'm using Encog and I ran the ocr sample. It works fine. However, I want to pass an image file (png, jpg,...) as a parameter. This image contains the text to to be recognized. Then, the system should return a string with the "same" text.
Has someone already did something similar? How should I start?
Thanks!
Step 1: In GUI create file input and take file from user
JFileChooser fc;
JButton b, b1;
JTextField tf;
FileInputStream in;
Socket s;
DataOutputStream dout;
DataInputStream din;
int i;
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == b) {
int x = fc.showOpenDialog(null);
if (x == JFileChooser.APPROVE_OPTION) {
fileToBeSent = fc.getSelectedFile();
tf.setText(f1.getAbsolutePath());
b1.setEnabled(true);
} else {
fileToBeSent = null;
tf.setText(null;);
b1.setEnabled(false);
}
}
if (e.getSource() == b1) {
send();
}
} catch (Exception ex) {
}
}
public void copy() throws IOException {
File f1 = fc.getSelectedFile();
tf.setText(f1.getAbsolutePath());
in = new FileInputStream(f1.getAbsolutePath());
while ((i = in.read()) != -1) {
System.out.print(i);
}
}
public void send() throws IOException {
dout.write(i);
dout.flush();
}
Step 2: Down sample it
private void processNetwork() throws IOException {
System.out.println("Downsampling images...");
for (final ImagePair pair : this.imageList) {
final MLData ideal = new BasicMLData(this.outputCount);
final int idx = pair.getIdentity();
for (int i = 0; i < this.outputCount; i++) {
if (i == idx) {
ideal.setData(i, 1);
} else {
ideal.setData(i, -1);
}
}
final Image img = ImageIO.read(fc.getFile());
final ImageMLData data = new ImageMLData(img);
this.training.add(data, ideal);
}
final String strHidden1 = getArg("hidden1");
final String strHidden2 = getArg("hidden2");
this.training.downsample(this.downsampleHeight, this.downsampleWidth);
final int hidden1 = Integer.parseInt(strHidden1);
final int hidden2 = Integer.parseInt(strHidden2);
this.network = EncogUtility.simpleFeedForward(this.training
.getInputSize(), hidden1, hidden2,
this.training.getIdealSize(), true);
System.out.println("Created network: " + this.network.toString());
}
Step 3: Begin Training with training set
private void processTrain() throws IOException {
final String strMode = getArg("mode");
final String strMinutes = getArg("minutes");
final String strStrategyError = getArg("strategyerror");
final String strStrategyCycles = getArg("strategycycles");
System.out.println("Training Beginning... Output patterns="
+ this.outputCount);
final double strategyError = Double.parseDouble(strStrategyError);
final int strategyCycles = Integer.parseInt(strStrategyCycles);
final ResilientPropagation train = new ResilientPropagation(this.network, this.training);
train.addStrategy(new ResetStrategy(strategyError, strategyCycles));
if (strMode.equalsIgnoreCase("gui")) {
TrainingDialog.trainDialog(train, this.network, this.training);
} else {
final int minutes = Integer.parseInt(strMinutes);
EncogUtility.trainConsole(train, this.network, this.training,
minutes);
}
System.out.println("Training Stopped...");
}
Step 4: Give down sampled file to neural network
public void processWhatIs() throws IOException {
final String filename = getArg("image");
final File file = new File(filename);
final Image img = ImageIO.read(file);
final ImageMLData input = new ImageMLData(img);
input.downsample(this.downsample, false, this.downsampleHeight,
this.downsampleWidth, 1, -1);
final int winner = this.network.winner(input);
System.out.println("What is: " + filename + ", it seems to be: "
+ this.neuron2identity.get(winner));
}
Step 5: Check Result
I have JFrame with a start button, which triggers the calculation of a Julia Set.
The code that is executed when the start button is clicked is as follows:
public void actionPerformed(ActionEvent aActionEvent)
{
String strCmd = aActionEvent.getActionCommand();
if (strCmd.equals("Start"))
{
m_cCanvas.init();
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_bRunning = true;
this.handleCalculation();
}
else if (aActionEvent.getSource() == m_cTReal)
Which used to work fine, except that the application could not be closed anymore. So I tried to use m_bRunning in a separate method so that actionPerformed() isn't blocked all the time to see if that would help, and then set m_bRunning = false in the method stop() which is called when the window is closed:
public void run()
{
if(m_bRunning)
{
this.handleCalculation();
}
}
The method run() is called from the main class in a while(true) loop.
Yet unfortunately, neither did that solve the problem, nor do I now have any output to the canvas or any debug traces with System.out.println(). Could anyone point me in the right direction on this?
EDIT:
Here are the whole files:
// cMain.java
package juliaSet;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
public class cMain {
public static void main(String[] args)
{
int windowWidth = 1000;//(int)screenSize.getWidth() - 200;
int windowHeight = 800;//(int)screenSize.getHeight() - 50;
int plotWidth = 400;//(int)screenSize.getWidth() - 600;
int plotHeight = 400;//(int)screenSize.getHeight() - 150;
JuliaSet cJuliaSet = new JuliaSet("Julia Set", windowWidth, windowHeight, plotWidth, plotHeight);
cJuliaSet.setVisible(true);
while(true)
{
cJuliaSet.run();
}
}
}
// JuliaSet.java
package juliaSet;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.Random;
import java.io.*;
import java.lang.ref.*;
public class JuliaSet extends JFrame implements ActionListener
{
private JButton m_cBStart;
private JTextField m_cTReal;
private JTextField m_cTImag;
private JTextField m_cTDivergThresh;
private JLabel m_cLReal;
private JLabel m_cLImag;
private JLabel m_cLDivergThresh;
private int m_iDivergThresh = 10;
private String m_cMsgDivThresh = "Divergence threshold = " + m_iDivergThresh;
private JuliaCanvas m_cCanvas;
private int m_iPlotWidth; // number of cells
private int m_iPlotHeight; // number of cells
private Boolean m_bRunning = false;
private double m_dReal = 0.3;
private double m_dImag = -0.5;
private String m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
private String m_cMsgIter = "x = 0, y = 0";
private Complex m_cCoordPlane[][];
private double m_dAbsSqValues[][];
private int m_iIterations[][];
private Complex m_cSummand;
private BufferedImage m_cBackGroundImage = null;
private FileWriter m_cFileWriter;
private BufferedWriter m_cBufferedWriter;
private String m_sFileName = "log.txt";
private Boolean m_bWriteLog = false;
private static final double PLOTMAX = 2.0; // we'll have symmetric axes
// ((0,0) at the centre of the
// plot
private static final int MAXITER = 0xff;
JuliaSet(String aTitle, int aFrameWidth, int aFrameHeight, int aPlotWidth, int aPlotHeight)
{
super(aTitle);
this.setSize(aFrameWidth, aFrameHeight);
m_iPlotWidth = aPlotWidth;
m_iPlotHeight = aPlotHeight;
m_cSummand = new Complex(m_dReal, m_dImag);
m_cBackGroundImage = new BufferedImage(aFrameWidth, aFrameHeight, BufferedImage.TYPE_INT_RGB);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
stop();
super.windowClosing(e);
System.exit(0);
}
});
GridBagLayout cLayout = new GridBagLayout();
GridBagConstraints cConstraints = new GridBagConstraints();
this.setLayout(cLayout);
m_cCanvas = new JuliaCanvas(m_iPlotWidth, m_iPlotHeight);
m_cCanvas.setSize(m_iPlotWidth, m_iPlotHeight);
m_cBStart = new JButton("Start");
m_cBStart.addActionListener(this);
m_cTReal = new JTextField(5);
m_cTReal.addActionListener(this);
m_cTImag = new JTextField(5);
m_cTImag.addActionListener(this);
m_cTDivergThresh = new JTextField(5);
m_cTDivergThresh.addActionListener(this);
m_cLReal = new JLabel("Re(c):");
m_cLImag = new JLabel("Im(c):");
m_cLDivergThresh = new JLabel("Divergence Threshold:");
cConstraints.insets.top = 3;
cConstraints.insets.bottom = 3;
cConstraints.insets.right = 3;
cConstraints.insets.left = 3;
// cCanvas
cConstraints.gridx = 0;
cConstraints.gridy = 0;
cLayout.setConstraints(m_cCanvas, cConstraints);
this.add(m_cCanvas);
// m_cLReal
cConstraints.gridx = 0;
cConstraints.gridy = 1;
cLayout.setConstraints(m_cLReal, cConstraints);
this.add(m_cLReal);
// m_cTReal
cConstraints.gridx = 1;
cConstraints.gridy = 1;
cLayout.setConstraints(m_cTReal, cConstraints);
this.add(m_cTReal);
// m_cLImag
cConstraints.gridx = 0;
cConstraints.gridy = 2;
cLayout.setConstraints(m_cLImag, cConstraints);
this.add(m_cLImag);
// m_cTImag
cConstraints.gridx = 1;
cConstraints.gridy = 2;
cLayout.setConstraints(m_cTImag, cConstraints);
this.add(m_cTImag);
// m_cLDivergThresh
cConstraints.gridx = 0;
cConstraints.gridy = 3;
cLayout.setConstraints(m_cLDivergThresh, cConstraints);
this.add(m_cLDivergThresh);
// m_cTDivergThresh
cConstraints.gridx = 1;
cConstraints.gridy = 3;
cLayout.setConstraints(m_cTDivergThresh, cConstraints);
this.add(m_cTDivergThresh);
// m_cBStart
cConstraints.gridx = 0;
cConstraints.gridy = 4;
cLayout.setConstraints(m_cBStart, cConstraints);
this.add(m_cBStart);
if (m_bWriteLog)
{
try
{
m_cFileWriter = new FileWriter(m_sFileName, false);
m_cBufferedWriter = new BufferedWriter(m_cFileWriter);
} catch (IOException ex) {
System.out.println("Error opening file '" + m_sFileName + "'");
}
}
this.repaint();
this.transformCoordinates();
}
public synchronized void stop()
{
if (m_bRunning)
{
m_bRunning = false;
boolean bRetry = true;
}
if (m_bWriteLog)
{
try {
m_cBufferedWriter.close();
m_cFileWriter.close();
} catch (IOException ex) {
System.out.println("Error closing file '" + m_sFileName + "'");
}
}
}
public void collectGarbage()
{
Object cObj = new Object();
WeakReference ref = new WeakReference<Object>(cObj);
cObj = null;
while(ref.get() != null) {
System.gc();
}
}
public void setSummand(Complex aSummand)
{
m_cSummand.setIm(aSummand.getIm());
m_dImag = aSummand.getIm();
m_cSummand.setRe(aSummand.getRe());
m_dReal = aSummand.getRe();
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
}
public void paint(Graphics aGraphics)
{
Graphics cScreenGraphics = aGraphics;
// render on background image
aGraphics = m_cBackGroundImage.getGraphics();
this.paintComponents(aGraphics);
// drawString() calls are debug code only....
aGraphics.setColor(Color.BLACK);
aGraphics.drawString(m_cSMsg, 10, 450);
aGraphics.drawString(m_cMsgIter, 10, 465);
aGraphics.drawString(m_cMsgDivThresh, 10, 480);
// rendering is done, draw background image to on screen graphics
cScreenGraphics.drawImage(m_cBackGroundImage, 0, 0, null);
}
public void actionPerformed(ActionEvent aActionEvent)
{
String strCmd = aActionEvent.getActionCommand();
if (strCmd.equals("Start"))
{
m_cCanvas.init();
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_bRunning = true;
}
else if (aActionEvent.getSource() == m_cTReal)
{
m_dReal = Double.parseDouble(m_cTReal.getText());
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_cSummand.setRe(m_dReal);
}
else if (aActionEvent.getSource() == m_cTImag)
{
m_dImag = Double.parseDouble(m_cTImag.getText());
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_cSummand.setIm(m_dImag);
}
else if (aActionEvent.getSource() == m_cTDivergThresh)
{
m_iDivergThresh = Integer.parseInt(m_cTDivergThresh.getText());
m_cMsgDivThresh = "Divergence threshold = " + m_iDivergThresh;
}
this.update(this.getGraphics());
}
public void transformCoordinates()
{
double dCanvasHeight = (double) m_cCanvas.getHeight();
double dCanvasWidth = (double) m_cCanvas.getWidth();
// init matrix with same amount of elements as pixels in canvas
m_cCoordPlane = new Complex[(int) dCanvasHeight][(int) dCanvasWidth];
double iPlotRange = 2 * PLOTMAX;
for (int i = 0; i < dCanvasHeight; i++)
{
for (int j = 0; j < dCanvasWidth; j++)
{
m_cCoordPlane[i][j] = new Complex((i - (dCanvasWidth / 2)) * iPlotRange / dCanvasWidth,
(j - (dCanvasHeight / 2)) * iPlotRange / dCanvasHeight);
}
}
}
public void calcAbsSqValues()
{
int iCanvasHeight = m_cCanvas.getHeight();
int iCanvasWidth = m_cCanvas.getWidth();
// init matrix with same amount of elements as pixels in canvas
m_dAbsSqValues = new double[iCanvasHeight][iCanvasWidth];
m_iIterations = new int[iCanvasHeight][iCanvasWidth];
Complex cSum = new Complex();
if (m_bWriteLog) {
try
{
m_cBufferedWriter.write("m_iIterations[][] =");
m_cBufferedWriter.newLine();
}
catch (IOException ex)
{
System.out.println("Error opening file '" + m_sFileName + "'");
}
}
for (int i = 0; i < iCanvasHeight; i++)
{
for (int j = 0; j < iCanvasWidth; j++)
{
cSum.setRe(m_cCoordPlane[i][j].getRe());
cSum.setIm(m_cCoordPlane[i][j].getIm());
m_iIterations[i][j] = 0;
do
{
m_iIterations[i][j]++;
cSum.square();
cSum.add(m_cSummand);
m_dAbsSqValues[i][j] = cSum.getAbsSq();
} while ((m_iIterations[i][j] < MAXITER) && (m_dAbsSqValues[i][j] < m_iDivergThresh));
this.calcColour(i, j, m_iIterations[i][j]);
m_cMsgIter = "x = " + i + " , y = " + j;
if(m_bWriteLog)
{
System.out.println(m_cMsgIter);
System.out.flush();
}
if (m_bWriteLog) {
try
{
m_cBufferedWriter.write(Integer.toString(m_iIterations[i][j]));
m_cBufferedWriter.write(" ");
}
catch (IOException ex) {
System.out.println("Error writing to file '" + m_sFileName + "'");
}
}
}
if (m_bWriteLog) {
try
{
m_cBufferedWriter.newLine();
}
catch (IOException ex) {
System.out.println("Error writing to file '" + m_sFileName + "'");
}
}
}
m_dAbsSqValues = null;
m_iIterations = null;
cSum = null;
}
private void calcColour(int i, int j, int aIterations)
{
Color cColour = Color.getHSBColor((int) Math.pow(aIterations, 4), 0xff,
0xff * ((aIterations < MAXITER) ? 1 : 0));
m_cCanvas.setPixelColour(i, j, cColour);
cColour = null;
}
private void handleCalculation()
{
Complex cSummand = new Complex();
for(int i = -800; i <= 800; i++)
{
for(int j = -800; j <= 800; j++)
{
cSummand.setRe(((double)i)/1000.0);
cSummand.setIm(((double)j)/1000.0);
this.setSummand(cSummand);
this.calcAbsSqValues();
this.getCanvas().paint(m_cCanvas.getGraphics());
this.paint(this.getGraphics());
}
}
cSummand = null;
this.collectGarbage();
System.gc();
System.runFinalization();
}
public boolean isRunning()
{
return m_bRunning;
}
public void setRunning(boolean aRunning)
{
m_bRunning = aRunning;
}
public Canvas getCanvas()
{
return m_cCanvas;
}
public void run()
{
if(m_bRunning)
{
this.handleCalculation();
}
}
}
class JuliaCanvas extends Canvas
{
private int m_iWidth;
private int m_iHeight;
private Random m_cRnd;
private BufferedImage m_cBackGroundImage = null;
private int m_iRed[][];
private int m_iGreen[][];
private int m_iBlue[][];
JuliaCanvas(int aWidth, int aHeight)
{
m_iWidth = aWidth;
m_iHeight = aHeight;
m_cRnd = new Random();
m_cRnd.setSeed(m_cRnd.nextLong());
m_cBackGroundImage = new BufferedImage(m_iWidth, m_iHeight, BufferedImage.TYPE_INT_RGB);
m_iRed = new int[m_iHeight][m_iWidth];
m_iGreen = new int[m_iHeight][m_iWidth];
m_iBlue = new int[m_iHeight][m_iWidth];
}
public void init() {
}
public void setPixelColour(int i, int j, Color aColour)
{
m_iRed[i][j] = aColour.getRed();
m_iGreen[i][j] = aColour.getGreen();
m_iBlue[i][j] = aColour.getBlue();
}
private int getRandomInt(double aProbability)
{
return (m_cRnd.nextDouble() < aProbability) ? 1 : 0;
}
#Override
public void paint(Graphics aGraphics)
{
// store on screen graphics
Graphics cScreenGraphics = aGraphics;
// render on background image
aGraphics = m_cBackGroundImage.getGraphics();
for (int i = 0; i < m_iWidth; i++)
{
for (int j = 0; j < m_iHeight; j++)
{
Color cColor = new Color(m_iRed[i][j], m_iGreen[i][j], m_iBlue[i][j]);
aGraphics.setColor(cColor);
aGraphics.drawRect(i, j, 0, 0);
cColor = null;
}
}
// rendering is done, draw background image to on screen graphics
cScreenGraphics.drawImage(m_cBackGroundImage, 1, 1, null);
}
#Override
public void update(Graphics aGraphics)
{
paint(aGraphics);
}
}
class Complex {
private double m_dRe;
private double m_dIm;
public Complex()
{
m_dRe = 0;
m_dIm = 0;
}
public Complex(double aRe, double aIm)
{
m_dRe = aRe;
m_dIm = aIm;
}
public Complex(Complex aComplex)
{
m_dRe = aComplex.m_dRe;
m_dIm = aComplex.m_dIm;
}
public double getRe() {
return m_dRe;
}
public void setRe(double adRe)
{
m_dRe = adRe;
}
public double getIm() {
return m_dIm;
}
public void setIm(double adIm)
{
m_dIm = adIm;
}
public void add(Complex acComplex)
{
m_dRe += acComplex.getRe();
m_dIm += acComplex.getIm();
}
public void square()
{
double m_dReSave = m_dRe;
m_dRe = (m_dRe * m_dRe) - (m_dIm * m_dIm);
m_dIm = 2 * m_dReSave * m_dIm;
}
public double getAbsSq()
{
return ((m_dRe * m_dRe) + (m_dIm * m_dIm));
}
}
I'm quoting a recent comment from #MadProgrammer (including links)
"Swing is single threaded, nothing you can do to change that, all events are posted to the event queue and processed by the Event Dispatching Thread, see Concurrency in Swing for more details and have a look at Worker Threads and SwingWorker for at least one possible solution"
There is only one thread in your code. That thread is busy doing the calculation and can not respond to events located in the GUI. You have to separate the calculation in another thread that periodically updates the quantities that appears in the window. More info about that in the links, courtesy of #MadProgrammer, I insist.
UPDATED: As pointed by #Yusuf, the proper way of launching the JFrame is
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new JuliaSet("Julia Set", windowWidth, windowHeight, plotWidth, plotHeight);
}
});
Set the frame visible on construction and start calculation when the start button is pressed.
First;
Endless loop is not a proper way to do this. This part is loops and taking CPU and never give canvas to refresh screen. if you add below code your code will run as expected. but this is not the proper solution.
cMain.java:
while (true) {
cJuliaSet.run();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Second: you could call run method when start button clicked. But you should create a thread in run method to not freeze screen.
public static void main(String[] args) {
int windowWidth = 1000;// (int)screenSize.getWidth() - 200;
int windowHeight = 800;// (int)screenSize.getHeight() - 50;
int plotWidth = 400;// (int)screenSize.getWidth() - 600;
int plotHeight = 400;// (int)screenSize.getHeight() - 150;
JuliaSet cJuliaSet = new JuliaSet("Julia Set", windowWidth, windowHeight, plotWidth, plotHeight);
cJuliaSet.setVisible(true);
//While loop removed
}
actionPerformed:
if (strCmd.equals("Start")) {
m_cCanvas.init();
m_cSMsg = "c = " + Double.toString(m_dReal) + " + " + "j*" + Double.toString(m_dImag);
m_bRunning = true;
this.run(); // added call run method.
} else if (aActionEvent.getSource() == m_cTReal) {
run method:
public void run()
{
if(m_bRunning)
{
new Thread(){ //Thread to release screen
#Override
public void run() {
JuliaSet.this.handleCalculation();
}
}.start(); //also thread must be started
}
}
As said by #RubioRic, SwingUtilities.invokeLater method is also a part of solution. But you need to check whole of your code and you should learn about Threads.