コード例 #1
0
        public void TestImage(ref HWindowControl hv_HWindowControl)
        {
            if (hv_Test_DlClassifierName == null)
            {
                Console.WriteLine("错误提示:无可用分类器");
                return;
            }
            if (ho_Preprocessed_TestImage == null)
            {
                Console.WriteLine("图像未进行预处理");
                return;
            }

            HObject ho_ImagePreprocessed        = new HObject();
            HTuple  hv_DLClassifierResultHandle = new HTuple();


            HTuple hv_Exception = new HTuple();
            HTuple hv_Text      = new HTuple();

            hv_Test_DLClassifierHandle.Dispose();
            HOperatorSet.ReadDlClassifier(hv_Test_DlClassifierName, out hv_Test_DLClassifierHandle);

            //If it is not possible to accumulate more than one image at a time the batch size should be set to 1.
            HOperatorSet.SetDlClassifierParam(hv_Test_DLClassifierHandle, "batch_size", 1);

            //Set the runtime to 'cpu' to perform the inference on the CPU, if this is possible on the current hardware.
            //(see the system requirements in the Installation Guide)
            try
            {
                HOperatorSet.SetDlClassifierParam(hv_Test_DLClassifierHandle, "runtime", "cpu");
                hv_Runtime.Dispose();
                hv_Runtime = "cpu";
            }
            // catch (Exception)
            catch (HalconException HDevExpDefaultException1)
            {
                HDevExpDefaultException1.ToHTuple(out hv_Exception);
                //Keep the 'gpu' runtime if switching to 'cpu' failed.
                hv_Runtime.Dispose();
                hv_Runtime = "gpu";
            }
            //This initializes the runtime environment immediately.
            HOperatorSet.SetDlClassifierParam(hv_Test_DLClassifierHandle, "runtime_init", "immediately");
            //
            //dev_disp_inference_text(hv_Runtime, hv_WindowHandle);
            // stop(...); only in hdevelop
            ho_ImagePreprocessed.Dispose();

            preprocess_dl_classifier_images(this.ho_TestImage, out ho_ImagePreprocessed, new HTuple(),
                                            new HTuple(), hv_Test_DLClassifierHandle);

            hv_DLClassifierResultHandle.Dispose();
            HOperatorSet.ApplyDlClassifier(ho_ImagePreprocessed, hv_Test_DLClassifierHandle,
                                           out hv_DLClassifierResultHandle);
            hv_Inference_PredictedClass.Dispose();
            HOperatorSet.GetDlClassifierResult(hv_DLClassifierResultHandle, "all", "predicted_classes",
                                               out hv_Inference_PredictedClass);
            HOperatorSet.GetDlClassifierResult(hv_DLClassifierResultHandle, "all", "confidences", out hv_Inference_Confidences);
            //
            DispImageAdaptively(ref hv_HWindowControl, this.ho_TestImage);
            hv_Text.Dispose();
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                hv_Text = "Predicted class: " + hv_Inference_PredictedClass + ";  Confidence: " + hv_Inference_Confidences;
            }
            HOperatorSet.DispText(hv_HWindowControl.HalconWindow, hv_Text, "window", "top", "left", "red", "box", "true");

            HOperatorSet.DispText(hv_HWindowControl.HalconWindow, "Click to continue", "window", "bottom", "right", "black", new HTuple(), new HTuple());
            this.ho_TestImage = null;
        }