/// <summary> /// 每固定時間進行畫面取的並進行人臉辨識的動作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void tiCapture_Tick(object sender, EventArgs e) { Image objImage = oWebCam.CaptureImage(); picRender.Image = objImage; // 先取出照片中的人臉與其FaceId var ms = new MemoryStream(); objImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); Face[] faces = null; try { // faces = await face.DetectAsync(ms); HttpResponseMessage response = await new CognitiveUtility().FaceDetect(ms, base.SetupConfig.FaceApiKey); string strContent = await response.Content.ReadAsStringAsync(); faces = JsonConvert.DeserializeObject <Face[]>(strContent); } catch (Exception ex) { string strErr = ex.Message; } // 將照片中的臉,與指定的PersonGroup進行比對 if (faces != null) { Guid[] faceGuids = faces.Select(x => x.FaceId).ToArray(); if (faceGuids.Length > 0) { string strPersonGroup = ((Models.CognitiveModels.ListItem)cbxPersonGroup.SelectedItem).Value; IdentifyResult[] result = await face.IdentifyAsync(strPersonGroup, faceGuids); // 取得照片中的人臉 lbxPerson.Items.Clear(); string strPersonNameLabel = ""; for (int i = 0; i < result.Length; i++) { for (int p = 0; p < result[i].Candidates.Length; p++) { string strPersonId = result[i].Candidates[p].PersonId.ToString(); string strPersonName = (DicPerson.Contains(strPersonId)) ? DicPerson[strPersonId].ToString() : ""; lbxPerson.Items.Add(strPersonName); strPersonNameLabel += strPersonName + ","; } } // 顯示報到文字 if (strPersonNameLabel != "") { strPersonNameLabel = strPersonNameLabel.Substring(0, strPersonNameLabel.Length - 1) + " 報到完成"; lblPersonName.Text = strPersonNameLabel; tiPersonLabel.Enabled = true; plPersonName.Visible = true; int intFontSize = ((plPersonName.Width / 15) > 48) ? 48 : (plPersonName.Width / 15); lblPersonName.Font = new Font("微軟正黑體", intFontSize); } } } }
/// <summary> /// 點選透過WebCam照像的動作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void btnCam_Click(object sender, EventArgs e) { // 打開WebCam oWebCam = new WebCam(); oWebCam.Container = picImage; oWebCam.Container.Height = picImage.Height; oWebCam.Container.Width = picImage.Width; oWebCam.OpenConnection(); string strTime = DateTime.Now.ToString("yyyyMMddHHmmss"); string strFileFolder = ConfigurationManager.AppSettings["FileFolder"].ToString(); string strFileName = strFileFolder + strTime + ".jpg"; Image objImg = oWebCam.CaptureImage(); objImg.Save(strFileName); plTag.BackgroundImage = Bitmap.FromFile(strFileName); Face[] faces = await face.DetectAsync(File.OpenRead(strFileName), true, true); await this.DetectFace(faces); }
/// <summary> /// 點選透過WebCam照像的動作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void btnCam_Click(object sender, EventArgs e) { // 打開WebCam oWebCam = new WebCam(); oWebCam.Container = picImage; oWebCam.Container.Height = picImage.Height; oWebCam.Container.Width = picImage.Width; oWebCam.OpenConnection(); Image img = oWebCam.CaptureImage(); picImage.Image = img; MemoryStream ms = new MemoryStream(); img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); oWebCam.Dispose(); HttpResponseMessage response = await new CognitiveUtility().FaceDetect(ms, base.SetupConfig.FaceApiKey); string strContent = await response.Content.ReadAsStringAsync(); Face[] faces = JsonConvert.DeserializeObject <Face[]>(strContent); await this.DetectFace(faces); }
private async Task GetFaceFromCamAsync() { oWebCam.Container.Height = picRender.Height; oWebCam.Container.Width = picRender.Width; string strTime = DateTime.Now.ToString("yyyyMMddHHmmss"); string strFileFolder = ConfigurationManager.AppSettings["FileFolder"].ToString(); string strFileName = strFileFolder + strTime + ".jpg"; Image objImg = oWebCam.CaptureImage(); objImg.Save(strFileName); plCam.BackgroundImage = Bitmap.FromFile(strFileName); // 先取出照片中的人臉與其FaceId Face[] faces = null; try { Stream st = File.OpenRead(strFileName); faces = await face.DetectAsync(st, true, true); objPlUtil.RenderFaceRectangle(faces); } catch (Exception ex) { string strErr = ex.Message; } // 將照片中的臉,與指定的PersonGroup進行比對 if (faces != null) { Guid[] faceGuids = faces.Select(x => x.FaceId).ToArray(); if (faceGuids.Length > 0) { string strPersonGroup = ((Models.CognitiveModels.ListItem)cbxPersonGroup.SelectedItem).Value; IdentifyResult[] result = await face.IdentifyAsync(strPersonGroup, faceGuids); // 取得照片中的人臉 lbxPerson.Items.Clear(); string strPersonNameLabel = ""; for (int i = 0; i < result.Length; i++) { for (int p = 0; p < result[i].Candidates.Length; p++) { string strPersonId = result[i].Candidates[p].PersonId.ToString(); string strPersonName = (DicPerson.Contains(strPersonId)) ? DicPerson[strPersonId].ToString() : ""; lbxPerson.Items.Add(strPersonName); strPersonNameLabel += strPersonName + ","; } } // 顯示文字 if (strPersonNameLabel != "") { strPersonNameLabel = strPersonNameLabel.Substring(0, strPersonNameLabel.Length - 1); lblPersonName.Text = strPersonNameLabel; tiPersonLabel.Enabled = true; plPersonName.Visible = true; } } } }