private async Task <RichResult> QuickReturn(VisionHelper vision, MsgObject msg)
        {
            var ret = await vision.AnalyzeImage(msg.PicUrl, msg.FromUserName);

            if (ret != null)
            {
                //Response.Write(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{2}]]></Content><DebugInfo><![CDATA[{3}]]></DebugInfo><ErrorInfo><![CDATA[{4}]]></ErrorInfo></xml>",
                //    msg.FromUserName, msg.ToUserName, ret.analyzeImageResult, ret.timeLogs, ret.errorLogs));
                if (ret.errorLogs.Equals(""))
                {
                    // Production mode
                    var returnString = string.Format("{0} 想知道贵图有多火辣么? 请看归图:{1}", ret.analyzeImageResult, ret.uploadedUrl);
                    Response.Write(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{2}]]></Content></xml>",
                                                 msg.FromUserName, msg.ToUserName, returnString));
                    Response.End();
                    return(ret);
                }
                else
                {
                    //Response.Write(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{2}]]></Content></xml>",
                    //msg.FromUserName, msg.ToUserName, "机器人很忙,请稍后再试"));
                    //Response.End();
                    return(ret);
                }
            }
            return(null);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            string subscriptionKey = ConfigurationManager.AppSettings["subscriptionKey"];

            if (string.IsNullOrWhiteSpace(subscriptionKey))
            {
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.WriteLine("To play this sample, you should firstly get a subscription key and put it into the App.Config file.");
                Console.WriteLine("If you don't have one, please access");
                Console.WriteLine("http://www.projectoxford.ai/doc/general/subscription-key-mgmt");

                Console.ResetColor();
                Console.WriteLine();
                Console.WriteLine("Please enter any key......");
                Console.ReadLine();

                return;
            }

            var    vision         = new VisionHelper(subscriptionKey);
            string imagePathorUrl = "http://a2.assets.nationalgeographic.es/soc_photo/10970.600x450.jpg";

            // string imagePathorUrl = "http://www.colins-it.co.uk/blog/wp-content/uploads/2012/08/keep-calm.jpg";
            //var imagePathorUrl = GetValidImagePathorUrl();
            vision.GetThumbnail(imagePathorUrl, 250, 250, true, "C:\\fotos\\");

            Console.ReadLine();
        }
Esempio n. 3
0
        private async Task <string> AnalyzeServerImg()
        {
            var bitmap = await DownloadBitmap($"http://{Program.ServerIp}:5000/api/screenshot", Method.Post, ("url", Url), ("html", "0"));

            {
                var guid = Guid.NewGuid().ToString();
                FilePathFromServer = $@"{CurrentDirectory}\screenshot\{guid}";
            }

            bitmap.Save(FilePathFromServer);
            var fileNameFromServer = FilePathFromServer.Substring(FilePathFromServer.LastIndexOf('\\'));
            var filebytes          = File.ReadAllBytes(FilePathFromServer);

            using (var ms = new MemoryStream(filebytes))
            {
                ScreenShotFromServer = new Bitmap(ms);
                ScreenShotFromServer.SaveJpeg(
                    $@"{CurrentDirectory}\img\{fileNameFromServer}", 50);
                DoneCapturingServer?.Invoke(ScreenShotFromServer, EventArgs.Empty);
            }

            var visionHelper = new VisionHelper("visualFeatures=Categories,Tags,Description,Faces,ImageType,Color&language=en");

            return(await visionHelper.MakeAnalysisRequest <string>(FilePathFromServer));
        }
        private void bCalRotateCenter_Click(object sender, EventArgs e)
        {
            if (fm_SoftwareCliab.FindTemp == null || fm_SoftwareCliab.FindTemp.GetInvocationList().Length <= 0)
            {
                MessageBox.Show("请打开相机轴控界面!!");
                return;
            }

            Task.Factory.StartNew(() => {
                var entiy             = SystemEntiy.Instance[this.module.Module];
                PointF curPos         = entiy.XYPos;
                Nozzle nz             = this.selectNz2.SelectNz;
                PointContour circle   = new PointContour();
                List <PointF> ImagePt = new List <PointF>();

                Camera camera = Camera.Bottom1;
                if (nz == Nozzle.Nz3 || nz == Nozzle.Nz4)
                {
                    camera = Camera.Bottom2;
                }

                // 12 次拟合
                for (int I = 0; I < 6; ++I)
                {
                    entiy.RGoAngleTillStop(I * 60, nz);
                    Thread.Sleep(200);

                    var result = fm_SoftwareCliab.FindTemp.Invoke(this.module.Module, camera);
                    if (result.State == VisionResultState.OK)
                    {
                        ImagePt.Add(new PointF((float)result.Point.X, (float)result.Point.Y));
                    }
                    else
                    {
                        MessageBox.Show("侦测失败!!");
                        return;
                    }
                }
                double x, y, r = 0;

                if (VisionHelper.FitCircle(ImagePt.ToArray(), out x, out y, out r))
                {
                    circle.X = x;
                    circle.Y = y;
                    this.BeginInvoke(new Action(() => {
                        this.tDetectX.Text = x.ToString("f3");
                        this.tDetectY.Text = y.ToString("f3");
                    }));

                    MessageBox.Show("旋转中心检测成功!!");
                }
                else
                {
                    MessageBox.Show("拟合圆失败!!");
                }
            });
        }
Esempio n. 5
0
        private void bCal_Click(object sender, EventArgs e)
        {
            Form_Main.Instance.imageSet.Image.WriteBmpFile("D://1.bmp");
            Thread.Sleep(50);
            double value = 0;

            VisionHelper.GetImageDefinitionValue("D://1.bmp", out value);
            this.tAppend.AppendText($"清晰度:{value} \n");
        }
Esempio n. 6
0
 private void btnFindTemp2_Click(object sender, EventArgs e)
 {
     Variable.CamReturn cam = new Variable.CamReturn();
     if (VisionHelper.FindNccTemplate(Form_Main.Instance.imageSet.Image, Form_Main.Instance.imageSet.Roi, ModelID, 1, 0.6, 0, 0, 1, 1, ref cam))
     {
         VisionHelper.ShowResult(Form_Main.Instance.imageSet, cam);
         btnFindTemp2.BackColor = Color.Transparent;
     }
     else
     {
         btnFindTemp2.BackColor = Color.Red;
     }
 }
        public async Task <ActionResult> AnalyzeHome(string faceUrl = "", string photoName = "")
        {
            string       requestId     = Guid.NewGuid().ToString();
            int?         contentLength = null;
            VisionHelper vision        = new VisionHelper(GetVisionAPIkey(), fontPath, DateTime.Now, fontPath);
            RichResult   res           = null;

            try
            {
                string postString = string.Empty;

                //using (Stream stream = Request.InputStream)
                //{
                //    byte[] postBytes = new byte[stream.Length];
                //    stream.Read(postBytes, 0, (int)stream.Length);
                //    postString = Encoding.Unicode.GetString(postBytes);
                //    return Json(JsonConvert.SerializeObject(postString), "application/json");
                //}

                Stopwatch stopwatch = Stopwatch.StartNew();
                //Trace.WriteLine(string.Format("Start Analyze Request: RequestId: {0};", requestId));
                if (Request.ContentType == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "BadRequest"));
                }
                if (string.Equals(Request.ContentType, "application/octet-stream"))
                {
                    contentLength = Request.ContentLength;
                    Request.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
                    //var img = System.Drawing.Image.FromStream(Request.InputStream);

                    //return Json(JsonConvert.SerializeObject(img.Width), "application/json");
                    res = await vision.AnalyzeImage(Request.InputStream);
                }
                else if (!string.IsNullOrEmpty(faceUrl) && faceUrl != "undefined")
                {
                    res = await vision.AnalyzeImage(faceUrl);
                }


                //Trace.WriteLine(string.Format("Completed Analyze Request: RequestId: {0};", requestId));
                return(Json(JsonConvert.SerializeObject(res), "application/json"));
            }
            catch (Exception e)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, e.Message));
            }
        }
        public string GetImageUrlByAge(int age, Face face)
        {
            //get images with the given age
            try
            {
                string       requestId  = Guid.NewGuid().ToString();
                VisionHelper vision     = new VisionHelper(GetVisionAPIkey(), fontPath, DateTime.Now, fontPath);
                RichResult   res        = null;
                string       postString = string.Empty;

                var list    = MvcApplication.Age2FaceListMap[age];
                var url     = "";
                var urlList = new List <string>();
                var random  = new Random();
                int idx     = 0;
                foreach (var line in list)
                {
                    var gender     = line.Item1;
                    int faceGender = 1; //male
                    if (!face.Attributes.Gender.Equals("male"))
                    {
                        faceGender = 2;
                    }
                    if (gender != faceGender)
                    {
                        continue;
                    }
                    urlList.Add(line.Item5);
                    idx = random.Next(0, urlList.Count);
                    if (idx > 10 - urlList.Count)
                    {
                        return(urlList[idx]);
                    }
                }
                var getrandomIdx = random.Next(0, urlList.Count - 1);
                return(urlList[getrandomIdx]);
            }
            catch (Exception e)
            {
                return(null);//new HttpStatusCodeResult(HttpStatusCode.InternalServerError, e.Message);
            }
        }
Esempio n. 9
0
        private async Task <string> AnalyzePcImg()
        {
            var ps = new Process
            {
                StartInfo = new ProcessStartInfo(
                    $@"{CurrentDirectory}\WebGuard.Supplier.exe")
                {
                    Arguments = $"0 {Url}",
                    RedirectStandardOutput = true,
                    UseShellExecute        = false
                }
            };

            ps.Start();
            var filePath = ps.StandardOutput.ReadLine();

            while (GetProcessesByName("WebGuard.Supplier").Length != 0)
            {
                await Delay(2000);
            }

            if (filePath == null)
            {
                return(null);
            }
            FilePathFromPc = filePath;
            var fileNameFromPc = filePath.Substring(filePath.LastIndexOf('\\'));
            var filebytes      = File.ReadAllBytes(filePath);

            using (var ms = new MemoryStream(filebytes))
            {
                ScreenShotFromPc = new Bitmap(ms);
                ScreenShotFromPc.SaveJpeg(
                    $@"{CurrentDirectory}\img\{fileNameFromPc}", 50);
                DoneCapturingPc?.Invoke(ScreenShotFromPc, EventArgs.Empty);
            }

            var visionHelper = new VisionHelper("visualFeatures=Categories,Tags,Description,Faces,ImageType,Color&language=en");

            return(await visionHelper.MakeAnalysisRequest <string>(FilePathFromPc));
        }
Esempio n. 10
0
        private void bGetK_Click(object sender, EventArgs e)
        {
            this.ChartPaneFront.GraphPane.CurveList.Clear();
            double[] press1 = new double[50];
            double[] press2 = new double[50];

            double[] avgPress1 = new double[5];
            double[] avgPress2 = new double[5];

            for (int i = 0; i < 50; i++)
            {
                press1[i] = double.Parse(this.dGV_Press.Rows[i].Cells[1].Value.ToString());
                press2[i] = double.Parse(this.dGV_Press.Rows[i].Cells[2].Value.ToString());
            }

            LineItem curve = this.ChartPaneFront.GraphPane.AddCurve("", press1, press2, Color.Black, SymbolType.Circle);

            curve.Line.IsVisible = false;
            curve.Symbol.Size    = 15;
            curve.Symbol.Fill    = new Fill(Color.Yellow);
            this.ChartPaneFront.AxisChange();
            this.ChartPaneFront.Invalidate();

            double K = 0;
            double D = 0;
            double R = 0;

            if (VisionHelper.FitLine(press1, press2, out K, out D, out R))
            {
                this.tK.Text = K.ToString("F3");
                this.tD.Text = D.ToString("F3");
                MessageBox.Show($"相关性: {R}");

                for (int i = 0; i < 50; i++)
                {
                    this.dGV_Press.Rows[i].Cells[3].Value = (K * press1[i] + D).ToString("F3");
                }
            }
        }
Esempio n. 11
0
 private void btnStduyTemp2_Click(object sender, EventArgs e)
 {
     VisionHelper.CreatNCCTemplate(Form_Main.Instance.imageSet.Image, Form_Main.Instance.imageSet.Roi, out ModelID);
     Form_Main.Instance.Extract_LearnPttern(Form_Main.Instance.imageSet.Image, image_Edit2.Image);
 }
Esempio n. 12
0
        private void bStartChkCailb_Jig_Click(object sender, EventArgs e)
        {
            if (!bRun)
            {
                this.bStartChkCailb_Jig.Text = "正在测量...";
                ChkPoint_Jig_Act             = new List <PointF>();
                bRun = true;
                double col    = double.Parse(this.textCol_Jig.Text);
                double row    = double.Parse(this.textRow_Jig.Text);
                double rMin   = double.Parse(this.tMinR.Text);
                double rMax   = double.Parse(this.tMaxR.Text);
                double gian   = double.Parse(this.tGainValue.Text);
                double offset = double.Parse(this.tOffsetValue.Text);


                if (!Directory.Exists(Variable.sPath_SYS_Jig))
                {
                    Directory.CreateDirectory(Variable.sPath_SYS_Jig);
                }

                if (File.Exists(Variable.sPath_SYS_Jig + "Pt_Act.txt"))
                {
                    File.Delete(Variable.sPath_SYS_Jig + "Pt_Act.txt");
                }
                if (File.Exists(Variable.sPath_SYS_Jig + "Pt_Machine.txt"))
                {
                    File.Delete(Variable.sPath_SYS_Jig + "Pt_Machine.txt");
                }
                if (Thd_Cailb == null || !Thd_Cailb.IsAlive)
                {
                    Thd_Cailb = new Thread(() =>
                    {
                        Variable.CamReturn cam = new Variable.CamReturn();
                        Form_Main.Instance.StartGrabImage();

                        for (int i = 0; i < chkPoint_Jig.Count; i++)
                        {
                            #region 开始测量
                            try
                            {
                                if (!bRun)
                                {
                                    MessageBox.Show("停止侦测", "提示");
                                    return;
                                }

                                PointF Pt_Act = new PointF();
                                Pt_Act.X      = (float)Math.Floor(i / col);
                                Pt_Act.Y      = (float)(i - col * Pt_Act.X);
                                ChkPoint_Jig_Act.Add(Pt_Act);
                                PointF Pt_Machine = new PointF();
                                Form_Main.Instance.XYGoPosTillStop(20 * 1000, this.chkPoint_Jig[i], variable);
                                // 获得上相机图像
                                VisionImage image = Form_Main.Instance.GrabImage2View(Camera.CAM.Top);
                                image             = Form_Main.Instance.GainOffset(image, gian, offset);
                                Algorithms.Copy(image, Form_Main.Instance.imageSet.Image);
                                PointContour centr = new PointContour();
                                double rad         = 0;
                                if (VisionHelper.DetectCircle(Form_Main.Instance.imageSet.Image, Form_Main.Instance.imageSet.Roi, rMin, rMax, out centr, out rad))
                                {
                                    cam.X    = centr.X;
                                    cam.Y    = centr.Y;
                                    cam.IsOK = true;
                                    VisionHelper.ShowResult(Form_Main.Instance.imageSet, cam);
                                    Pt_Machine = Form_Main.Instance.Point2CCDCenter(this.chkPoint_Jig[i], new PointContour(cam.X, cam.Y), 0, 0);
                                }
                                else
                                {
                                    throw new Exception("寻找模板失败");
                                }
                                //if (VisionHelper.FindNccTemplate(Form_Main.Instance.imageSet.Image, Form_Main.Instance.imageSet.Roi, ModelID, 1, 0.5, 0, 0, 1, 1, ref cam))
                                //{
                                //    VisionHelper.ShowResult(Form_Main.Instance.imageSet, cam);
                                //    Pt_Machine = Form_Main.Instance.Point2CCDCenter(this.chkPoint_Jig[i], new PointContour(cam.X, cam.Y), 0);
                                //}
                                //else
                                //{
                                //    throw new Exception("寻找模板失败");
                                //}

                                ChkPoint_Jig_Act.Add(Pt_Machine);

                                StreamWriter sw = File.AppendText(Variable.sPath_SYS_Jig + "Pt_Act.txt");
                                sw.Write(Pt_Act.X.ToString("F3") + "," + Pt_Act.Y.ToString("F3") + "\r\n");
                                sw.Close();
                                StreamWriter sw1 = File.AppendText(Variable.sPath_SYS_Jig + "Pt_Machine.txt");
                                sw1.Write(Pt_Machine.X.ToString("F3") + "," + Pt_Machine.Y.ToString("F3") + "\r\n");
                                sw1.Close();
                            }
                            catch (Exception ex)
                            {
                                if (MessageBox.Show($"侦测失败:{ex.Message} 是否重新识别 Y/N ", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                {
                                    i--;
                                    continue;
                                }
                                else
                                {
                                    MessageBox.Show("玻璃杯校验停止!!!");
                                    bRun = false;
                                    return;
                                }
                            }
                            #endregion
                        }
                    });

                    Thd_Cailb.Start();
                }
            }
            else
            {
                if (MessageBox.Show("是否停止测量", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    this.bStartChkCailb_Jig.Text = "开始测量";
                    bRun = false;
                }
            }
        }
        public async Task <ActionResult> AnalyzeOneImage(string faceUrl = "", string photoName = "")
        {
            string requestId = Guid.NewGuid().ToString();
            //int? contentLength = null;
            VisionHelper vision = new VisionHelper(GetVisionAPIkey(), fontPath, DateTime.Now, fontPath);
            RichResult   res    = null;

            try
            {
                string postString = string.Empty;

                //using (Stream stream = Request.InputStream)
                //{
                //    byte[] postBytes = new byte[stream.Length];
                //    stream.Read(postBytes, 0, (int)stream.Length);
                //    postString = Encoding.Unicode.GetString(postBytes);
                //    return Json(JsonConvert.SerializeObject(postString), "application/json");
                //}

                Stopwatch stopwatch = Stopwatch.StartNew();
                //Trace.WriteLine(string.Format("Start Analyze Request: RequestId: {0};", requestId));
                //if (Request.Content.GetType() == null)
                //{
                //    return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "BadRequest");

                //}

                if (!string.IsNullOrEmpty(faceUrl) && faceUrl != "undefined")
                {
                    res = await vision.AnalyzeImage(faceUrl);
                }
                else
                {
                    //if (string.Equals(Request.Headers.GetType(), "application/octet-stream"))
                    //{
                    // contentLength = Request.Content.;
                    //contentLength = Request.ContentLength;
                    //var stream = new MemoryStream();
                    //await Request.Content.CopyToAsync(stream);
                    //stream.Seek(0, System.IO.SeekOrigin.Begin);
                    res = await vision.AnalyzeImage(Request.InputStream);
                }

                if (res.analysisResult.RichFaces.Length > 0)
                {
                    res.stepSize = 5;
                    res.minAge   = res.stepSize * (int)((int)res.analysisResult.RichFaces[0].Attributes.Age / (float)res.stepSize);
                    res.maxAge   = 90;

                    var urls = new List <string>();
                    for (var age = res.minAge; age <= res.maxAge; age += res.stepSize)
                    {
                        var url = GetImageUrlByAge(age, res.analysisResult.RichFaces[0]);
                        urls.Add(!string.IsNullOrEmpty(url) ? url : "");
                    }
                    res.agingImgUrls = urls.ToArray();
                }
                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                return(Json(JsonConvert.SerializeObject(res), "application/json"));
            }
            catch (Exception e)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, e.Message));
            }
        }
Esempio n. 14
0
        private void bAutoCal_Click(object sender, EventArgs e)
        {
            this.bAutoCal.Enabled = false;
            Count = 0;
            double last  = 0;
            double value = 0;

            Variable.VelMode vel = new Variable.VelMode(0, 100, 300, 300);
            AutoTimer.Start();
            int    JumpCount = 0;
            double MoveTirm  = 1;
            double MaxValue  = 0;
            double MaxZ      = 0;
            bool   first     = true;

            if (this.cb_SelectNz.SelectedIndex >= 0)
            {
                Form_Main.Instance.imageSet.Image.WriteBmpFile("D://1.bmp");
                int Dir = 1; // 正向寻找

                Thread.Sleep(100);
                Application.DoEvents();
                VisionHelper.GetImageDefinitionValue("D://1.bmp", out last);
                Form_Main.Instance.Z_RunParamMap[(uint)this.cb_SelectNz.SelectedIndex].MoveTrim(Dir * MoveTirm, vel);
                MaxValue = last;
                MaxZ     = Form_Main.Instance.Z_RunParamMap[(uint)this.cb_SelectNz.SelectedIndex].Pos;
                while (Count < 120)
                {
                    Thread.Sleep(100);
                    Application.DoEvents();
                    Form_Main.Instance.imageSet.Image.WriteBmpFile("D://1.bmp");

                    Thread.Sleep(100);
                    Application.DoEvents();
                    VisionHelper.GetImageDefinitionValue("D://1.bmp", out value);

                    if (value > MaxValue)
                    {
                        MaxValue = value;
                        MaxZ     = Form_Main.Instance.Z_RunParamMap[(uint)this.cb_SelectNz.SelectedIndex].Pos;
                    }

                    if (value > last)
                    {
                        Form_Main.Instance.Z_RunParamMap[(uint)this.cb_SelectNz.SelectedIndex].MoveTrim(Dir * MoveTirm, vel);
                        Thread.Sleep(100);
                    }
                    else
                    {
                        Dir = -1 * Dir;

                        if (!first)
                        {
                            JumpCount++;
                            if (JumpCount > 3)
                            {
                                JumpCount = 0;
                                MoveTirm  = MoveTirm / 2;
                                if (MoveTirm < 0.1)
                                {
                                    // Find OK
                                    break;
                                }
                            }
                        }
                        first = false;
                    }

                    last = value;
                    this.tAppend.AppendText($"清晰度:{value} \n");
                }

                Thread.Sleep(100);
                Form_Main.Instance.Z_RunParamMap[(uint)this.cb_SelectNz.SelectedIndex].StopAxis();
                Form_Main.Instance.Z_RunParamMap[(uint)this.cb_SelectNz.SelectedIndex].CleSts();
                Form_Main.Instance.Z_RunParamMap[(uint)this.cb_SelectNz.SelectedIndex].GoPos(MaxZ, vel);
            }

            AutoTimer.Stop();

            this.bAutoCal.Enabled = true;
        }
        public override VisionResult Detected(VisionImage image, Dictionary <string, VisionResult> Result = null, VisionFlow parent = null, Shape newRoi = null)
        {
            VisionResult rtn = new VisionResult();

            rtn.State = VisionResultState.WaitCal;
            try
            {
                using (HImage hImage = VisionHelper.Image(image))
                {
                    var roi = this.ROI;
                    if (newRoi != null)
                    {
                        roi = newRoi;
                    }
                    RectangleContour rect = roi as RectangleContour;
                    using (HImage reduceImage = hImage.ReduceDomain(
                               new HRegion(rect.Top, rect.Left,
                                           rect.Top + rect.Height,
                                           rect.Left + rect.Width)))
                    {
                        HRegion hRegion = new HRegion();
                        hRegion = reduceImage.Threshold(this.MinThreshold, this.MaxThreshold);

                        rtn.Area = hRegion.Area.D;
                        this.AddVisionResc(rtn, $"面积侦测成功 面积值:{rtn.Area:N3}");
                        rtn.State = VisionResultState.OK;
                    }
                }

                //using (VisionImage imageMask = new VisionImage(ImageType.U8, 7))
                //{
                //    using (VisionImage image2Process = new VisionImage(ImageType.U8, 7))
                //    {
                //        //Algorithms.Copy(image, image2Process);
                //        Algorithms.Threshold(image, image2Process, new Range(this.Threshold, 255), true, 255);
                //        PixelValue fillValue = new PixelValue(255);
                //        Range intervalRange = new Range(0, 0);

                //        var roi = this.ROI;
                //        if (newRoi != null)
                //            roi = newRoi;

                //        Algorithms.RoiToMask(imageMask, roi.ConvertToRoi(), fillValue, image2Process);
                //        HistogramReport a = new HistogramReport();
                //        a = Algorithms.Histogram(image2Process, 256, intervalRange, imageMask);

                //        image2Process?.Dispose();
                //        imageMask?.Dispose();
                //        if (a.Histogram.Count >= 256)
                //        {
                //            if (CheckWhite)
                //                rtn.Area = a.Histogram[255];//白色区域面积
                //            else
                //                rtn.Area = a.Histogram[0];//黑色区域面积
                //        }

                //        this.AddVisionResc(rtn, $"面积:{rtn.Area}");
                //        rtn.State = VisionResultState.OK;
                //    }
                //}
            }
            catch (VisionException ex)
            {
                this.AddVisionResc(rtn, ex.Message);

                rtn.State = VisionResultState.NG;
            }

            return(rtn);
        }
        public async Task <HttpResponseMessage> Analyze(string faceUrl = "", string photoName = "")
        {
            string requestId = Guid.NewGuid().ToString();
            //int? contentLength = null;
            VisionHelper vision = new VisionHelper(GetVisionAPIkey(), fontPath, DateTime.Now, fontPath);
            RichResult   res    = null;

            try
            {
                string postString = string.Empty;

                //using (Stream stream = Request.InputStream)
                //{
                //    byte[] postBytes = new byte[stream.Length];
                //    stream.Read(postBytes, 0, (int)stream.Length);
                //    postString = Encoding.Unicode.GetString(postBytes);
                //    return Json(JsonConvert.SerializeObject(postString), "application/json");
                //}

                Stopwatch stopwatch = Stopwatch.StartNew();
                //Trace.WriteLine(string.Format("Start Analyze Request: RequestId: {0};", requestId));
                //if (Request.Content.GetType() == null)
                //{
                //    return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "BadRequest");

                //}

                if (!string.IsNullOrEmpty(faceUrl) && faceUrl != "undefined")
                {
                    res = await vision.AnalyzeImage(faceUrl);
                }
                else
                {
                    //if (string.Equals(Request.Headers.GetType(), "application/octet-stream"))
                    //{
                    // contentLength = Request.Content.;
                    //contentLength = Request.ContentLength;
                    var stream = new MemoryStream();
                    await Request.Content.CopyToAsync(stream);

                    stream.Seek(0, System.IO.SeekOrigin.Begin);
                    //var img = System.Drawing.Image.FromStream(Request.InputStream);
                    //return Json(JsonConvert.SerializeObject(img.Width), "application/json");
                    res = await vision.AnalyzeImage(stream);
                }



                //Trace.WriteLine(string.Format("Completed Analyze Request: RequestId: {0};", requestId));
                //return Json(JsonConvert.SerializeObject(res),(Newtonsoft.Json.JsonSerializerSettings) "application/json", System.Text.Encoding.UTF8);


                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                //return Json(JsonConvert.SerializeObject(Json(JsonConvert.SerializeObject(res)).Content));
                result.Content = new StringContent(JsonConvert.SerializeObject(Json(JsonConvert.SerializeObject(res)).Content));//new StringContent(JsonConvert.SerializeObject(res));
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                return(result);

                //Error	20	Argument 2: cannot convert from 'string' to 'Newtonsoft.Json.JsonSerializerSettings'
            }
            catch (Exception e)
            {
                return(null);//new HttpStatusCodeResult(HttpStatusCode.InternalServerError, e.Message);
            }
        }
        public override VisionResult Detected(VisionImage image, Dictionary <string, VisionResult> Result = null, VisionFlow parent = null, Shape newRoi = null)
        {
            VisionResult rtn = new VisionResult();

            rtn.State = VisionResultState.WaitCal;

            try
            {
                lock (locked)
                {
                    using (VisionImage temp = new VisionImage())
                    {
                        var roi = this.ROI;
                        if (newRoi != null)
                        {
                            roi = newRoi;
                        }

                        using (HImage hImage = VisionHelper.Image(image))
                        {
                            RectangleContour rect = roi as RectangleContour;

                            using (HImage reduceImage = hImage.ReduceDomain(
                                       new HRegion(rect.Top, rect.Left,
                                                   rect.Top + rect.Height,
                                                   rect.Left + rect.Width)))
                            {
                                HTuple numLevels = new HTuple();
                                numLevels.TupleAdd(this.MaxNumLevels);
                                numLevels.TupleAdd(this.MinNumLevels);

                                HTuple row, col, scaleX, scaleY, score, angle;
                                reduceImage.FindAnisoShapeModel(this.ShapeModel,
                                                                this.StartAngle, this.EndAngle - this.StartAngle,
                                                                this.YMinScale, this.YMaxScale,
                                                                this.XMinScale, this.XMaxScale,
                                                                this.MinScore, 1, this.MaxOverlap,
                                                                this.Pixel.ToString(),
                                                                0,
                                                                this.Greediness,
                                                                out row, out col, out angle, out scaleY, out scaleX, out score);

                                if (score.Length > 0)
                                {
                                    rtn.State = VisionResultState.OK;
                                    image.Overlays.Default.AddLine
                                        (new LineContour(new PointContour(col.D - 50, row.D), new PointContour(col.D + 50, row.D)), Rgb32Value.RedColor);

                                    image.Overlays.Default.AddLine
                                        (new LineContour(new PointContour(col.D, row.D - 50), new PointContour(col.D, row.D + 50)), Rgb32Value.RedColor);
                                    rtn.Point = new PointContour(col.D, row.D);
                                    rtn.Angle = angle.TupleDeg().D;
                                    this.AddVisionResc(rtn, $"轮廓匹配成功 角度:{rtn.Angle:N3} 分数:{score[0].D:N3}");
                                }
                                else
                                {
                                    rtn.State = VisionResultState.NG;
                                    this.AddVisionResc(rtn, $"没有找到匹配模板");
                                }
                            }
                        }
                    }
                }
            }
            catch (HalconException ex)
            {
                this.AddVisionResc(rtn, $"轮廓匹配失败 {ex.GetErrorMessage()}");
            }
            return(rtn);
        }
        public override VisionResult Detected(VisionImage image, Dictionary <string, VisionResult> Result = null, VisionFlow parent = null, Shape newRoi = null)
        {
            VisionResult rtn = new VisionResult();

            rtn.State = VisionResultState.WaitCal;
            if (ROI == null || image == null)
            {
                rtn.State = VisionResultState.NG;
                return(rtn);
            }

            try
            {
                using (VisionImage temp = new VisionImage())
                {
                    var roi = this.ROI;
                    if (newRoi != null)
                    {
                        roi = newRoi;
                    }

                    using (HImage hImage = VisionHelper.Image(image))
                    {
                        RectangleContour rect = roi as RectangleContour;
                        using (HImage reduceImage = hImage.ReduceDomain(
                                   new HRegion(rect.Top, rect.Left,
                                               rect.Top + rect.Height,
                                               rect.Left + rect.Width)))
                        {
                            string code = string.Empty;
                            switch (CodeType)
                            {
                            case CodeType.Code_2D_Mat:
                                code = "Data Matrix ECC 200";
                                break;

                            case CodeType.Code_QR:
                                code = "QR Code";
                                break;
                            }

                            using (HDataCode2D code2D = new HDataCode2D(code, "default_parameters", "maximum_recognition"))
                            {
                                HTuple result       = new HTuple();
                                HTuple resultString = new HTuple();
                                code2D.FindDataCode2d(reduceImage, "stop_after_result_num", 1, out result, out resultString);
                                if (resultString.Length > 0)
                                {
                                    rtn.BarCode = resultString.SArr[0];
                                    this.AddVisionResc(rtn, $"条码寻找成功:{rtn.BarCode}");
                                    rtn.State = VisionResultState.OK;
                                    image.Overlays.Default.AddText(rtn.BarCode, new PointContour(rect.Left, rect.Top), Rgb32Value.BlueColor, new OverlayTextOptions("Consolas", 128));
                                }
                                else
                                {
                                    rtn.State = VisionResultState.NG;
                                    this.AddVisionResc(rtn, $"没有找到条码");
                                }
                            }
                        }
                    }
                }
            }
            catch (HalconException ex)
            {
                this.AddVisionResc(rtn, $"没有找到条码 {ex.GetErrorMessage()}");
            }
            return(rtn);
        }
        private async Task <bool> ProcessMsg(string xml, bool returnImage)
        {
            MsgObject msg = new MsgObject(xml);

            if (msg.MsgType != "image")
            {
                string resString = "请点+号输入一张人物风景照片试试";
                Response.Write(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{2}]]></Content></xml>",
                                             msg.FromUserName, msg.ToUserName, resString));
                Response.End();
                return(false);
            }

            bool isDebug = false;

            if (isDebug)
            {
                string debugString = msg.PicUrl;
                Response.Write(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{2}]]></Content></xml>",
                                             msg.FromUserName, msg.ToUserName, msg.PicUrl));
                Response.End();
                return(false);
            }



            //string imagePathorUrl = msg.PicUrl;
            //string imagePathorUrl = msg.PicUrl.Replace("https://", "").Replace("http://", "");
            //var ret = vision.AnalyzeImage(msg.PicUrl);
            RichResult ret = null;
            //ret = vision.AnalyzeImage(msg.PicUrl);

            VisionHelper vision = new VisionHelper(GetVisionAPIkey(), fontPath, DateTime.Now, fontPath);


            var task = QuickReturn(vision, msg);

            //md5 = GetMd5(msg.PicUrl);
            md5 = GetMd5(msg.PicUrl + msg.CreateTime.ToString());
            //check data from db
            using (var dbContext = new WeixinDBContext())
            {
                //ImageStorage image = dbContext.ImageStorages.FirstOrDefault(p => p.OpenId == msg.FromUserName && p.PicUrl == msg.PicUrl && p.CreateTime == msg.CreateTime);
                //ImageStorage image = dbContext.ImageStorages.FirstOrDefault(p => p.PicUrl == msg.PicUrl);
                //ImageStorage image = dbContext.ImageStorages.FirstOrDefault(p => p.PicUrl == msg.PicUrl && p.CreateTime == msg.CreateTime);

                //ImageStorage image = dbContext.ImageStorages.FirstOrDefault(p => p.Md5 == md5 && p.CreateTime == msg.CreateTime);
                ImageStorage image = dbContext.ImageStorages.FirstOrDefault(p => p.Md5 == md5);
                if (image != null)
                {
                    Response.Write(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{2}]]></Content><DebugInfo><![CDATA[{3}]]></DebugInfo><ErrorInfo><![CDATA[{4}]]></ErrorInfo></xml>",
                                                 msg.FromUserName, msg.ToUserName, image.ParsedDescription, image.TimeLog, md5));
                    Response.End();
                    return(true);
                }
                dbContext.Dispose();
            }



            ret = await task;

            //when not results in DB and got error
            if (!ret.errorLogs.Equals(""))
            {
                //Response.Write(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{2}]]></Content></xml>",
                //        msg.FromUserName, msg.ToUserName, "机器人很忙,请稍后再试"));
                Response.Write(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{2}]]></Content><DebugInfo><![CDATA[{3}]]></DebugInfo><ErrorInfo><![CDATA[{4}]]></ErrorInfo></xml>",
                                             msg.FromUserName, msg.ToUserName, ret.analyzeImageResult, ret.timeLogs, ret.errorLogs));
                Response.End();
            }
            //// Debug mode
            //Response.Write(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{2}]]></Content><DebugInfo><![CDATA[{3}]]></DebugInfo><ErrorInfo><![CDATA[{4}]]></ErrorInfo></xml>",
            //    msg.FromUserName, msg.ToUserName, ret.analyzeImageResult, ret.timeLogs, ret.errorLogs));

            // Production mode
            //Response.Write(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{2}]]></Content></xml>", msg.FromUserName, msg.ToUserName, ret.analyzeImageResult));

            //Response.End();


            //save db first without parsedContent
            int id = -1;

            using (var dbContext = new WeixinDBContext())
            {
                ImageStorage image = new ImageStorage();
                image.OpenId     = msg.FromUserName;
                image.CreateTime = msg.CreateTime;
                image.PicUrl     = msg.PicUrl;
                image.Md5        = GetMd5(msg.PicUrl + msg.CreateTime.ToString() + ret.errorLogs);
                //image.PicContent = ret.rawImage;
                image.ParsedUrl         = ret.uploadedUrl;
                image.ParsedDescription = ret.analyzeImageResult + ret.errorLogs;
                image.TimeLog           = ret.timeLogs;
                dbContext.ImageStorages.Add(image);
                dbContext.SaveChanges();
                id = image.Id;
                dbContext.Dispose();
            }

            //write to DB
            //var webClient = new WebClient();
            //var processedImageBytes = webClient.DownloadData(ret.uploadedUrl);

            //using (var dbContext = new WeixinDBContext())
            //{
            //    ImageStorage image = dbContext.ImageStorages.FirstOrDefault(p => p.Id == id);
            //    if (image != null)
            //    {
            //        image.ParsedContent = processedImageBytes;
            //        dbContext.SaveChanges();
            //    }
            //}

            return(true);
        }