public byte[] ExtractTemplate(DataSetMain.ImageDataTable table)
        {
            if (extractor == null)
            {
                return(new byte[0]);
            }

            NLTemplate template = null;

            try
            {
                // Start extraction of face template from a stream (sequence of images)
                extractor.ExtractStart(table.Rows.Count);

                NleExtractionStatus extractionStatus = NleExtractionStatus.None;
                // process stream of images
                foreach (DataSetMain.ImageRow row in table)
                {
                    // read image
                    NImage image = NImage.FromStream(new MemoryStream(row.Image));
                    //????????????????????? Changing  to NPixelFormat.Grayscale
                    using (NGrayscaleImage grayscaleImage = (NGrayscaleImage)NImage.FromImage(NPixelFormat.Grayscale, 0, image))
                    {
                        if (image != null)
                        {
                            image.Dispose();
                        }
                        // use image as another frame of stream
                        NleDetectionDetails details;
                        int baseFrameIndex;
                        extractionStatus = extractor.ExtractNext(grayscaleImage, out details, out baseFrameIndex, out template);
                    }
                }
                if (extractionStatus == NleExtractionStatus.TemplateCreated)
                {
                    // return compressed template
                    return(template.Save().ToByteArray());
                }
            }
            catch (Exception e)
            {
                string msg = e.InnerException != null ? e.InnerException.Message : e.Message;
                throw new VerilookToolException(errormsg + msg);
            }
            finally
            {
                if (template != null)
                {
                    template.Dispose();
                }
            }

            return(new byte[0]);
        }
Exemple #2
0
        private bool createTemplate(Bitmap enrollmentBmp, bool largeTemplate, out NleDetectionDetails detDetails)
        {
            NLExtractor templateExtractor = new NLExtractor();

            if (largeTemplate)
            {
                templateExtractor.TemplateSize = NleTemplateSize.Large;
            }
            else
            {
                templateExtractor.TemplateSize = NleTemplateSize.Medium;
            }
            NImage              enrollmentImage     = NImage.FromBitmap(enrollmentBmp);
            NGrayscaleImage     enrollmentGrayscale = enrollmentImage.ToGrayscale();
            NleDetectionDetails _detDetails         = null;

            try {
                verifyLicense();
                NleExtractionStatus extractionStatus;
                facialTemplate = templateExtractor.Extract(enrollmentGrayscale, out _detDetails, out extractionStatus);

                if (extractionStatus != NleExtractionStatus.TemplateCreated)
                {
                    MessageBox.Show("Face Template Extraction Failed!\nPlease try again.\n" + extractionStatus.ToString());
                    detDetails = _detDetails;
                    return(false);
                }
                else
                {
                    detDetails = _detDetails;
                    return(true);
                }
            } catch (Exception ex) {
                MessageBox.Show("" + ex);
                detDetails = null;
                return(false);
            } finally {
                NLicense.ReleaseComponents(Components);
                if (templateExtractor != null)
                {
                    templateExtractor.Dispose();
                }
            }
        }
        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 enrollFromImage(bool toView2)
        {
            NImage nImage = null;

            if (System.Configuration.ConfigurationManager.AppSettings["Enroll"] != "file")
            {
                byte[]          buffer = null;
                DBHelper.DBUtil db     = new DBHelper.DBUtil();
                if (System.Configuration.ConfigurationManager.AppSettings["Enroll"] == "service")
                {
                    buffer = db.GetImageFromWebService(IMAGE_TYPE.picture, this.userId);
                }
                else
                {
                    buffer = db.GetImage(IMAGE_TYPE.picture, this.userId);
                }

                MemoryStream ms = null;
                try
                {
                    if (buffer != null)
                    {
                        ms = new MemoryStream(buffer);
                    }

                    if (ms != null)
                    {
                        nImage = NImage.FromStream(ms);
                    }
                }
                catch (Exception ex)
                {
                    ShowError(string.Format("Error creating image retrieved from database {0}", ex.Message));
                    return;
                }
                finally
                {
                    if (ms != null)
                    {
                        ms.Dispose();
                    }
                }
            }
            else
            {
                if (!toView2)
                {
                    nImage = NImage.FromFile(Path.GetDirectoryName(Application.ExecutablePath) + "\\Roman.jpg");
                }
                else
                {
                    nImage = NImage.FromFile(Path.GetDirectoryName(Application.ExecutablePath) + "\\Roman2.jpg");
                }
            }

            try
            {
                if (!toView2)
                {
                    nlView1.Image = nImage.ToBitmap();
                }
                else
                {
                    nlView2.Image = nImage.ToBitmap();
                }

                using (NGrayscaleImage gray = nImage.ToGrayscale())
                {
                    NleDetectionDetails details;
                    NleExtractionStatus status;
                    try { _extractor.DetectAllFeaturePoints = Settings.Default.ExtractorDetectAllFeaturesNonLive; }
                    catch { }
                    NLTemplate template = _extractor.Extract(gray, out details, out status);
                    if (status != NleExtractionStatus.TemplateCreated)
                    {
                        _newRecord = null;
//                        ShowError(string.Format("Template extraction failed: {0}", status));
                        String descr = getStatusDescription(status);
                        if (descr != String.Empty)
                        {
                            ShowError(string.Format("Template extraction failed: {0}", descr));
                        }
                        else
                        {
                            ShowError(string.Format("Template extraction failed: {0}", status));
                        }
                        return;
                    }
                    else
                    {
                        _newRecord = new FaceRecord(template, nImage, details);
                        if (!toView2)
                        {
                            _enrolledTemplateList = new List <byte[]>();
                            _enrolledTemplateList.Add(template.Save().ToByteArray());
                        }
                        else
                        {
                            _capturedTemplateList = new List <byte[]>();
                            _capturedTemplateList.Add(template.Save().ToByteArray());
                        }
                        template.Dispose();
                    }

                    if (!toView2)
                    {
                        SetImageToView(nlView1, nImage.ToBitmap(), new NleDetectionDetails[] { details }, status, 100, TimeSpan.Zero);
                    }
                    else
                    {
                        SetImageToView(nlView2, nImage.ToBitmap(), new NleDetectionDetails[] { details }, status, 100, TimeSpan.Zero);
                    }
                }
            }
            catch (Exception ex)
            {
                ShowError(ex.Message);
            }
            return;
        }
Exemple #5
0
 internal byte[] encryptTemplate(NLTemplate template)
 {
     byte[] templateArray     = template.Save().ToByteArray();
     byte[] encryptedTemplate = EncryptBytes(templateArray, "damelon");
     return(encryptedTemplate);
 }
Exemple #6
0
 public FaceRecord(NLTemplate nlTemplate, NImage image, params NleDetectionDetails[] detectionDetails)
 {
     this._nlTemplate = nlTemplate;
     this._detectionDetails = detectionDetails;
     this.Image = image;
 }
Exemple #7
0
        public void insertEntry(string fName, string lName, string permissionsLevel, string id, Bitmap imageBmp, NLTemplate template, string vidLoc)
        {
            OleDbConnection myDbConnection = new OleDbConnection(connectionString);

            using (myDbConnection)
            {
                cryptography encrypt          = new cryptography();
                string       crypFName        = encrypt.encryptString(fName);
                string       cryptLName       = encrypt.encryptString(lName);
                string       cryptAccessLevel = encrypt.encryptString(permissionsLevel);
                string       cryptUserId      = encrypt.encryptString(id);
                string       cryptVidLoc      = encrypt.encryptString(vidLoc);
                byte[]       cryptImage       = encrypt.encryptImage(imageBmp);
                byte[]       cryptTemplate    = encrypt.encryptTemplate(template);

                OleDbCommand command = new OleDbCommand();
                command.CommandType = CommandType.Text;
                command.Connection  = myDbConnection;
                command.CommandText = "INSERT INTO myTable (firstName, lastName, userId, permission, picture, template, videoLink) Values (@p1,@p2,@p3,@p4,@p5,@p6, @p7)";
                command.Parameters.AddWithValue("@P1", crypFName);
                command.Parameters.AddWithValue("@p2", cryptLName);
                command.Parameters.AddWithValue("@p3", cryptUserId);
                command.Parameters.AddWithValue("@p4", cryptAccessLevel);
                command.Parameters.AddWithValue("@p5", cryptImage);
                command.Parameters.AddWithValue("@p6", cryptTemplate);
                command.Parameters.AddWithValue("p7", cryptVidLoc);
                try
                {
                    myDbConnection.Open();
                    command.ExecuteNonQuery();
                }
                catch (OleDbException ex)
                {
                    DisplayOleDbErrorCollection(ex);
                    MessageBox.Show("No records were recorded");
                }
                finally
                {
                    myDbConnection.Close();
                }
            }
        }
Exemple #8
0
        private void enroll(Bitmap globalBitmap)
        {
            NleDetectionDetails detDetails = null;
            string destination             = null;
            string permissionLevel         = "User";
            bool   lgTemplate = true;
            Draw   clipping   = new Draw();

            if (firstNameTextBox.Text == "" || lastNameTextBox.Text == "")
            {
                MessageBox.Show("Please fill in the first and last name fields.");
                return;
            }
            // Strip extra spaces from name entries.
            string firstName = RemoveWhitespace(firstNameTextBox.Text);
            string lastName  = RemoveWhitespace(lastNameTextBox.Text);

            if (!myDdInterface.userExists(firstName, lastName))
            {
                string enrolleeId = createUserId(firstName, lastName);

                if (!createTemplate(globalBitmap, lgTemplate, out detDetails))
                {
                    myThread = new Thread(getLiveVideo);
                    myThread.Start();
                    videoFileLoc      = null;
                    videoFileTxt.Text = null;
                    return;
                }

                if (videoFileLoc == null)
                {
                    MessageBox.Show("The default video will play when you are recognized.\n You can change it later.");
                    videoFileLoc = defaultVideoFile;
                }
                else
                {
                    destination = Path.Combine(Environment.CurrentDirectory, "videoFiles\\" + videoFileTxt.Text);
                    if (!File.Exists(destination))                     // Check to see if the file is already there
                    {
                        File.Copy(videoFileLoc, destination);
                    }
                }
                string videoFileName = Path.GetFileName(videoFileLoc);
                if (!myDdInterface.adminExists())
                {
                    permissionLevel = "Admin";
                }

                // insert user info into database
                myDdInterface.insertEntry(firstName, lastName, permissionLevel, enrolleeId, globalBitmap, facialTemplate, videoFileName);
                facialTemplate = null;
                // set the enrolled image
                if (enrolledImagePictureBox.InvokeRequired)
                {
                    enrolledImagePictureBox.Invoke(new Action(() => enrolledImagePictureBox.Image = globalBitmap));
                    enrolledImageNameLabel.Invoke(new Action(() => enrolledImageNameLabel.Text    = (firstNameTextBox.Text + " " + lastNameTextBox.Text)));
                }
                else
                {
                    enrolledImagePictureBox.Image = globalBitmap;
                    enrolledImageNameLabel.Text   = (firstName + " " + lastName);
                }
                // make it visible
                if (!enrolledImagePictureBox.Visible)
                {
                    enrolledPicVisToggle();
                }
                videoFileLoc         = null;
                videoFileTxt.Text    = null;
                profileFNameTxt.Text = null;
                profileLNameTxt.Text = null;
                myThread             = new Thread(getLiveVideo);
                myThread.Start();
                MessageBox.Show("Enrollment Successful. Your userId is " + enrolleeId);
            }
            else
            {
                MessageBox.Show("That user is already enrolled");
                myThread = new Thread(getLiveVideo);
                myThread.Start();
                return;
            }
        }
Exemple #9
0
        private int identify()
        {
            verifyLicense();
            bool         largeTemplate   = true;
            cryptography decrypt         = new cryptography();
            NMatcher     templateMatcher = null;
            int          maxKeyNum       = 0;
            int          primKey         = 0;

            try {
                NBuffer probeTemplateBuffer = null;
                byte [] probeTemplateArray  = null;
                try {
                    NleDetectionDetails details;                     // unused
                    if (!createTemplate(globalInsetFaceBmp, largeTemplate, out details))
                    {
                        return(primKey);
                    }
                    probeTemplateBuffer = facialTemplate.Save();
                    probeTemplateArray  = probeTemplateBuffer.ToByteArray();
                } catch (IOException ex) {
                    MessageBox.Show("error reading input file {0}: " + ex);
                    return(primKey);
                }

                // extract gallery templates
                // This code allows for searching a database where rows (users) have been deleted,
                // causing primaryKeys to not be sequential.
                int row = 1;
                maxKeyNum = myDdInterface.maxPrimaryKey();
                if (maxKeyNum == 0)
                {
                    MessageBox.Show("There are no registered users.\nPlease enroll and try again.");
                    return(maxKeyNum);
                }
                List <byte []>  dbaseTemplates = new List <byte []> ();
                List <int [, ]> rowKeyList     = new List <int [, ]> ();        // list of row/primary keys

                for (int i = 0; i < maxKeyNum; i++)
                {
                    try {
                        byte [] tempArray = myDdInterface.getTemplateFromKey(i + 1);
                        if (tempArray != null)                        // skip deleted records
                        {
                            int [,] rowKeyRef = new int [1, 2];
                            dbaseTemplates.Add(tempArray);
                            // store row/primary key reference
                            rowKeyRef [0, 0] = row;
                            rowKeyRef [0, 1] = i + 1;
                            rowKeyList.Add(rowKeyRef);
                            row++;
                        }
                    } catch (IOException ex) {
                        MessageBox.Show("error reading reference template " + i + ": " + ex);
                        return(primKey);
                    }
                }

                templateMatcher = new NMatcher();
                templateMatcher.IdentifyStart(probeTemplateArray);

                try {
                    int numOfTemplates = dbaseTemplates.Count();
                    int score          = 0;
                    int highScore      = 0;
                    for (int i = 0; i < numOfTemplates; i++)
                    {
                        score = templateMatcher.IdentifyNext(dbaseTemplates [i]);
                        if (score > highScore)
                        {
                            highScore = score;
                            // get primary key based on row number
                            int [,] tempRef = new int [1, 2];
                            tempRef         = rowKeyList [i];
                            primKey         = tempRef [0, 1];
                        }
                    }
                    if (primKey == 0)
                    {
                        MessageBox.Show("Sorry, Unable to Identify.\nTry again.");
                        return(primKey);
                    }
                    else
                    {
                        string fName     = myDdInterface.getFName(primKey);
                        string lName     = myDdInterface.getLName(primKey);
                        Bitmap storedBmp = myDdInterface.getImageFromId(myDdInterface.userIdFromKey(primKey));                          // Check for potential errors referencing Primary key.
                        if (enrolledImagePictureBox.InvokeRequired)
                        {
                            enrolledImagePictureBox.Invoke(new Action(() => enrolledImagePictureBox.Image = storedBmp));
                            enrolledImageNameLabel.Invoke(new Action(() => enrolledImageNameLabel.Text    = (fName + " " + lName)));
                        }
                        else
                        {
                            enrolledImagePictureBox.Image = storedBmp;
                            enrolledImageNameLabel.Text   = (fName + " " + lName);
                        }
                        if (!enrolledImagePictureBox.Visible)
                        {
                            enrolledPicVisToggle();
                        }
                        if (tabIndex == 0)                        // play video only on main tab
                        {
                            playVideoFile(primKey);
                        }
                        return(primKey);
                    }
                } catch (Exception ex) {
                    MessageBox.Show("" + ex);
                } finally {
                    templateMatcher.IdentifyEnd();
                    facialTemplate = null;
                }
                return(primKey);
            } catch (Exception ex) {
                MessageBox.Show("" + ex);
                return(primKey);
            } finally {
                NLicense.ReleaseComponents(Components);
                if (templateMatcher != null)
                {
                    templateMatcher.Dispose();
                }
            }
        }