private void UpdateFormatList() { _pendingFormat = null; isUpdatingFormatList = true; cbFormats.BeginUpdate(); cbFormats.Items.Clear(); try { if (_camera != null) { foreach (NMediaFormat format in _camera.GetFormats()) { cbFormats.Items.Add(format); } NMediaFormat currentFormat = _camera.GetCurrentFormat(); if (currentFormat != null) { int index = cbFormats.Items.IndexOf(currentFormat); if (index == -1) { cbFormats.Items.Add(currentFormat); } cbFormats.SelectedItem = currentFormat; } } } finally { cbFormats.EndUpdate(); cbFormats.Enabled = cbFormats.Items.Count > 0; isUpdatingFormatList = false; } }
private void capture() { if (System.Configuration.ConfigurationManager.AppSettings["Verify"] != "file") { if (_camera == null) { LogLine("No camera set", true); return; } try { if (_pendingFormat != null) { _camera.SetCurrentFormat(_pendingFormat); _pendingFormat = null; } _camera.StartCapturing(); UpdateFormatList(); startCapturing(); } catch (Exception ex) { ShowError(ex.Message); } } else { enrollFromImage(true); // to view2 score = Identify(_capturedTemplateList, _enrolledTemplateList); LogLine(string.Format("Face match details: score {0}.", score), true); backgroundWorker_RunWorkerCompleted(null, null); } }
private void cbFormats_SelectedIndexChanged(object sender, EventArgs e) { if (isUpdatingFormatList) { return; } _pendingFormat = cbFormats.SelectedItem as NVideoFormat; }
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { bool extractionStarted = false; try { NImage frame = null; NGrayscaleImage grayscaleImage = null; int frameNumber = 0; int bestFrame; int frameCount = Tools.LiveEnrolFrameCount; _extractor.DetectAllFeaturePoints = false; while (backgroundWorker.CancellationPending == false) { if (_pendingFormat != null && _fromCamera) { _camera.SetCurrentFormat(_pendingFormat); _pendingFormat = null; } if (!_fromCamera && _pause) { System.Threading.Thread.Sleep(500); continue; } try { TimeSpan duration = TimeSpan.Zero; TimeSpan timeStamp = TimeSpan.Zero; if (_fromCamera) { frame = _camera.GetFrame(); } else { lock (_readerLock) { frame = _videoReader.ReadVideoSample(out timeStamp, out duration); } } if (frame == null) //camera unplugged or end of file { createFaceRecord = false; SetImageToView(null, null, null, NleExtractionStatus.None, -1, timeStamp); return; } using (grayscaleImage = frame.ToGrayscale()) { if (createFaceRecord) { NleDetectionDetails details; NLTemplate template = null; //NBuffer template = null; if (!extractionStarted) { UpdateExtractorTemplateSize(); frameCount = Tools.LiveEnrolFrameCount; _extractor.ExtractStart(); extractionStarted = true; frameNumber = 0; ClearCapturedImages(); } frameNumber++; NleExtractionStatus status = _extractor.ExtractNext(grayscaleImage, out details); capturedImages.Add((NImage)frame.Clone()); if (status != NleExtractionStatus.None || frameNumber >= frameCount) { template = _extractor.ExtractEnd(out bestFrame, out status); if (status == NleExtractionStatus.TemplateCreated) { NTemplate nTemplate = new NTemplate(); NImage bestImage = frame; if (bestFrame < capturedImages.Count && bestFrame >= 0) { bestImage = capturedImages[bestFrame]; } _newRecord = new FaceRecord(template, bestImage, details); _newRecord.AddToTemplate(nTemplate); template.Dispose(); capturedImages.Remove(bestImage); _capturedTemplateList = new List <byte[]>(); _capturedTemplateList.Add(nTemplate.Save().ToByteArray()); score = Identify(_capturedTemplateList, _enrolledTemplateList); LogLine(string.Format("Face match details: score {0}.", score), true); backgroundWorker.CancelAsync(); } else { _newRecord = null; } extractionStarted = false; createFaceRecord = false; } if (!createFaceRecord) { ClearCapturedImages(); } SetImageToView(nlView2, frame.ToBitmap(), new NleDetectionDetails[] { details }, status, (int)(frameNumber * 100.0 / frameCount), timeStamp); if (status != NleExtractionStatus.None && status != NleExtractionStatus.TemplateCreated) { backgroundWorker.CancelAsync(); score = 0; } } else { NleDetectionDetails[] details = null; try { NleFace[] faces = _extractor.DetectFaces(grayscaleImage); if (faces != null) { details = new NleDetectionDetails[faces.Length]; for (int i = 0; i < faces.Length; i++) { details[i] = _extractor.DetectFacialFeatures(grayscaleImage, faces[i]); } } } finally { SetImageToView(nlView2, frame.ToBitmap(), details, NleExtractionStatus.None, -1, timeStamp); } } } //using } // try finally { if (frame != null) { frame.Dispose(); } } }// while } catch (Exception ex) { foreach (NImage img in capturedImages) { img.Dispose(); } capturedImages.Clear(); ShowError(ex.Message); } finally { try { int baseFrameIndex; NleExtractionStatus status; if (extractionStarted) { _extractor.ExtractEnd(out baseFrameIndex, out status); } if (_fromCamera && _camera != null) { _camera.StopCapturing(); } if (!_fromCamera && _videoReader != null) { _videoReader.Stop(); } } catch { } } }
private void cbFormats_SelectedIndexChanged(object sender, EventArgs e) { if (isUpdatingFormatList) return; _pendingFormat = cbFormats.SelectedItem as NVideoFormat; }
private void capture() { if (System.Configuration.ConfigurationManager.AppSettings["Verify"] != "file") { if (_camera == null) { LogLine("No camera set", true); return; } try { if (_pendingFormat != null) { _camera.SetCurrentFormat(_pendingFormat); _pendingFormat = null; } _camera.StartCapturing(); UpdateFormatList(); startCapturing(); } catch (Exception ex) { ShowError(ex.Message); } } else { enrollFromImage(true); // to view2 score = Identify(_capturedTemplateList, _enrolledTemplateList); LogLine(string.Format("Face match details: score {0}.", score), true); backgroundWorker_RunWorkerCompleted(null, null); } }
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { bool extractionStarted = false; try { NImage frame = null; NGrayscaleImage grayscaleImage = null; int frameNumber = 0; int bestFrame; int frameCount = Tools.LiveEnrolFrameCount; _extractor.DetectAllFeaturePoints = false; while (backgroundWorker.CancellationPending == false) { if (_pendingFormat != null && _fromCamera) { _camera.SetCurrentFormat(_pendingFormat); _pendingFormat = null; } if (!_fromCamera && _pause) { System.Threading.Thread.Sleep(500); continue; } try { TimeSpan duration = TimeSpan.Zero; TimeSpan timeStamp = TimeSpan.Zero; if (_fromCamera) { frame = _camera.GetFrame(); } else { lock (_readerLock) { frame = _videoReader.ReadVideoSample(out timeStamp, out duration); } } if (frame == null) //camera unplugged or end of file { createFaceRecord = false; SetImageToView(null, null, null, NleExtractionStatus.None, -1, timeStamp); return; } using (grayscaleImage = frame.ToGrayscale()) { if (createFaceRecord) { NleDetectionDetails details; NLTemplate template = null; //NBuffer template = null; if (!extractionStarted) { UpdateExtractorTemplateSize(); frameCount = Tools.LiveEnrolFrameCount; _extractor.ExtractStart(); extractionStarted = true; frameNumber = 0; ClearCapturedImages(); } frameNumber++; NleExtractionStatus status = _extractor.ExtractNext(grayscaleImage, out details); capturedImages.Add((NImage)frame.Clone()); if (status != NleExtractionStatus.None || frameNumber >= frameCount) { template = _extractor.ExtractEnd(out bestFrame, out status); if (status == NleExtractionStatus.TemplateCreated) { NTemplate nTemplate = new NTemplate(); NImage bestImage = frame; if (bestFrame < capturedImages.Count && bestFrame >= 0) bestImage = capturedImages[bestFrame]; _newRecord = new FaceRecord(template, bestImage, details); _newRecord.AddToTemplate(nTemplate); template.Dispose(); capturedImages.Remove(bestImage); _capturedTemplateList = new List<byte[]>(); _capturedTemplateList.Add(nTemplate.Save().ToByteArray()); score = Identify(_capturedTemplateList, _enrolledTemplateList); LogLine(string.Format("Face match details: score {0}.", score), true); backgroundWorker.CancelAsync(); } else { _newRecord = null; } extractionStarted = false; createFaceRecord = false; } if (!createFaceRecord) { ClearCapturedImages(); } SetImageToView(nlView2, frame.ToBitmap(), new NleDetectionDetails[] { details }, status, (int)(frameNumber * 100.0 / frameCount), timeStamp); if (status != NleExtractionStatus.None && status != NleExtractionStatus.TemplateCreated) { backgroundWorker.CancelAsync(); score = 0; } } else { NleDetectionDetails[] details = null; try { NleFace[] faces = _extractor.DetectFaces(grayscaleImage); if (faces != null) { details = new NleDetectionDetails[faces.Length]; for (int i = 0; i < faces.Length; i++) { details[i] = _extractor.DetectFacialFeatures(grayscaleImage, faces[i]); } } } finally { SetImageToView(nlView2, frame.ToBitmap(), details, NleExtractionStatus.None, -1, timeStamp); } } }//using }// try finally { if (frame != null) frame.Dispose(); } }// while } catch (Exception ex) { foreach (NImage img in capturedImages) { img.Dispose(); } capturedImages.Clear(); ShowError(ex.Message); } finally { try { int baseFrameIndex; NleExtractionStatus status; if (extractionStarted) _extractor.ExtractEnd(out baseFrameIndex, out status); if (_fromCamera && _camera != null) _camera.StopCapturing(); if (!_fromCamera && _videoReader != null) _videoReader.Stop(); } catch { } } }
private void UpdateFormatList() { _pendingFormat = null; isUpdatingFormatList = true; cbFormats.BeginUpdate(); cbFormats.Items.Clear(); try { if (_camera != null) { foreach (NMediaFormat format in _camera.GetFormats()) { cbFormats.Items.Add(format); } NMediaFormat currentFormat = _camera.GetCurrentFormat(); if (currentFormat != null) { int index = cbFormats.Items.IndexOf(currentFormat); if (index == -1) { cbFormats.Items.Add(currentFormat); } cbFormats.SelectedItem = currentFormat; } } } finally { cbFormats.EndUpdate(); cbFormats.Enabled = cbFormats.Items.Count > 0; isUpdatingFormatList = false; } }