- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Good day!
I work with face recognition first time
I have a problem with code
I use this code sample:
using System;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Threading;
using System.Drawing;
using System.Windows.Controls;
using System.IO;
namespace FaceID
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Thread processingThread;
private PXCMSenseManager senseManager;
private PXCMFaceConfiguration.RecognitionConfiguration recognitionConfig;
private PXCMFaceData faceData;
private PXCMFaceData.RecognitionData recognitionData;
private Int32 numFacesDetected;
private string userId;
private string dbState;
private const int DatabaseUsers = 10;
private const string DatabaseName = "UserDB";
private const string DatabaseFilename = "database.bin";
private bool doRegister;
private bool doUnregister;
private int faceRectangleHeight;
private int faceRectangleWidth;
private int faceRectangleX;
private int faceRectangleY;
public MainWindow()
{
InitializeComponent();
rectFaceMarker.Visibility = Visibility.Hidden;
chkShowFaceMarker.IsChecked = true;
numFacesDetected = 0;
userId = string.Empty;
dbState = string.Empty;
doRegister = false;
doUnregister = false;
// Start SenseManage and configure the face module
ConfigureRealSense();
// Start the worker thread
processingThread = new Thread(new ThreadStart(ProcessingThread));
processingThread.Start();
}
private void ConfigureRealSense()
{
PXCMFaceModule faceModule;
PXCMFaceConfiguration faceConfig;
// Start the SenseManager and session
senseManager = PXCMSenseManager.CreateInstance();
// Enable the color stream
senseManager.EnableStream(PXCMCapture.StreamType.STREAM_TYPE_COLOR, 640, 480, 30);
// Enable the face module
senseManager.EnableFace();
faceModule = senseManager.QueryFace();
faceConfig = faceModule.CreateActiveConfiguration();
// Configure for 3D face tracking (if camera cannot support depth it will revert to 2D tracking)
faceConfig.SetTrackingMode(PXCMFaceConfiguration.TrackingModeType.FACE_MODE_COLOR_PLUS_DEPTH);
// Enable facial recognition
recognitionConfig = faceConfig.QueryRecognition();
recognitionConfig.Enable();
// Create a recognition database
PXCMFaceConfiguration.RecognitionConfiguration.RecognitionStorageDesc recognitionDesc = new PXCMFaceConfiguration.RecognitionConfiguration.RecognitionStorageDesc();
recognitionDesc.maxUsers = DatabaseUsers;
//recognitionConfig.CreateStorage(DatabaseName, out recognitionDesc);
//recognitionConfig.UseStorage(DatabaseName);
LoadDatabaseFromFile();
recognitionConfig.SetRegistrationMode(PXCMFaceConfiguration.RecognitionConfiguration.RecognitionRegistrationMode.REGISTRATION_MODE_CONTINUOUS);
// Apply changes and initialize
faceConfig.ApplyChanges();
senseManager.Init();
faceData = faceModule.CreateOutput();
// Mirror image
senseManager.QueryCaptureManager().QueryDevice().SetMirrorMode(PXCMCapture.Device.MirrorMode.MIRROR_MODE_HORIZONTAL);
// Release resources
faceConfig.Dispose();
faceModule.Dispose();
}
private void ProcessingThread()
{
// Start AcquireFrame/ReleaseFrame loop
while (senseManager.AcquireFrame(true) >= pxcmStatus.PXCM_STATUS_NO_ERROR)
{
// Acquire the color image data
PXCMCapture.Sample sample = senseManager.QuerySample();
Bitmap colorBitmap;
PXCMImage.ImageData colorData;
sample.color.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_RGB24, out colorData);
colorBitmap = colorData.ToBitmap(0, sample.color.info.width, sample.color.info.height);
// Get face data
if (faceData != null)
{
faceData.Update();
numFacesDetected = faceData.QueryNumberOfDetectedFaces();
if (numFacesDetected > 0)
{
// Get the first face detected (index 0)
PXCMFaceData.Face face = faceData.QueryFaceByIndex(0);
// Retrieve face location data
PXCMFaceData.DetectionData faceDetectionData = face.QueryDetection();
if (faceDetectionData != null)
{
PXCMRectI32 faceRectangle;
faceDetectionData.QueryBoundingRect(out faceRectangle);
faceRectangleHeight = faceRectangle.h;
faceRectangleWidth = faceRectangle.w;
faceRectangleX = faceRectangle.x;
faceRectangleY = faceRectangle.y;
}
// Process face recognition data
if (face != null)
{
// Retrieve the recognition data instance
recognitionData = face.QueryRecognition();
// Set the user ID and process register/unregister logic
if (recognitionData.IsRegistered())
{
userId = Convert.ToString(recognitionData.QueryUserID());
if (doUnregister)
{
recognitionData.UnregisterUser();
doUnregister = false;
}
}
else
{
if (doRegister)
{
recognitionData.RegisterUser();
// Capture a jpg image of registered user
colorBitmap.Save("image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
doRegister = false;
}
else
{
userId = "Unrecognized";
}
}
}
}
else
{
userId = "No users in view";
}
}
// Display the color stream and other UI elements
UpdateUI(colorBitmap);
// Release resources
colorBitmap.Dispose();
sample.color.ReleaseAccess(colorData);
sample.color.Dispose();
// Release the frame
senseManager.ReleaseFrame();
}
}
private void UpdateUI(Bitmap bitmap)
{
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
{
// Display the color image
if (bitmap != null)
{
imgColorStream.Source = ConvertBitmap.BitmapToBitmapSource(bitmap);
}
// Update UI elements
lblNumFacesDetected.Content = String.Format("Faces Detected: {0}", numFacesDetected);
lblUserId.Content = String.Format("User ID: {0}", userId);
lblDatabaseState.Content = String.Format("Database: {0}", dbState);
// Change picture border color depending on if user is in camera view
if (numFacesDetected > 0)
{
bdrPictureBorder.BorderBrush = System.Windows.Media.Brushes.LightGreen;
}
else
{
bdrPictureBorder.BorderBrush = System.Windows.Media.Brushes.Red;
}
// Show or hide face marker
if ((numFacesDetected > 0) && (chkShowFaceMarker.IsChecked == true))
{
// Show face marker
rectFaceMarker.Height = faceRectangleHeight;
rectFaceMarker.Width = faceRectangleWidth;
Canvas.SetLeft(rectFaceMarker, faceRectangleX);
Canvas.SetTop(rectFaceMarker, faceRectangleY);
rectFaceMarker.Visibility = Visibility.Visible;
// Show floating ID label
lblFloatingId.Content = String.Format("User ID: {0}", userId);
Canvas.SetLeft(lblFloatingId, faceRectangleX);
Canvas.SetTop(lblFloatingId, faceRectangleY - 20);
lblFloatingId.Visibility = Visibility.Visible;
}
else
{
// Hide the face marker and floating ID label
rectFaceMarker.Visibility = Visibility.Hidden;
lblFloatingId.Visibility = Visibility.Hidden;
}
}));
// Release resources
bitmap.Dispose();
}
private void LoadDatabaseFromFile()
{
if (File.Exists(DatabaseFilename))
{
Byte[] buffer = File.ReadAllBytes(DatabaseFilename);
recognitionConfig.SetDatabaseBuffer(buffer);
dbState = "Loaded";
}
else
{
dbState = "Not Found";
}
}
private void SaveDatabaseToFile()
{
// Allocate the buffer to save the database
PXCMFaceData.RecognitionModuleData recognitionModuleData = faceData.QueryRecognitionModule();
Int32 nBytes = recognitionModuleData.QueryDatabaseSize();
Byte[] buffer = new Byte[nBytes];
// Retrieve the database buffer
recognitionModuleData.QueryDatabaseBuffer(buffer);
// Save the buffer to a file
// (NOTE: production software should use file encryption for privacy protection)
File.WriteAllBytes(DatabaseFilename, buffer);
dbState = "Saved";
}
private void DeleteDatabaseFile()
{
if (File.Exists(DatabaseFilename))
{
File.Delete(DatabaseFilename);
dbState = "Deleted";
}
else
{
dbState = "Not Found";
}
}
private void ReleaseResources()
{
// Stop the worker thread
processingThread.Abort();
// Release resources
faceData.Dispose();
senseManager.Dispose();
}
private void btnRegister_Click(object sender, RoutedEventArgs e)
{
doRegister = true;
}
private void btnUnregister_Click(object sender, RoutedEventArgs e)
{
doUnregister = true;
}
private void btnSaveDatabase_Click(object sender, RoutedEventArgs e)
{
SaveDatabaseToFile();
}
private void btnDeleteDatabase_Click(object sender, RoutedEventArgs e)
{
DeleteDatabaseFile();
}
private void btnExit_Click(object sender, RoutedEventArgs e)
{
ReleaseResources();
this.Close();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
ReleaseResources();
}
}
}
Do you know when RecognitionConfiguration, CreateStorage (), SetStorageDesc (), UseStorage () and DeleteStorage () will be implemented?
How I can create and use database for recognition now? Can you give me the code sample, please?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Ana,
I just sent a private message to you regarding my testing of the code you provided; in summary, I could not reproduce any evident problems. Please follow the steps I outlined in my message and let me know what you find.
Thanks, Bryan

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page