Beispiel #1
0
        public override async Task <Response> Load(VisionMessage request, ServerCallContext context)
        {
            _logger.LogDebug("Load message {}", request.Meta);
            await SaveToStorage(request);

            var response = new Response()
            {
                Success = true
            };

            return(response);
        }
Beispiel #2
0
        private async Task SaveToStorage(VisionMessage request)
        {
            var meta = JsonSerializer.Deserialize <Dictionary <string, string> >(request.Meta);
            var obj  = new Google.Apis.Storage.v1.Data.Object()
            {
                Bucket      = _options.BucketName,
                Name        = Guid.NewGuid().ToString(),
                ContentType = "image/jpeg",
                Metadata    = meta
            };

            _logger.LogDebug("Save object {} to {}", obj.Name, _options.BucketName);
            await _storage.UploadObjectAsync(
                obj,
                new MemoryStream(request.Data.ToByteArray())
                );

            // await _storage.UploadObjectAsync(
            // _options.BucketName, Guid.NewGuid().ToString(), "image/jpeg",
            // new MemoryStream(request.Data.ToByteArray()));
        }
Beispiel #3
0
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                Globals.HARVEST_SIGN_ID = Convert.ToInt32(textBox1.Text);
                int com_x_sum = 0, com_y_sum = 0, com_x_y_point_count = 0;

                if (false)
                {
                    /*
                    Bitmap bmp_3 = (Bitmap)Bitmap.FromFile(Constants.base_folder + "hasat\\" + textBox1.Text + "_3.bmp");
                    Bitmap bmp_4 = AutoBrightnessProcessor.autoBrightness(bmp_3, Rectangle.Empty, Constants.AUTOBRIGHTNESS_BASE_LUM);
                    int dynamic_gray = 0;
                    Bitmap bmp_4x = new ColorLabelFilter(new Color[] { Color.White, Color.FromArgb(dynamic_gray, dynamic_gray, dynamic_gray) }, true).Apply(bmp_4);
                    bmp_4x.Save(Constants.base_folder + "hasat\\zzz\\bmp_4a.bmp");

                    int y = 0;
                    if ("NN_SURF".Equals(Constants.NN_SVM_SURF))
                    {
                        int start_x = 0, start_y = 0;
                        for (; y < Constants.SIGN_HEIGHT && (start_x + start_y) == 0; y++)
                        {
                            for (int x = 0; x < Constants.SIGN_WIDTH; x++)
                            {
                                Color clr = bmp_4x.GetPixel(x, y);
                                if (clr.R == 0)
                                {
                                    start_x = x;
                                    start_y = y;
                                    break;
                                }
                            }
                        }
                        PointedColorFloodFill filter = new PointedColorFloodFill();
                        filter.Tolerance = Color.FromArgb(0, 0, 0);
                        filter.FillColor = Color.FromArgb(255, 255, 255);
                        filter.StartingPoint = new Point(start_x, start_y);
                        Bitmap rgb_flood_fill = filter.Apply(bmp_4x);

                        bool black_region_found_at_center = false;
                        y = 4 * (Constants.SIGN_HEIGHT / 10);
                        for (int x = 4 * (Constants.SIGN_WIDTH / 10); x < 6 * (Constants.SIGN_WIDTH / 10); x += 2)
                        {
                            Color clr = rgb_flood_fill.GetPixel(x, y);
                            if (clr.R == 0)
                            {
                                black_region_found_at_center = true;
                            }
                        }
                        if (!black_region_found_at_center)
                        {
                            y = 6 * (Constants.SIGN_HEIGHT / 10);
                            for (int x = 4 * (Constants.SIGN_WIDTH / 10); x < 6 * (Constants.SIGN_WIDTH / 10); x += 2)
                            {
                                Color clr = rgb_flood_fill.GetPixel(x, y);
                                if (clr.R == 0)
                                {
                                    black_region_found_at_center = true;
                                    break;
                                }
                            }
                        }
                        if (black_region_found_at_center)
                        {
                            bmp_4x = rgb_flood_fill;
                        }
                        bmp_4x.Save(Constants.base_folder + "hasat\\zzz\\bmp_4x.bmp");
                    }

                    System.Drawing.Imaging.BitmapData image_data = bmp_4x.LockBits(new Rectangle(0, 0, bmp_4x.Width, bmp_4x.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp_4x.PixelFormat);
                    int bpp = 3;
                    int nOffset = image_data.Stride - bmp_4x.Width * bpp;
                    System.IntPtr Scan0 = image_data.Scan0;
                    unsafe
                    {
                        byte* p = (byte*)Scan0;
                        for (y = 0; y < Constants.SIGN_HEIGHT; y++)
                        {
                            // for each pixel
                            for (int x = 0; x < Constants.SIGN_WIDTH; x++, p += bpp)
                            {
                                if (y >= Constants.SIGN_HEIGHT - 14 || y <= 14 || x >= Constants.SIGN_WIDTH - 12 || x <= 12
                                    ||
                                         (x <= Constants.SIGN_WIDTH / 2 && y <= (Constants.SIGN_HEIGHT) - 2 * x + 8)
                                    || (x > Constants.SIGN_WIDTH / 2 && y <= 2 * (x - Constants.SIGN_HEIGHT / 2) + 8)

                                    )
                                {
                                    RGB pixel = new RGB(p[RGB.R], p[RGB.G], p[RGB.B]);
                                    p[RGB.R] = 255;
                                    p[RGB.G] = 255;
                                    p[RGB.B] = 255;
                                }
                                else if (p[RGB.R] == 0)
                                {
                                    com_x_sum += x;
                                    com_y_sum += y;
                                    com_x_y_point_count++;
                                }
                            }
                            p += nOffset;
                        }

                    }
                    bmp_4x.UnlockBits(image_data);
                    */
                }

                Bitmap bmp_6 = (Bitmap)Bitmap.FromFile(Constants.base_folder + "hasat\\" + textBox1.Text + "_6.bmp");
                bmp_6.Save(Constants.base_folder + "hasat\\zzz\\" + textBox1.Text + "_6.bmp");

                System.Drawing.Imaging.BitmapData image_data = bmp_6.LockBits(new Rectangle(0, 0, bmp_6.Width, bmp_6.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp_6.PixelFormat);
                int bpp = 3;
                int nOffset = image_data.Stride - bmp_6.Width * bpp;
                System.IntPtr Scan0 = image_data.Scan0;
                unsafe
                {
                    byte* p = (byte*)Scan0;
                    for (int y = 0; y < Constants.SIGN_HEIGHT; y++)
                    {
                        for (int x = 0; x < Constants.SIGN_WIDTH; x++, p += bpp)
                        {
                            if (p[RGB.R] == 0)
                            {
                                com_x_sum += x;
                                com_y_sum += y;
                                com_x_y_point_count++;
                            }
                        }
                        p += nOffset;
                    }
                }
                bmp_6.UnlockBits(image_data);

                // Center of Mass
                int com_x = com_x_sum / (com_x_y_point_count == 0 ? 1 : com_x_y_point_count);
                int com_y = com_y_sum / (com_x_y_point_count == 0 ? 1 : com_x_y_point_count);

                VisionMessage vm = new VisionMessage(bmp_6.Height, bmp_6.Width, BOUNLib.ToolBox.ByteTools.pixelFormatToBPP(bmp_6.PixelFormat), bmp_6);
                vm.CoM_X = com_x;
                vm.CoM_Y = com_y;
                if ("NN_SURF".Equals(Constants.NN_SVM_SURF))
                {
                    NN_SURFProcessor SURF = new NN_SURFProcessor(null, Constants.SignType.triangular);
                    SURF.consumeMessage(vm, 111);
                }
                else if ("SVM_SURF".Equals(Constants.NN_SVM_SURF))
                {
                    SVM_SURFProcessor SURF = new SVM_SURFProcessor(null, Constants.SignType.triangular);
                    SURF.consumeMessage(vm, 111);
                }
            }
        }
Beispiel #4
0
            public override void consumeMessage(IMessage message, int msgID)
            {
                VisionMessage vm = message as VisionMessage;
                if (vm.bypass)
                {
                    vm.bypass = false;
                    msgService.sendMsg(vm);
                    return;
                }

                // Bitmap bitmap = ByteTools.BytesToBmp(vm.Bitmap, vm.BmpWidth, vm.BmpHeight, ByteTools.bppToPixelFormat(vm.BytePerPixel));
                Bitmap bitmap = vm.Bitmap;
                Bitmap rgb = bitmap;

                bitmap = lf.Apply(bitmap);
                rgb = new GrayscaleToRGB().Apply(bitmap);

                Graphics g = Graphics.FromImage(rgb);
                if (g == null)
                {
                    Console.WriteLine("Null Graphics!");
                    return;
                }

                lock (this)
                {
                    if (!threadRunning)
                    {
                        threadRunning = true;
                        /*for (int i = 0; i < 3; i++)
                        {
                            if (g != null && currentStateProbs[i] > double.MinValue)
                            {
                                drawLine(g, new HoughLine((double)currentThetaStates[i], (short)currentRStates[i], 0, 0d),
                                   //new Rectangle(0, heightRes[0], bitmap.Width, heightRes[heightRes.Length - 1]-heightRes[0])
                                   new Rectangle(0,0,bitmap.Width, bitmap.Height*2));
                            }
                        }
                        */
                        for (int i = 0; i < hlidx; i++)
                        {
                            drawLine(g, hls[i], new Rectangle(0, 0, bitmap.Width, bitmap.Height * 2));
                        }

                        for (int i = 0; i < heightRes.Length - 1; i++)
                        {
                            int y = heightRes[i];
                            int yDelta = heightRes[i + 1] - heightRes[i];
                            int xDelta = widthRes[i + 1] - widthRes[i];

                            for (int x = 0; x < bitmap.Width; x += xDelta)
                            {
                                Rectangle sub = new Rectangle(x, y, xDelta, yDelta);
                                Bitmap subImg = (Bitmap)bitmap.Clone(sub, bitmap.PixelFormat);
                                HoughLine hl = processSubImage(subImg);
                                g.DrawRectangle(p2, sub);
                            }
                        }
                        ThreadStart starter = delegate { findLines(bitmap); };
                        Thread innerThread = new Thread(starter);
                        innerThread.Start();
                    }
                }
                vm = new VisionMessage(rgb.Height, rgb.Width, ByteTools.pixelFormatToBPP(rgb.PixelFormat), rgb);
                msgService.sendMsg(vm);
            }
Beispiel #5
0
            void searchSolution(Bitmap rgb)
            {
                long start = DateTime.Now.Ticks / 10000;
                Globals.FRAMES_PROCESSED_CIRCULAR++;
                int com_x_sum = 0, com_y_sum = 0, com_x_y_point_count = 0;
                Globals.HARVEST_SIGN_ID++;
                Bitmap bmp = sf.Apply(rgb);

                //if (redTestPoints == null)
                //    calculateRedTestPoints();

                // GeoTransChromosome sampleChromosome = new GeoTransChromosome(bmp, sf, redTestPoints, nonRedTestPoints);
                GeoTransChromosome sampleChromosome = new GeoTransChromosome(bmp, sf, null, this);

                if (population == null || population.BestChromosome == null || population.BestChromosome.Fitness < RS_THRESHOLD - 10)
                {   // fresh population
                    population = new Population(Constants.GA_POPULATION_SIZE,
                        sampleChromosome,
                        sampleChromosome,
                        new EliteSelection()
                        );
                    population.MutationRate = Constants.GA_MUTATION_RATE;
                    population.CrossoverRate = Constants.GA_CROSSOVER_RATE;
                }
                else
                {
                    // half from previous
                    Population tmpPopulation = new Population(Constants.GA_POPULATION_SIZE,
                        sampleChromosome,
                        sampleChromosome,
                        new EliteSelection()
                        );
                    tmpPopulation.MutationRate = Constants.GA_MUTATION_RATE;
                    tmpPopulation.CrossoverRate = Constants.GA_CROSSOVER_RATE;
                    for (int j = 0; j < tmpPopulation.Size / 2; j++)
                    {
                        ((GeoTransChromosome)tmpPopulation[j]).copyContent((GeoTransChromosome)population.BestChromosome);
                    }
                    population = tmpPopulation;
                }

                int i = 0;
                do
                {
                    long start_epoch = DateTime.Now.Ticks / 10000;
                    Globals.FRAMES_PROCESSED_GA_RUNEPOCH++;

                    // run one epoch of genetic algorithm
                    population.RunEpoch();
                    i++;

                    if (Constants.EVALUATE_TIME_ENABLED)
                    {
                        int x = (int)(DateTime.Now.Ticks / 10000 - start_epoch);
                        if (x >= 0)
                        {
                            Globals.TIME_GA_RUNEPOCH_MIN = x < Globals.TIME_GA_RUNEPOCH_MIN ? x : Globals.TIME_GA_RUNEPOCH_MIN;
                            Globals.TIME_GA_RUNEPOCH_MAX = x > Globals.TIME_GA_RUNEPOCH_MAX ? x : Globals.TIME_GA_RUNEPOCH_MAX;
                            Globals.TIME_GA_RUNEPOCH_TOTAL += x;
                        }
                    }
                } while (i < Constants.GA_NUMBER_ITERATIONS);

                GeoTransChromosome bestChromo = null;
                if (population.BestChromosome != null && population.BestChromosome.Fitness > RS_THRESHOLD)
                {
                    bestChromo = (GeoTransChromosome)population.BestChromosome;
                    // int rx = 0, ry = 0, rr = 0;
                    // float rs = 0f;

                    int rx = bestChromo.X, ry = bestChromo.Y, rr = bestChromo.Width;
                    float rs = (float)bestChromo.Fitness;

                    // findCircle(bestChromo.X, bestChromo.Y, bmp, ref rx, ref ry, ref rr, ref rs);
                    if (rs < RS_THRESHOLD || rr==0)
                    {
                        bestChromo = null;
                    }
                    else
                    {
                        rr = (int)(Constants.CAPTURE_RESIZE_CIRCLE * rr);

                        // rr = rr - 2;
                        // Graphics g = Graphics.FromImage(rgb);
                        // g.DrawEllipse(Pens.Cyan, rx - rr, ry - rr, rr * 2, rr * 2);

                        if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses)
                        {
                            rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_0.bmp");
                            bmp.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_1.bmp");
                        }

                        rgb = new Crop(new Rectangle(rx - rr,
                                                        ry - rr,
                                                        (rx + rr > rgb.Width - 1 ? rgb.Width - 1 - rx : 2 * rr + 2),
                                                        (ry + rr > rgb.Height - 1 ? rgb.Height - 1 - ry : 2 * rr + 2))).Apply(rgb);
                        rgb = new ResizeBicubic(Constants.SIGN_WIDTH, Constants.SIGN_HEIGHT).Apply(rgb);
                        if (panel != null)
                        {
                            Graphics g = panel.getPanelGraphics();
                            g.DrawRectangle(Pens.Silver, 5, 4, Constants.SIGN_WIDTH, Constants.SIGN_HEIGHT);
                            g.DrawImage(rgb, new Point(5, 4));
                        }

                        if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses)
                            rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_3_" +rs+ ".bmp");

                        rgb = AutoBrightnessProcessor.autoBrightness(rgb, Rectangle.Empty, Constants.AUTOBRIGHTNESS_BASE_LUM);

                        if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses)
                            rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_4.bmp");

                        if (Constants.LABELING_TYPE == Constants.LabelingType.redBlackWhite)
                            rgb = new ColorLabelFilter(new Color[] { Color.FromArgb(255, 0, 0), Color.White, Color.Black }, true).Apply(rgb);
                        else if (Constants.LABELING_TYPE == Constants.LabelingType.blackAndWhite)
                        {
                            int dynamic_gray = 0;
                            rgb = new ColorLabelFilter(new Color[] { Color.White, Color.FromArgb(dynamic_gray, dynamic_gray, dynamic_gray) }, true).Apply(rgb);
                        }

                        /* int start_x = 0, start_y = 0;
                        int y = 0;
                        for (; y < Constants.SIGN_HEIGHT && (start_x + start_y) == 0; y++)
                        {
                            for (int x = 0; x < Constants.SIGN_WIDTH; x++)
                            {
                                Color clr = rgb.GetPixel(x, y);
                                if (clr.R == 0)
                                {
                                    start_x = x;
                                    start_y = y;
                                    break;
                                }
                            }
                        }
                        PointedColorFloodFill filter = new PointedColorFloodFill();
                        filter.Tolerance = Color.FromArgb(0, 0, 0);
                        filter.FillColor = Color.FromArgb(255, 255, 255);
                        filter.StartingPoint = new Point(start_x, start_y);
                        Bitmap rgb_flood_fill = filter.Apply(rgb);

                        bool black_region_found_at_center = false;
                        y = 4 * (Constants.SIGN_HEIGHT / 10);
                        for (int x = 4 * (Constants.SIGN_WIDTH / 10); x < 6 * (Constants.SIGN_WIDTH / 10); x += 2)
                        {
                            Color clr = rgb_flood_fill.GetPixel(x, y);
                            if (clr.R == 0)
                            {
                                black_region_found_at_center = true;
                            }
                        }
                        if (!black_region_found_at_center)
                        {
                            y = 6 * (Constants.SIGN_HEIGHT / 10);
                            for (int x = 4 * (Constants.SIGN_WIDTH / 10); x < 6 * (Constants.SIGN_WIDTH / 10); x += 2)
                            {
                                Color clr = rgb_flood_fill.GetPixel(x, y);
                                if (clr.R == 0)
                                {
                                    black_region_found_at_center = true;
                                    break;
                                }
                            }
                        }
                        if (black_region_found_at_center)
                        {
                            rgb = rgb_flood_fill;
                        }
                        */

                        BitmapData image_data = rgb.LockBits(new Rectangle(0, 0, rgb.Width, rgb.Height), ImageLockMode.ReadWrite, rgb.PixelFormat);
                        int bpp = 3;
                        int nOffset = image_data.Stride - rgb.Width * bpp;
                        System.IntPtr Scan0 = image_data.Scan0;
                        unsafe
                        {
                            byte* p = (byte*)Scan0;
                            for (int y = 0; y < Constants.SIGN_HEIGHT; y++)
                            {
                                // for each pixel
                                for (int x = 0; x < Constants.SIGN_WIDTH; x++, p += bpp)
                                {
                                    if (y >= Constants.SIGN_HEIGHT - 10 || y <= 10 || x >= Constants.SIGN_WIDTH - 10 || x <= 10 ||
                                        Math.Sqrt( (y - Constants.SIGN_HEIGHT / 2) * (y - Constants.SIGN_HEIGHT / 2) + (x - Constants.SIGN_WIDTH / 2) * (x - Constants.SIGN_WIDTH / 2)) > 24
                                        )
                                    {
                                        p[RGB.R] = 255;
                                        p[RGB.G] = 255;
                                        p[RGB.B] = 255;
                                    }
                                    else if (p[RGB.R] == 0)
                                    {
                                        com_x_sum += x;
                                        com_y_sum += y;
                                        com_x_y_point_count++;
                                    }
                                }
                                p += nOffset;
                            }

                        }
                        rgb.UnlockBits(image_data);

                        // Graphics g = Graphics.FromImage(rgb);
                        // g.DrawEllipse(Pens.Green, 32 - 24, 32 - 24, 24 * 2, 24 * 2);

                        if (com_x_y_point_count < 10)
                            bestChromo = null;
                        else if (Constants.HARVEST_TYPE > Constants.HarvestType.noHarvest)
                        {
                            if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestIntoFolder)
                            {
                                string[] signs = Globals.SIGN_IN_FRAME.Split(',');

                                for (int j = 1; j < signs.Length; j++)
                                {
                                    if (!Directory.Exists(Constants.base_folder + "hasat\\" + signs[j]))
                                        Directory.CreateDirectory(Constants.base_folder + "hasat\\" + signs[j]);
                                    lock (Globals.HARVEST_LOCK)
                                    {
                                        rgb.Save(Constants.base_folder + "hasat\\" + signs[j] + "\\" + Globals.HARVEST_SIGN_ID + ".bmp");
                                    }
                                }
                            }
                            else
                            {
                                rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_6.bmp");
                            }
                        }

                        gtcBmp = rgb;
                    }
                }

                if (Constants.EVALUATE_TIME_ENABLED)
                {
                    int x = (int)(DateTime.Now.Ticks / 10000 - start);
                    if (x >= 0)
                    {
                        Globals.TIME_CIRCULAR_MIN = x < Globals.TIME_CIRCULAR_MIN ? x : Globals.TIME_CIRCULAR_MIN;
                        Globals.TIME_CIRCULAR_MAX = x > Globals.TIME_CIRCULAR_MAX ? x : Globals.TIME_CIRCULAR_MAX;
                        Globals.TIME_CIRCULAR_TOTAL += x;
                    }
                }

                if (bestChromo != null)
                {
                    if (panel != null)
                    {
                        Graphics g = panel.getPanelGraphics();
                        g.DrawRectangle(Pens.Silver, 5, 74, Constants.SIGN_WIDTH, Constants.SIGN_HEIGHT);
                        g.DrawImage(gtcBmp, new Point(5, 74));
                    }
                    if (msgService != null)
                    {
                        VisionMessage vm = new VisionMessage(gtcBmp.Height, gtcBmp.Width, ByteTools.pixelFormatToBPP(gtcBmp.PixelFormat), gtcBmp);

                        // Center of Mass
                        int com_x = com_x_sum / (com_x_y_point_count == 0 ? 1 : com_x_y_point_count);
                        int com_y = com_y_sum / (com_x_y_point_count == 0 ? 1 : com_x_y_point_count);
                        vm.CoM_X = com_x;
                        vm.CoM_Y = com_y;
                        msgService.sendMsg(vm);
                    }
                }
                else
                {
                    if (Globals.RIGHT_PANEL_SHOWING_STH)
                    {
                        Globals.RIGHT_PANEL_SHOWING_STH = false;
                        // Bitmap img = new Bitmap(160, Constants.IMAGE_HEIGHT, PixelFormat.Format24bppRgb);
                        Graphics g = panel.getPanelGraphics();
                        g.Clear(Color.Silver);

                        /*if (msgService != null)
                        {
                            VisionMessage vm = new VisionMessage(img.Height, img.Width, ByteTools.pixelFormatToBPP(img.PixelFormat), img);
                            vm.bypass = true;
                            msgService.sendMsg(vm);
                        }*/
                    }
                }

                threadRunning = false;
            }
Beispiel #6
0
 /// <summary> buffer callback, COULD BE FROM FOREIGN THREAD. </summary>
 int ISampleGrabberCB.BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
 {
     Bitmap bitmap = new Bitmap(m_videoWidth, m_videoHeight, m_stride, PixelFormat.Format24bppRgb, pBuffer);
     bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
     // byte[] bmp = ByteTools.BmpToBytes(bitmap, PixelFormat.Format24bppRgb);
     VisionMessage vm = new VisionMessage(m_videoHeight, m_videoWidth, ByteTools.pixelFormatToBPP(PixelFormat.Format24bppRgb), bitmap);
     msgService.sendMsg(vm);
     Thread.Sleep((int)(videoInfoHeader.AvgTimePerFrame/20000));
     return 0;
 }
Beispiel #7
0
 private static byte fastGetPixelR(VisionMessage vm, int x, int y)
 {
     int imgWidth = vm.BmpWidth;
     int pixelSize = vm.BytePerPixel;
     byte[] img = ByteTools.BmpToBytes(vm.Bitmap, vm.Bitmap.PixelFormat);
     return img[y * pixelSize * imgWidth + x * pixelSize + 2];
 }
Beispiel #8
0
            public override void consumeMessage(IMessage message, int msgID)
            {
                long start = DateTime.Now.Ticks / 10000;
                Globals.FRAMES_PROCESSED_AUTOBRIGHTNESS++;

                VisionMessage vm = message as VisionMessage;
                Bitmap rgb = vm.Bitmap;
                rgb = autoBrightness(rgb, baseRect, baseLum);
                vm = new VisionMessage(rgb.Height, rgb.Width, ByteTools.pixelFormatToBPP(rgb.PixelFormat), rgb);

                if (Constants.EVALUATE_TIME_ENABLED)
                {
                    int x = (int)(DateTime.Now.Ticks / 10000 - start);
                    if (x >= 0)
                    {
                        Globals.TIME_AUTOBRIGHTNESS_MIN = x < Globals.TIME_AUTOBRIGHTNESS_MIN ? x : Globals.TIME_AUTOBRIGHTNESS_MIN;
                        Globals.TIME_AUTOBRIGHTNESS_MAX = x > Globals.TIME_AUTOBRIGHTNESS_MAX ? x : Globals.TIME_AUTOBRIGHTNESS_MAX;
                        Globals.TIME_AUTOBRIGHTNESS_TOTAL += x;
                    }
                }
                msgService.sendMsg(vm);
            }
Beispiel #9
0
            public override void consumeMessage(IMessage message, int msgID)
            {
                long start = DateTime.Now.Ticks / 10000;
                Globals.FRAMES_PROCESSED_SVM++;

                VisionMessage vm = message as VisionMessage;
                if (vm.bypass)
                {
                    vm.bypass = false;
                    msgService.sendMsg(vm);
                    return;
                }
                Bitmap rgb = vm.Bitmap;

                Node[] nds = new Node[NNTrain.numOfinputs];

                SVMTrain.fillFeatures_SURF(rgb, vm.CoM_X, vm.CoM_Y, nds);

                /*
                Bitmap img = new Bitmap(160, Constants.IMAGE_HEIGHT, PixelFormat.Format24bppRgb);
                Graphics g = Graphics.FromImage(img);
                g.Clear(Color.Silver);
                g.DrawImage(rb.Apply(rgb), new Point(16, 6));
                */
                if (this.msgService == null)    // Main'den test amacli cagirdigimiz zaman
                    rgb.Save(Constants.base_folder + "hasat\\zzz\\" + Globals.HARVEST_SIGN_ID + "_surf.bmp");
                else if (Constants.HARVEST_TYPE > Constants.HarvestType.noHarvest)
                    rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_surf.bmp");

                double[] arr = Prediction.PredictProbability(model, nds);
                int sign_class = (int)Prediction.Predict(model, nds);

                if (arr[0] + arr[1] + arr[2] != 0)
                    Console.WriteLine("");

                if (signType == Constants.SignType.triangular && !Constants.triangles.Contains(sign_class))
                    return;
                if (signType == Constants.SignType.circular && !Constants.circles.Contains(sign_class))
                    return;

                // g.DrawImage(rb.Apply(signs[sign_class]), new Point(16, 150));

                if (Constants.EVALUATE_SUCCESS_ENABLED)
                {
                    if (Globals.SIGN_IN_FRAME.Contains("," + sign_class))
                    {
                        Globals.TRUE_POSITIVES++;
                        Globals.SIGN_IN_FRAME_IS_DETECTED = true;
                        if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses)
                            signs[sign_class].Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_true.bmp");
                    }
                    else
                    {
                        Globals.FALSE_POSITIVES++;
                        if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses)
                            signs[sign_class].Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_false.bmp");
                    }
                }

                if (this.msgService == null)    // Main'den test amacli cagirdigimiz zaman
                {
                    signs[sign_class].Save(Constants.base_folder + "hasat\\zzz\\" + Globals.HARVEST_SIGN_ID + "_svm_decide.bmp");
                    return;
                }

                if (Constants.EVALUATE_TIME_ENABLED)
                {
                    int x = (int)(DateTime.Now.Ticks / 10000 - start);
                    if (x >= 0)
                    {
                        Globals.TIME_SVM_MIN = x < Globals.TIME_SVM_MIN ? x : Globals.TIME_SVM_MIN;
                        Globals.TIME_SVM_MAX = x > Globals.TIME_SVM_MAX ? x : Globals.TIME_SVM_MAX;
                        Globals.TIME_SVM_TOTAL += x;
                    }
                }

                // Globals.RIGHT_PANEL_SHOWING_STH = true;
                Bitmap img = signs[sign_class];
                vm = new VisionMessage(img.Height, img.Width, ByteTools.pixelFormatToBPP(img.PixelFormat), img);
                msgService.sendMsg(vm);
            }
Beispiel #10
0
            public override void consumeMessage(IMessage message, int msgID)
            {
                VisionMessage vm = message as VisionMessage;
                Bitmap rgb = vm.Bitmap;

                rgb = filter.Apply(rgb);

                if (rgb.PixelFormat == PixelFormat.Format8bppIndexed)
                {
                    rgb = new GrayscaleToRGB().Apply(rgb);
                }
                vm = new VisionMessage(rgb.Height, rgb.Width, ByteTools.pixelFormatToBPP(rgb.PixelFormat), rgb);
                msgService.sendMsg(vm);
            }
Beispiel #11
0
            public override void consumeMessage(IMessage message, int msgID)
            {
                VisionMessage vm = message as VisionMessage;
                // Bitmap bitmap = ByteTools.BytesToBmp(vm.Bitmap, vm.BmpWidth, vm.BmpHeight, ByteTools.bppToPixelFormat(vm.BytePerPixel));
                Bitmap bitmap = vm.Bitmap;
                Bitmap rgb = bitmap;

                bitmap = new ColorRemapping(redMap, greenMap, blueMap).Apply(bitmap);
                bitmap = new GrayscaleBT709().Apply(bitmap);
                bitmap = new Threshold(32).Apply(bitmap);

                //rgb = new GrayscaleToRGB().Apply(bitmap);

                Graphics g = Graphics.FromImage(rgb);

                if (g == null)
                {
                    Console.WriteLine("Null Graphics!");
                    return;
                }

                if (!threadRunning)
                {
                    threadRunning = true;
                    for (int i = 0; i < 3; i++)
                    {
                        if (g != null && currentStateProbs[i] > double.MinValue)
                        {
                            drawLine(g, new HoughLine((double)currentThetaStates[i], (short)currentRStates[i], 0, 0d),
                               new Rectangle(0, heightRes[0], bitmap.Width, (heightRes[heightRes.Length - 1] - heightRes[0]) * 2));
                        }
                    }
                    ThreadStart starter = delegate { findLines(bitmap); };
                    Thread innerThread = new Thread(starter);
                    innerThread.Start();
                }

                vm = new VisionMessage(rgb.Height, rgb.Width, ByteTools.pixelFormatToBPP(rgb.PixelFormat), rgb);
                msgService.sendMsg(vm);
            }
Beispiel #12
0
            void searchSolution(Bitmap rgb)
            {
                long start = DateTime.Now.Ticks / 10000;
                Globals.FRAMES_PROCESSED_TRIANGULAR++;
                int com_x_sum = 0, com_y_sum = 0, com_x_y_point_count = 0;
                Globals.HARVEST_SIGN_ID++;

                //if (redTestPoints == null)
                //    calculateRedTestPoints();

                Bitmap bmp = sf.Apply(rgb);
                GeoTransChromosome sampleChromosome = new GeoTransChromosome(bmp, sf, this, null);

                Population tmpPopulation = new Population(Constants.GA_POPULATION_SIZE,
                    sampleChromosome,
                    sampleChromosome,
                    new EliteSelection()
                );
                tmpPopulation.MutationRate = Constants.GA_MUTATION_RATE;
                tmpPopulation.CrossoverRate = Constants.GA_CROSSOVER_RATE;

                if (population == null || population.BestChromosome == null || population.BestChromosome.Fitness < RS_THRESHOLD / 4)
                {
                    // fresh population
                }
                else
                {
                    // half from previous
                    for (int j = 0; j < tmpPopulation.Size / 2; j++)
                    {
                        ((GeoTransChromosome)tmpPopulation[j]).copyContent((GeoTransChromosome)population[j]);
                    }
                }
                population = tmpPopulation;

                Graphics gg = null;

                /*
                Bitmap bmp_x = null;
                if (1!=1)
                {
                    bmp_x = new Bitmap(Constants.IMAGE_WIDTH, Constants.IMAGE_HEIGHT * 2, PixelFormat.Format24bppRgb);
                    gg = Graphics.FromImage(bmp_x);
                    gg.DrawImage(bmp, 0, 0);
                    gg.DrawImage(rgb, 0, Constants.IMAGE_HEIGHT);
                    for (int x = 0; x < population.Size; x++)
                    {
                        GeoTransChromosome chromo = (GeoTransChromosome)population[x];
                        gg.DrawRectangle(Pens.Cyan, chromo.X, chromo.Y, 1, 1);
                    }
                }
                */

                int i = 0;
                do
                {
                    long start_epoch = DateTime.Now.Ticks / 10000;
                    Globals.FRAMES_PROCESSED_GA_RUNEPOCH++;

                    // run one epoch of genetic algorithm
                    population.RunEpoch();

                    if (Constants.EVALUATE_TIME_ENABLED)
                    {
                        int x = (int)(DateTime.Now.Ticks / 10000 - start_epoch);
                        if (x >= 0)
                        {
                            Globals.TIME_GA_RUNEPOCH_MIN = x < Globals.TIME_GA_RUNEPOCH_MIN ? x : Globals.TIME_GA_RUNEPOCH_MIN;
                            Globals.TIME_GA_RUNEPOCH_MAX = x > Globals.TIME_GA_RUNEPOCH_MAX ? x : Globals.TIME_GA_RUNEPOCH_MAX;
                            Globals.TIME_GA_RUNEPOCH_TOTAL += x;
                        }
                    }
                    i++;
                } while (i < Constants.GA_NUMBER_ITERATIONS);

                GeoTransChromosome bestChromo = null;
                float rs = 0;
                if (population.BestChromosome != null && population.BestChromosome.Fitness > RS_THRESHOLD)
                {
                    bestChromo = (GeoTransChromosome)population.BestChromosome;

                    int rx = bestChromo.X, ry = bestChromo.Y, rr = bestChromo.Width;
                    rs = (float)bestChromo.Fitness;

                    if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses)
                    {
                        rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_0.bmp");
                        bmp.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_1.bmp");
                    }
                    // findTriangle(bestChromo.X, bestChromo.Y, bmp, ref rx, ref ry, ref rr, ref rs);

                    if (rs < RS_THRESHOLD || rr==0)
                    {
                        if (gg != null)
                        {
                            gg.DrawRectangle(Pens.Red, bestChromo.X - 1, bestChromo.Y - 1, 3, 3);
                            gg.DrawRectangle(Pens.Red, bestChromo.X - bestChromo.Width / 2, bestChromo.Y - bestChromo.Width / 2, bestChromo.Width, bestChromo.Width);
                        }
                        bestChromo = null;
                    }
                    else
                    {
                        rr = (int)(Constants.CAPTURE_RESIZE_TRIANGLE * rr);
                        int xx = (int)(rr * Math.Cos(Math.PI * 30 / 180));  // üçgenin kenarı: 2 . xx
                        int yy = (int)(rr * Math.Sin(Math.PI * 30 / 180));  // üçgenin yüksekliği: 3 . yy

                        int x_left = rx - xx;
                        int y_top = ry - (2 * yy) - ((3*yy)/10);
                        rgb = new Crop(new Rectangle(x_left,
                                                    y_top,
                                                    (x_left + 2 * xx > rgb.Width - 1 ? rgb.Width - 1 - x_left : 2 * xx),
                                                    (y_top + 2 * xx > rgb.Height - 1 ? rgb.Height - 1 - y_top : 2 * xx))).Apply(rgb);
                        if (gg != null)
                        {
                            gg.DrawImage(rgb, 1, Constants.IMAGE_HEIGHT + 1);
                            gg.DrawRectangle(Pens.DarkOrchid, rx - 1, ry - 1, 3, 3);
                            gg.DrawRectangle(Pens.Green, x_left, y_top, rgb.Width, rgb.Height);
                        }

                        rgb = new ResizeBicubic(Constants.SIGN_WIDTH, Constants.SIGN_HEIGHT).Apply(rgb);
                        if (panel != null)
                        {
                            Graphics g = panel.getPanelGraphics();
                            g.DrawRectangle(Pens.Silver, 5, 4, Constants.SIGN_WIDTH, Constants.SIGN_HEIGHT);
                            g.DrawImage(rgb, new Point(5, 4));
                        }

                        if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses)
                            rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_3_" +rs+ ".bmp");
                        rgb = AutoBrightnessProcessor.autoBrightness(rgb, Rectangle.Empty, Constants.AUTOBRIGHTNESS_BASE_LUM);

                        if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses)
                            rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_4.bmp");

                        int dynamic_gray = 0;
                        if (Constants.LABELING_TYPE == Constants.LabelingType.redBlackWhite)
                            rgb = new ColorLabelFilter(new Color[] { Color.FromArgb(255, 0, 0), Color.White, Color.Black }, true).Apply(rgb);
                        else if (Constants.LABELING_TYPE == Constants.LabelingType.blackAndWhite)
                        {
                            rgb = new ColorLabelFilter(new Color[] { Color.White, Color.FromArgb(dynamic_gray, dynamic_gray, dynamic_gray) }, true).Apply(rgb);
                        }
                        if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses)
                            rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_5.bmp");

                        int start_x = 0, start_y = 0;
                        int y = 0;
                        for (; y < Constants.SIGN_HEIGHT && (start_x+start_y)==0; y++)
                        {
                            for (int x = 0; x < Constants.SIGN_WIDTH; x++)
                            {
                                Color clr = rgb.GetPixel(x, y);
                                if (clr.R == 0)
                                {
                                    start_x = x;
                                    start_y = y;
                                    break;
                                }
                            }
                        }
                        PointedColorFloodFill filter = new PointedColorFloodFill();
                        filter.Tolerance = Color.FromArgb(0, 0, 0);
                        filter.FillColor = Color.FromArgb(255, 255, 255);
                        filter.StartingPoint = new Point(start_x, start_y);
                        Bitmap rgb_flood_fill = filter.Apply(rgb);

                        int black_region_found_at_center = 0;
                        y = 4 * (Constants.SIGN_HEIGHT / 10);
                        for (int x = 4 * (Constants.SIGN_WIDTH / 10); x < 6 * (Constants.SIGN_WIDTH / 10); x += 2)
                        {
                            Color clr = rgb_flood_fill.GetPixel(x, y);
                            if (clr.R == 0)
                            {
                                black_region_found_at_center++;
                            }
                        }
                        if (black_region_found_at_center < 8)
                        {
                            y = 6 * (Constants.SIGN_HEIGHT / 10);
                            for (int x = 4 * (Constants.SIGN_WIDTH / 10); x < 6 * (Constants.SIGN_WIDTH / 10); x += 2)
                            {
                                Color clr = rgb_flood_fill.GetPixel(x, y);
                                if (clr.R == 0)
                                {
                                    black_region_found_at_center++;
                                }
                            }
                        }
                        if (black_region_found_at_center >= 5)
                        {
                            rgb = rgb_flood_fill;
                        }

                        BitmapData image_data = rgb.LockBits(new Rectangle(0, 0, rgb.Width, rgb.Height), ImageLockMode.ReadWrite, rgb.PixelFormat);
                        int bpp = 3;
                        int nOffset = image_data.Stride - rgb.Width * bpp;
                        System.IntPtr Scan0 = image_data.Scan0;
                        unsafe
                        {
                            byte* p = (byte*)Scan0;
                            for (y = 0; y < Constants.SIGN_HEIGHT; y++)
                            {
                                // for each pixel
                                for (int x = 0; x < Constants.SIGN_WIDTH; x++, p += bpp)
                                {
                                    if (y >= Constants.SIGN_HEIGHT - 12 || y <= 14 || x >= Constants.SIGN_WIDTH - 12 || x <= 12
                                        ||
                                             (x <= Constants.SIGN_WIDTH / 2 && y <= (Constants.SIGN_HEIGHT) - 2 * x + 8)
                                        || (x > Constants.SIGN_WIDTH / 2 && y <= 2 * (x - Constants.SIGN_HEIGHT / 2) + 8)

                                        )
                                    {
                                        p[RGB.R] = 255;
                                        p[RGB.G] = 255;
                                        p[RGB.B] = 255;
                                    }
                                    else if (p[RGB.R] == 0)
                                    {
                                        com_x_sum += x;
                                        com_y_sum += y;
                                        com_x_y_point_count++;
                                    }
                                }
                                p += nOffset;
                            }
                        }
                        rgb.UnlockBits(image_data);

                        if (com_x_y_point_count < 10)
                            bestChromo = null;
                        else if (Constants.HARVEST_TYPE > Constants.HarvestType.noHarvest)
                        {
                            if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestIntoFolder)
                            {
                                string[] signs = Globals.SIGN_IN_FRAME.Split(',');

                                for (int j = 1; j < signs.Length; j++)
                                {
                                    if (!Directory.Exists(Constants.base_folder + "hasat\\" + signs[j]))
                                        Directory.CreateDirectory(Constants.base_folder + "hasat\\" + signs[j]);
                                    lock (Globals.HARVEST_LOCK)
                                    {
                                        rgb.Save(Constants.base_folder + "hasat\\" + signs[j] + "\\" + Globals.HARVEST_SIGN_ID + ".bmp");
                                    }
                                }
                            }
                            else
                            {
                                rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_6.bmp");
                            }
                        }

                        gtcBmp = rgb;
                    }
                }

                if (Constants.EVALUATE_TIME_ENABLED)
                {
                    int x = (int)(DateTime.Now.Ticks / 10000 - start);
                    if (x >= 0)
                    {
                        Globals.TIME_TRIANGLE_MIN = x < Globals.TIME_TRIANGLE_MIN ? x : Globals.TIME_TRIANGLE_MIN;
                        Globals.TIME_TRIANGLE_MAX = x > Globals.TIME_TRIANGLE_MAX ? x : Globals.TIME_TRIANGLE_MAX;
                        Globals.TIME_TRIANGLE_TOTAL += x;
                    }
                }

                if (bestChromo != null)
                {
                    if (panel != null)
                    {
                        Graphics g = panel.getPanelGraphics();
                        g.DrawRectangle(Pens.Silver, 5, 74, Constants.SIGN_WIDTH, Constants.SIGN_HEIGHT);
                        g.DrawImage(gtcBmp, new Point(5, 74));
                    }
                    if (msgService != null)
                    {
                        VisionMessage vm = new VisionMessage(gtcBmp.Height, gtcBmp.Width, ByteTools.pixelFormatToBPP(gtcBmp.PixelFormat), gtcBmp);

                        // Center of Mass
                        vm.CoM_X = com_x_sum / (com_x_y_point_count == 0 ? 1 : com_x_y_point_count);
                        vm.CoM_Y = com_y_sum / (com_x_y_point_count == 0 ? 1 : com_x_y_point_count);
                        msgService.sendMsg(vm);
                    }
                }
                else
                {
                    if (Globals.RIGHT_PANEL_SHOWING_STH)
                    {
                        Globals.RIGHT_PANEL_SHOWING_STH = false;
                        Graphics g = panel.getPanelGraphics();
                        g.Clear(Color.Silver);
                    }
                }

                //if (rs > RS_THRESHOLD)
                //    bmp_x.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_" +rs+ "_" +sf.BlueCoeff+ "_" +sf.GreenCoeff+ ".bmp");

                threadRunning = false;
            }
Beispiel #13
0
 public static void getVmHist(VisionMessage vm, int[] rHist, int[] gHist, int[] bHist)
 {
     for (int x = vm.BmpWidth / 2; x < vm.BmpWidth; x++)
     {
         for (int y = 0; y < vm.BmpHeight; y++)
         {
             rHist[fastGetPixelR(vm, x, y)]++;
             gHist[fastGetPixelG(vm, x, y)]++;
             bHist[fastGetPixelB(vm, x, y)]++;
             fastSetPixel(vm, x, y, 0, 0, 0);
         }
     }
 }
Beispiel #14
0
 private static void fastSetPixel(VisionMessage vm, int x, int y, byte R, byte G, byte B)
 {
     int imgWidth = vm.BmpWidth;
     int pixelSize = vm.BytePerPixel;
     byte[] img = ByteTools.BmpToBytes(vm.Bitmap, vm.Bitmap.PixelFormat);
     img[y * pixelSize * imgWidth + x * pixelSize] = B;
     img[y * pixelSize * imgWidth + x * pixelSize + 1] = G;
     img[y * pixelSize * imgWidth + x * pixelSize + 2] = R;
 }
Beispiel #15
0
        public VisionMessage postToVision(List <ExpenseRecord> records)
        {
            string _lastEmployee = "";
            int    _detailSeq    = 0;
            int    _masterSeq    = 0
                                   int _period = 0;
            string _company = "";

            XElement        recs        = new XElement("RECS");
            List <XElement> detailRows  = new List <XElement>();
            List <XElement> masterRows  = new List <XElement>();
            List <XElement> controlRows = new List <XElement>();

            //creates a new posting batch id based on a guid
            string _batchId    = $"EX_{DateTime.Now:yyyymmddhhMMss}";
            string _masterPKey = "";

            try
            {
                //goes through the records ordered by employee and transdate
                foreach (var record in records.OrderBy(x => x.Employee).ThenBy(x => x.TransDate))
                {
                    //if the employee changes we will start a new master record (report)
                    if (_lastEmployee != record.Employee)
                    {
                        _detailSeq  = 0;
                        _masterSeq += 1;
                        _masterPKey = $"{_batchId}{_masterSeq:00}";

                        masterRows.Add(new XElement("ROW",
                                                    new XAttribute("tranType", "INSERT"),
                                                    new XElement("Batch", _batchId),
                                                    new XElement("MasterPKey", _masterPKey),
                                                    new XElement("Employee", record.Employee),
                                                    new XElement("ReportDate", record.TransDate),                                                //we simply apply the first date as report date
                                                    new XElement("ReportName", $"Expenses {record.Employee} for {record.TransDate.ToString()}"), //dummy report name
                                                    new XElement("Posted", "N"),
                                                    new XElement("Seq", _masterSeq),
                                                    new XElement("AdvanceAmount", 0),
                                                    new XElement("BarCode", ""),
                                                    new XElement("DefaultCurrencyCode", " ")
                                                    )
                                       );
                    }

                    _detailSeq += 1;

                    //assumes that all period and company values are the same in one file
                    _period  = record.Period;
                    _company = record.Company;
                    //for non-multi company the value must be a [space] character
                    if (_company == "")
                    {
                        _company = " ";
                    }

                    detailRows.Add(new XElement("ROW",
                                                new XAttribute("tranType", "INSERT"),
                                                new XElement("Batch", _batchId),
                                                new XElement("MasterPKey", _masterPKey),
                                                new XElement("PKey", GetGuid()),
                                                new XElement("Seq", _detailSeq),
                                                new XElement("TransDate", record.TransDate),
                                                new XElement("WBS1", record.WBS1),
                                                new XElement("WBS2", record.WBS2),
                                                new XElement("WBS3", record.WBS3),
                                                new XElement("Account", record.Account),
                                                new XElement("Amount", record.Amount.ToString("#.00")),
                                                new XElement("Description", record.Description),
                                                new XElement("SuppressBill", record.Billable == true ? "N" : "Y"),
                                                new XElement("NetAmount", record.Amount.ToString("#.00")),
                                                new XElement("PaymentExchangeRate", "1.00"),
                                                new XElement("PaymentAmount", record.Amount.ToString("#.00")),
                                                new XElement("PaymentExchangeInfo", 0),
                                                new XElement("CurrencyExchangeOverrideRate", 0),
                                                new XElement("CurrencyCode", " "),
                                                new XElement("OriginatingVendor", "")
                                                )
                                   );

                    _lastEmployee = record.Employee;
                }

                //add a control record
                controlRows.Add(new XElement("ROW",
                                             new XAttribute("tranType", "INSERT"),
                                             new XElement("Batch", _batchId),
                                             new XElement("Recurring", "N"),
                                             new XElement("Posted", "N"),
                                             new XElement("Creator", "DELTEKAPI"),
                                             new XElement("Period", _period),
                                             new XElement("EndDate", detailRows.Min(x => x.TryGetElementDateValue("TransDate").ToString("s"))),
                                             new XElement("Total", detailRows.Sum(x => x.TryGetElementDecimalValue("Amount"))),
                                             new XElement("AdvanceAmount", 0),
                                             new XElement("Company", _company),
                                             new XElement("DefaultCurrencyCode", " ")
                                             )
                                );

                //create full posting message
                foreach (var ctrl in controlRows)
                {
                    XElement exControl = new XElement("exControl",
                                                      new XAttribute("name", "exControl"),
                                                      new XAttribute("alias", "exControl"),
                                                      new XAttribute("keys", "Batch"),
                                                      new XElement(ctrl)
                                                      );
                    XElement exMaster = new XElement("exMaster",
                                                     new XAttribute("name", "exMaster"),
                                                     new XAttribute("alias", "exMaster"),
                                                     new XAttribute("keys", "Batch,Employee"),
                                                     masterRows.Where(x => x.Element("Batch").Value == ctrl.Element("Batch").Value)
                                                     );
                    XElement exDetail = new XElement("exDetail",
                                                     new XAttribute("name", "exDetail"),
                                                     new XAttribute("alias", "exDetail"),
                                                     new XAttribute("keys", "Batch,MasterPKey,PKey"),
                                                     detailRows.Where(x => x.Element("Batch").Value == ctrl.Element("Batch").Value)
                                                     );

                    recs.Add(
                        new XElement("REC",
                                     exControl,
                                     exMaster,
                                     exDetail
                                     )
                        );
                }

                //check if there are any rows to submit
                VisionMessage retval = new VisionMessage();
                if (detailRows.Count > 0)
                {
                    recs.ApplyVisionDefaultNamespace();
                    retval = repository.AddTransaction(TransactionType.EX, recs);
                }
                else
                {
                    retval = new VisionMessage("0", "No Data To Process", "");
                }

                //auto posting
                if (retval.ReturnCode == "1")
                {
                    if (Properties.Settings.Default.AutoPost == true)
                    {
                        retval = repository.PostTransaction(
                            TransactionType.LA,
                            _batchId,
                            _period);
                    }
                }

                return(retval);
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Exception adding Expense posting", ex);
            }
        }
Beispiel #16
0
            public override void consumeMessage(IMessage message, int msgID)
            {
                long start = DateTime.Now.Ticks / 10000;
                VisionMessage vm = message as VisionMessage;
                if (vm.bypass)
                {
                    msgService.sendMsg(vm);
                    return;
                }
                Bitmap rgb = vm.Bitmap; // ByteTools.BytesToBmp(vm.Bitmap, vm.BmpWidth, vm.BmpHeight, ByteTools.bppToPixelFormat(vm.BytePerPixel));

                int numOfinputs = 144; //NNTrain.NUM_MAX_INTEREST_POINTS * NNTrain.NUM_FEATURES;
                double[] input = new double[numOfinputs];

                NNTrain.fillFeatures(rgb, vm.CoM_X, vm.CoM_Y, input);

                SortedDictionary<double, int> outputs = new SortedDictionary<double, int>();
                int[] availSigns = networks.Keys.ToArray();
                foreach (int sign in availSigns)
                {
                     //triangle
                    if (signType == Constants.SignType.triangular && !Constants.triangles.Contains(sign))
                        continue;
                    if (signType == Constants.SignType.circular && !Constants.circles.Contains(sign))
                        continue;
                    outputs.Add(networks[sign].Compute(input)[0], sign);
                }

                double max_value = outputs.Last().Key;
                int sign_class = outputs.Last().Value;
                //Console.WriteLine("mv: " + max_value + " sign:" + sign_class);

                if (max_value > Constants.NN_THRESHOLD)
                {
                    msgService.sendMsg(new TextMessage("Sign:" + sign_class + " Prob:" + max_value.ToString("0.0000")));
                    Bitmap img = signs[sign_class];

                    if (Constants.EVALUATE_SUCCESS_ENABLED)
                    {
                        if (Globals.SIGN_IN_FRAME.Contains("," + sign_class))
                        {
                            Globals.TRUE_POSITIVES++;
                            Console.WriteLine("TRUE:" + sign_class);
                            Globals.SIGN_IN_FRAME_IS_DETECTED = true;
                            if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses)
                                signs[sign_class].Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_true_" + max_value + ".bmp");
                        }
                        else
                        {
                            Console.WriteLine("FALSE:" + sign_class, "," + Globals.SIGN_IN_FRAME);
                            Globals.FALSE_POSITIVES++;
                            if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses)
                                signs[sign_class].Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_false_" + max_value + ".bmp");
                        }
                    }
                    vm = new VisionMessage(img.Height, img.Width, ByteTools.pixelFormatToBPP(img.PixelFormat), img);
                    msgService.sendMsg(vm);
                }
                else
                {
                    if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses)
                        signs[sign_class].Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_nodecide_" + max_value + ".bmp");
                }

                if (this.msgService == null)    // Main'den test amacli cagirdigimiz zaman
                {
                    signs[sign_class].Save(Constants.base_folder + "hasat\\zzz\\" + Globals.HARVEST_SIGN_ID + "_surf_decide_" + max_value + ".bmp");
                    return;
                }

                if (Constants.EVALUATE_TIME_ENABLED)
                {
                    int x = (int)(DateTime.Now.Ticks / 10000 - start);
                    if (x >= 0)
                    {
                        Globals.TIME_SURF_MIN = x < Globals.TIME_SURF_MIN ? x : Globals.TIME_SURF_MIN;
                        Globals.TIME_SURF_MAX = x > Globals.TIME_SURF_MAX ? x : Globals.TIME_SURF_MAX;
                        Globals.TIME_SURF_TOTAL += x;
                    }
                }
            }
Beispiel #17
0
            /// <summary> buffer callback, COULD BE FROM FOREIGN THREAD. </summary>
            int ISampleGrabberCB.BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
            {
                Bitmap bitmap = new Bitmap(m_videoWidth, m_videoHeight, m_stride, PixelFormat.Format24bppRgb, pBuffer);
                bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
                // byte[] bmp = ByteTools.BmpToBytes(bitmap, PixelFormat.Format24bppRgb);
                VisionMessage vm = new VisionMessage(m_videoHeight, m_videoWidth, ByteTools.pixelFormatToBPP(PixelFormat.Format24bppRgb), bitmap);
                msgService.sendMsg(vm);
                //Thread.Sleep((int)(videoInfoHeader.AvgTimePerFrame / 10000));

                if (Constants.EVALUATE_SUCCESS_ENABLED)
                {
                    string tmp = (string)Globals.FRAME_SIGN_HASH["" + Globals.CURRENT_FRAME_INDEX];
                    if (tmp != null)
                    {   // 3 seconds passed. Now there is another sign in the video.
                        if (!Globals.SIGN_IN_FRAME.Contains(Constants.NO_SIGN) && !Globals.SIGN_IN_FRAME_IS_DETECTED)
                        {
                            Globals.NUMBER_OF_MISSES++;
                            Console.WriteLine("MISS:" + Globals.SIGN_IN_FRAME.Substring(Globals.SIGN_IN_FRAME.IndexOf(',')+1));
                            if (currentFrame!=null && (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses))
                                currentFrame.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_miss.bmp");
                        }
                        Globals.SIGN_IN_FRAME = tmp;
                        Globals.SIGN_IN_FRAME_IS_DETECTED = false;
                    }
                }
                else if (Constants.LABELING_ENABLED)
                {
                    AForge.Imaging.ImageStatistics statistics = new AForge.Imaging.ImageStatistics(bitmap);
                    if (Math.Abs(Globals.CURRENT_R_MEAN - Math.Round(statistics.Red.Mean)) > 1 || Math.Abs(Globals.CURRENT_G_MEAN - Math.Round(statistics.Green.Mean)) > 1 || Math.Abs(Globals.CURRENT_B_MEAN - Math.Round(statistics.Blue.Mean)) > 2)
                    {
                        Globals.CURRENT_R_MEAN = (int)Math.Round(statistics.Red.Mean);
                        Globals.CURRENT_G_MEAN = (int)Math.Round(statistics.Green.Mean);
                        Globals.CURRENT_B_MEAN = (int)Math.Round(statistics.Blue.Mean);
                    }
                }
                if (Globals.FRAME_COUNT == Globals.CURRENT_FRAME_INDEX)
                {
                    Globals.init();
                }
                else
                {
                    Globals.CURRENT_FRAME_INDEX++;
                }

                currentFrame = bitmap;
                return 0;
            }
Beispiel #18
0
            public override void consumeMessage(IMessage message, int msgID)
            {
                long start = DateTime.Now.Ticks / 10000;
                Globals.FRAMES_PROCESSED_NNSURF++;

                VisionMessage vm = message as VisionMessage;
                if (vm.bypass)
                {
                    msgService.sendMsg(vm);
                    return;
                }

                Bitmap rgb = vm.Bitmap;
                double[] input = new double[NNTrain.numOfinputs];

                NNTrain.fillFeatures_SURF(rgb, vm.CoM_X, vm.CoM_Y, input);

                double[] output = network.Compute(input);

                // Bitmap img = new Bitmap(160, Constants.IMAGE_HEIGHT, PixelFormat.Format24bppRgb);
                // Graphics g = Graphics.FromImage(img);
                // g.Clear(Color.Silver);
                // g.DrawImage(rb.Apply(rgb), new Point(16, 6));

                if (this.msgService == null)    // Main'den test amacli cagirdigimiz zaman
                    rgb.Save(Constants.base_folder + "hasat\\zzz\\" + Globals.HARVEST_SIGN_ID + "_surf.bmp");
                else if (Constants.HARVEST_TYPE > Constants.HarvestType.noHarvest)
                    rgb.Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_surf.bmp");

                int sign_class = 0;
                double max_value = Constants.MAX(output, ref sign_class);
                sign_class++;
                if (max_value > Constants.SURF_MATCH_THRESHOLD)        // default 0.9
                {
                    //if (signType == Constants.SignType.triangular && !Constants.triangles.Contains(sign_class))
                    //    return;
                    //if (signType == Constants.SignType.circular && !Constants.circles.Contains(sign_class))
                    //    return;

                    // Globals.RIGHT_PANEL_SHOWING_STH = true;
                    // g.DrawImage(rb.Apply(signs[sign_class]), new Point(16, 150));

                    if (Constants.EVALUATE_SUCCESS_ENABLED)
                    {
                        if (Globals.SIGN_IN_FRAME.Contains("," + sign_class))
                        {
                            Globals.TRUE_POSITIVES++;
                            Globals.SIGN_IN_FRAME_IS_DETECTED = true;
                            if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses)
                                signs[sign_class].Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_true_" + max_value + ".bmp");
                        }
                        else
                        {
                            Globals.FALSE_POSITIVES++;
                            if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses)
                                signs[sign_class].Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_false_" + max_value + ".bmp");
                        }
                    }

                    Bitmap img = signs[sign_class];
                    vm = new VisionMessage(img.Height, img.Width, ByteTools.pixelFormatToBPP(img.PixelFormat), img);
                    msgService.sendMsg(vm);
                }
                else
                {
                    if (Constants.HARVEST_TYPE == Constants.HarvestType.harvestAll || Constants.HARVEST_TYPE == Constants.HarvestType.harvestMisses)
                        signs[sign_class].Save(Constants.base_folder + "hasat\\" + Globals.HARVEST_SIGN_ID + "_nodecide_" + max_value + ".bmp");
                }

                if (this.msgService == null)    // Main'den test amacli cagirdigimiz zaman
                {
                    signs[sign_class].Save(Constants.base_folder + "hasat\\zzz\\" + Globals.HARVEST_SIGN_ID + "_surf_decide_" + max_value + ".bmp");
                    return;
                }

                if (Constants.EVALUATE_TIME_ENABLED)
                {
                    int x = (int)(DateTime.Now.Ticks / 10000 - start);
                    if (x >= 0)
                    {
                        Globals.TIME_NNSURF_MIN = x < Globals.TIME_NNSURF_MIN ? x : Globals.TIME_NNSURF_MIN;
                        Globals.TIME_NNSURF_MAX = x > Globals.TIME_NNSURF_MAX ? x : Globals.TIME_NNSURF_MAX;
                        Globals.TIME_NNSURF_TOTAL += x;
                    }
                }
            }
Beispiel #19
0
 public void sendBmp(Bitmap bitmap)
 {
     if (isStarted)
     {
         // byte[] bmp = ByteTools.BmpToBytes(bitmap, PixelFormat.Format24bppRgb);
         VisionMessage vm = new VisionMessage(bitmap.Height, bitmap.Width, ByteTools.pixelFormatToBPP(PixelFormat.Format24bppRgb), bitmap);
         msgService.sendMsg(vm);
     }
 }
Beispiel #20
0
 public IMessage duplicate()
 {
     VisionMessage vm = new VisionMessage();
     return vm.fromByteArray(this.toByteArray());
 }