Ejemplo n.º 1
0
        private void CalculateButton_OnClick(object sender, RoutedEventArgs e)
        {
            Save();

            var fileName = InspectionReportTextBox.Text;

            var inspectionReport = fileName.DeserializeFromXamlFile <InspectionReport>();

            var cs = CoordinateCircleCalculator.Calculate(inspectionReport.Results);

            var dt = DateTime.Now.ToString("_yyyy-MM-dd_HH.mm.ss");

            var newFileName = fileName + dt + ".csv";

            using (var csvWriterContainer = new CsvWriterContainer(newFileName))
            {
                var writer = csvWriterContainer.CsvWriter;

                writer.WriteField("Name.");
                writer.WriteField("X");
                writer.WriteField("Y");
                writer.WriteField("Angle");
                writer.WriteField("Length");
                writer.WriteField("|");
                writer.WriteField("X mm");
                writer.WriteField("Y mm");
                writer.WriteField("Angle");
                writer.NextRecord();

                foreach (ReferenceCircleInfo c in cs)
                {
                    writer.WriteField(c.Name);
                    writer.WriteField(c.Offset.X);
                    writer.WriteField(c.Offset.Y);
                    writer.WriteField(c.Angle);
                    writer.WriteField(c.Length);
                    writer.WriteField("|");
                    writer.WriteField(c.Offset.X.ToNumbericStringInMillimeterFromPixel(16));
                    writer.WriteField(c.Offset.Y.ToNumbericStringInMillimeterFromPixel(16));
                    writer.WriteField(c.Angle);
                    writer.NextRecord();
                }
            }

            MessageBox.Show(newFileName + " has exported.");
        }
Ejemplo n.º 2
0
        public void Run(string imageDir, InspectionSchema schema)
        {
            IList <string> fileNames;

            try
            {
                var strings = Directory.GetFiles(imageDir).ToList();

                fileNames = strings.Where(x =>
                {
                    if (x.EndsWith(".bmp"))
                    {
                        return(true);
                    }

                    if (x.EndsWith(".tif"))
                    {
                        return(true);
                    }

                    if (x.EndsWith(".jpg"))
                    {
                        return(true);
                    }

                    return(false);
                }).ToList();
            }
            catch (Exception)
            {
                MessageBox.Show("Image directory cannot load files! " + imageDir);
                return;
            }


            // Inspect
            IList <InspectionResult> inspectionResults = new List <InspectionResult>();

//            IList<ImageInfo> imageInfos = new List<ImageInfo>();
            IList <HImage> images = new List <HImage>();

            foreach (var fileName in fileNames)
            {
                string name = fileName;

                var fn = (string)name;
                Debug.WriteLine("Task.Started: " + fn);

//                BitmapImage bi = null;
//                try
//                {
//                    Debug.WriteLine("BitmapImage() begin");
//                    bi = new BitmapImage(new Uri(fn, UriKind.RelativeOrAbsolute));
//                    Debug.WriteLine("BitmapImage() end");
//                }
//                catch (Exception e2)
//                {
//                    MessageBox.Show("BitmapImage loading error: " + fn);
//                }
//
//                Debug.WriteLine("ToImageInfoWith8Bpp() begin");
//                var imageInfo = bi.ToImageInfoWith8Bpp();
//                Debug.WriteLine("ToImageInfoWith8Bpp() end");
//
//                imageInfos.Add(imageInfo);

                images.Add(new HImage(name));
            }

            IList <Task> tasks = new List <Task>();

            foreach (var imageInfo in images)
            {
//                string name = fileName;
                var imageInfo2 = imageInfo;
                var task       = new Task(
                    (x) =>
                {
                    HImage imageInfo3 = (HImage)imageInfo2;
//                        Debug.WriteLine("Task.Started: " + fn);
//
//                        BitmapImage bi = null;
//                        try
//                        {
//                            Debug.WriteLine("BitmapImage() begin");
//                            bi = new BitmapImage(new Uri(fn, UriKind.RelativeOrAbsolute));
//                            Debug.WriteLine("BitmapImage() end");
//                        }
//                        catch (Exception e2)
//                        {
//                            MessageBox.Show("BitmapImage loading error: " + fn);
//                        }
//
//                        Debug.WriteLine("ToImageInfoWith8Bpp() begin");
//                        var imageInfo = bi.ToImageInfoWith8Bpp();
//                        Debug.WriteLine("ToImageInfoWith8Bpp() end");

                    using (var inspectionController = new InspectionController())
                    {
                        inspectionController
                        .SetInspectionSchema(schema.DeepClone())
                        .SetImage(imageInfo3)
                        .CreateCoordinate()
                        .Inspect()
                        ;

                        inspectionController.InspectionResult.Comment = "";

                        inspectionResults.Add(inspectionController.InspectionResult);
                    }

                    //                var targetTask = new SearchingTask();
                    //                foreach (var csd in schema.CircleSearchingDefinitions)
                    //                {
                    //                    var relativeVector = new Vector(csd.BaselineX*1000.0/16.0, csd.BaselineY*1000.0/16.0);
                    //                    var originalVector = coord.GetOriginalVector(relativeVector);
                    //                    csd.CenterX = originalVector.X;
                    //                    csd.CenterY = originalVector.Y;
                    //                }
                    //                targetTask.CircleDefinitions.AddRange(schema.CircleSearchingDefinitions);
                    //
                    //
                    //                var targetResult = inspector.Search(imageInfo, targetTask);
                    //
                    //                targetResult.CircleSearchingResults.UpdateRelativeCoordinate(coord);
                }, imageInfo);
                tasks.Add(task);
                task.Start();
            }

            Task.WaitAll(tasks.ToArray());

            var coordinateResultGroups = inspectionResults.Select(x => x.CoordinateCircles).ToList();
            List <CircleSearchingResultCollection> objectsResultGroups = inspectionResults.Select(x => x.Circles).ToList();

            foreach (var task in objectsResultGroups)
            {
                foreach (var t in task)
                {
                    Debug.WriteLine("objectsResultGroups Circle " + t.Index + " X: " + t.Definition.CenterX);
                    Debug.WriteLine("objectsResultGroups Circle " + t.Index + " Y: " + t.Definition.CenterY);
                }
            }

            DateTime dateTime  = DateTime.Now;
            string   reportDir = "_reports" + dateTime.ToString("_yyyy-MM-dd_HH.mm.ss");
            var      exportDir = Path.Combine(imageDir, reportDir);

            Directory.CreateDirectory(exportDir);

            // SaveToCSV
            ReportManager.SaveToCSV(coordinateResultGroups, exportDir, "Coordinate");
            ReportManager.SaveToCSV(objectsResultGroups, exportDir, "Objects");
            Debug.WriteLine("SaveToCSV() end");


            var distGroups = inspectionResults.Select(x => x.DistanceBetweenPointsResults).ToList();

            ReportManager.SaveCsvGroupByEdge(distGroups, exportDir, "Edges");
            Debug.WriteLine("SaveCsvGroupByEdge() end");


            List <RegionDefectResult> defectsGroups = inspectionResults.SelectMany(x => x.RegionDefectResults).ToList();
            var dgs = defectsGroups.Select(x => x.DefectResults).ToList();

            ReportManager.SaveDefectResultsToCsvGroupByWorkpiece(dgs, exportDir, "Defects");
            Debug.WriteLine("SaveDefectResultsToCsvGroupByWorkpiece() end");

            // SaveToXaml
            ReportManager.SaveToXaml(coordinateResultGroups, exportDir, "Coordinate");
            ReportManager.SaveToXaml(objectsResultGroups, exportDir, "Objects");
            ReportManager.SaveToXaml(inspectionResults, exportDir, "All");
            Debug.WriteLine("SaveToXaml() end");


            var cs = CoordinateCircleCalculator.Calculate(inspectionResults);


            //            var allResultGroup = new List<CircleSearchingResultCollection>();
            //            allResultGroup.AddRange(coordinateResultGroups);
            //            allResultGroup.AddRange(objectsResultGroups);
            //            ReportManager.SaveToCSV(allResultGroup, exportDir, "All");
            //            ReportManager.SaveToXaml(allResultGroup, exportDir, "All");
        }