예제 #1
0
        private void StartButton_OnClick(object sender, RoutedEventArgs e)
        {
            Save();

            var ret = MessageBox.Show("Are you sure to start? ", "Warning", MessageBoxButton.YesNo);

            if (ret != MessageBoxResult.Yes)
            {
                return;
            }



            List <string> workedDir = new List <string>();

            foreach (var directory in Directories)
            {
                if (string.IsNullOrEmpty(directory.DirectoryPath))
                {
                    //MessageBox.Show("ImageDir is null: " + directory.DirectoryPath);
                    continue;
                }

                workedDir.Add(directory.DirectoryPath);

                // Init
                InspectionSchema schema = null;

                try
                {
                    if (!File.Exists(InspectionSchemaTextBox.Text))
                    {
                        MessageBox.Show("InspectionSchema file is not exist!");
                        return;
                    }

                    //                schema = InspectionSchemaTextBox.Text.LoadFromFile();
                    //schema = InspectionSchemaExtensions.LoadFromFile(InspectionSchemaTextBox.Text);
                    schema = InspectionController.GetInspectionSchema();
                }
                catch (Exception exception)
                {
                    MessageBox.Show("InspectionSchema loading error!");
                    return;
                }

                Run(directory.DirectoryPath, schema.DeepClone());
            }

            string dirs = string.Empty;

            foreach (var dir in workedDir)
            {
                dirs += dir + "\n";
            }

            MessageBox.Show("Directories exported:\n\n" + dirs);
        }
예제 #2
0
        public async Task <int> AcquisitionAndInspectAsync(int surfaceTypeIndex)
        {
            // Acquisition
            Task.Run(() => _acquisitionStartedEvent.OnNext(surfaceTypeIndex));
            var imageInfo = await _camera.AcquisitionAsync();

            switch (surfaceTypeIndex)
            {
            case 0:
                Machine.Inspection_SurfaceFront_GrabFinishedPcEventDevice.WriteTrue();
                break;

            case 1:
                Machine.Inspection_SurfaceBack_GrabFinishedPcEventDevice.WriteTrue();
                break;
            }

            Task.Run(() =>
            {
                _acquisitionCompletedEvent.OnNext(imageInfo.ToEntity());
                _calibrationStartedEvent.OnNext(surfaceTypeIndex);
            });

            // Calibration

            HImage calibImage;

            if (MachineConfig.MV_SimulationCalibrationEnabled)
            {
                calibImage = imageInfo.To8BppHImage();
            }
            else
            {
                var    swOriToHImageInfo = new NotifyStopwatch("imageInfo.To8BppHImage()");
                HImage oriImage          = imageInfo.To8BppHImage();
                swOriToHImageInfo.Dispose();

                //                HImage reducedImage = oriImage.Rectangle1Domain(2000, 2000, 5000, 5000);

                var calib   = new HalconImageCalibrator();
                var swCalib = new NotifyStopwatch("AcquisitionAndInspectAsync.CalibrateImage(imageInfo)");
                calibImage = calib.CalibrateImage(oriImage, MachineConfig.MV_CalibrationInterpolation);
                swCalib.Dispose();

                oriImage.Dispose();
            }

            var swCalibToImageInfo = new NotifyStopwatch("calibImage.ToImageInfo()");
            var calibImageInfo     = calibImage.ToImageInfo();

            swCalibToImageInfo.Dispose();

            calibImageInfo.SurfaceTypeIndex = surfaceTypeIndex;

            Debug.WriteLine("SurfaceType " + surfaceTypeIndex + ": StartGrabAsync() Finished");

            Task.Run(() =>
            {
                _calibrationCompletedEvent.OnNext(calibImageInfo.ToEntity());
                _inspectionStartedEvent.OnNext(surfaceTypeIndex);
            });

            Task.Run(
                () =>
            {
                InspectInfo inspectionInfo;
                if (MachineConfigProvider.MachineConfig.MV_SimulationInspectorEnabled)
                {
                    inspectionInfo = new InspectInfo();
                    calibImage.Dispose();
                }
                else
                {
                    InspectionResult inspectionResult;

                    try
                    {
                        Debug.WriteLine("_inspectionController.Inspect() start");
                        var sw3 = new NotifyStopwatch("_inspectionController.Inspect()");
                        _inspectionController
                        .SetInspectionSchema(_inspectionSchema.DeepClone())
                        .SetImage(calibImage)
                        .CreateCoordinate()
                        .Inspect();
                        Debug.WriteLine("_inspectionController.Inspect() OK");
                        sw3.Dispose();
                        calibImage.Dispose();
                        inspectionResult = _inspectionController.InspectionResult;
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("_inspectionController error.\n\n" + e.Message + "\n\n" +
                                        e.InnerException.Message);
                        Debug.WriteLine("_inspectionController error.\n\n" + e.Message + "\n\n" +
                                        e.InnerException.Message);

                        inspectionResult = new InspectionResult();
                    }

                    Debug.WriteLine("_inspectionController.GetInspectionInfo() Start");
                    inspectionInfo = inspectionResult.GetInspectionInfo(_inspectionController.Coordinate);
                    Debug.WriteLine("_inspectionController.GetInspectionInfo() OK");
                }

                var surfaceInspectInfo = new SurfaceInspectInfo()
                {
                    SurfaceTypeIndex = surfaceTypeIndex,
                    ImageInfo        = calibImageInfo.ToEntity(),
                    InspectInfo      = inspectionInfo,
                    WorkpieceIndex   =
                        _inspectCounter /
                        MachineConfigProvider.MachineConfig
                        .MV_AcquisitionCountPerWorkpiece
                };

                Task.Run(() => _inspectionCompletedEvent.OnNext(surfaceInspectInfo));

                Debug.WriteLine("AcquisitionAndInspectAsync.HandleInspect() above");
                HandleInspect(surfaceInspectInfo);
            });

            return(calibImageInfo.Index);
        }
예제 #3
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");
        }