Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApplication2
- {
- class pipeline : UtilMPipeline
- {
- protected int newframes;
- public pipeline()
- : base()
- {
- EnableImage(PXCMImage.ColorFormat.COLOR_FORMAT_DEPTH, 320, 240);
- EnableFaceLandmark();
- EnableFaceLocation();
- newframes = 0;
- }
- public PXCMFaceAnalysis face;
- public override void OnFaceLandmarkSetup(ref PXCMFaceAnalysis.Landmark.ProfileInfo finfo)
- {
- base.OnFaceLandmarkSetup(ref finfo);
- }
- public override void OnFaceSetup(ref PXCMFaceAnalysis.ProfileInfo profile)
- {
- }
- PXCMFaceAnalysis.Landmark faceland;
- public override void OnFaceDetectionSetup(ref PXCMFaceAnalysis.Detection.ProfileInfo finfo)
- {
- PXCMFaceAnalysis.Landmark faceland = QueryFace().DynamicCast<PXCMFaceAnalysis.Landmark>(PXCMFaceAnalysis.Landmark.CUID);
- base.OnFaceDetectionSetup(ref finfo);
- }
- public override bool OnDisconnect()
- {
- MessageBox.Show("Warning: You're camera has been disconnected! Reconnect it immediately!", "Warning!", MessageBoxButtons.OK);
- return base.OnDisconnect();
- }
- public override void OnReconnect()
- {
- MessageBox.Show("The camera has been reconnected!", "Reconnected!!", MessageBoxButtons.OK);
- }
- public override bool OnNewFrame()
- {
- for (uint i = 0; ; i++)
- {
- PXCMFaceAnalysis ft = QueryFace();
- int fid; ulong ts;
- if (ft.QueryFace(i, out fid, out ts) < pxcmStatus.PXCM_STATUS_NO_ERROR) break;
- PXCMFaceAnalysis.Detection ftd = ft.DynamicCast<PXCMFaceAnalysis.Detection>(PXCMFaceAnalysis.Detection.CUID);
- PXCMFaceAnalysis.Detection.Data ddata;
- if (ftd.QueryData(fid, out ddata) >= pxcmStatus.PXCM_STATUS_NO_ERROR)
- checkfaces();
- }
- return (++newframes < 50000);
- }
- bool IsSimilar(PXCMImage image1, PXCMImage image2)
- {
- PXCMFaceAnalysis.Recognition.Model[] reference = new PXCMFaceAnalysis.Recognition.Model[]{
- createModelFromImage(image1)};
- PXCMFaceAnalysis.Recognition.Model cur = createModelFromImage(image2);
- uint index;
- return cur.Compare(reference, null, out index) >= pxcmStatus.PXCM_STATUS_NO_ERROR;
- }
- PXCMFaceAnalysis.Recognition.Model createModelFromImage(PXCMImage image)
- {
- PXCMFaceAnalysis face3 = QueryFace();
- PXCMImage[] images = new PXCMImage[] { image, null, null, null };
- PXCMScheduler.SyncPoint sp;
- face3.ProcessImageAsync(images, out sp);
- sp.Synchronize();
- sp.Dispose();
- int fid; ulong ts;
- face3.QueryFace(0, out fid, out ts);
- PXCMFaceAnalysis.Recognition recta = face3.DynamicCast<PXCMFaceAnalysis.Recognition>(PXCMFaceAnalysis.Recognition.CUID);
- PXCMFaceAnalysis.Recognition.Model m111;
- recta.CreateModel(fid, out m111);
- return m111;
- }
- string savepathd = @"c:Faces\Users";
- private bool availablefaces = false;
- private bool checkedfaces = false;
- public void checkfaces()
- {
- if (!Directory.Exists(savepathd))
- {
- Directory.CreateDirectory(savepathd);
- availablefaces = false;
- registerface();
- }
- else
- {
- checkusers(savepathd);
- if (containsfiles = true)
- {
- }
- else
- {
- }
- }
- }
- public void registerface()
- {
- }
- private bool containsfiles = false;
- private void checkusers(string directory)
- {
- if (Directory.GetFiles(directory).Length > 0)
- {
- processusers(savepathd);
- availablefaces = true;
- }
- else
- {
- MessageBox.Show("Empty!");
- availablefaces = false;
- registerface();
- }
- }
- public void processusers(string directory)
- {
- string[] fileEntries = Directory.GetFiles(directory);
- foreach (string fileName in fileEntries)
- {
- if (fileName.Contains("user"))
- {
- PXCMImage face22 = this.QueryImage(PXCMImage.ImageType.IMAGE_TYPE_COLOR);
- Image nface = Image.FromFile(fileName, false);
- convertimage(fileName, face22);
- }
- }
- }
- public void convertimage(string file, PXCMImage cface)
- {
- PXCMSession session = this.QuerySession();
- Bitmap bitmap = (Bitmap)Image.FromFile(file);
- PXCMAccelerator accel;
- session.CreateAccelerator(out accel);
- PXCMImage.ImageInfo iinfo = new PXCMImage.ImageInfo();
- iinfo.width = (uint)bitmap.Width;
- iinfo.height = (uint)bitmap.Height;
- iinfo.format = PXCMImage.ColorFormat.COLOR_FORMAT_RGB32;
- PXCMImage image;
- accel.CreateImage(ref iinfo, out image);
- accel.Dispose();
- PXCMImage.ImageData idata;
- image.AcquireAccess(PXCMImage.Access.ACCESS_WRITE, out idata);
- BitmapData bdata = new BitmapData();
- bdata.Scan0 = idata.buffer.planes[0];
- bdata.Stride = idata.buffer.pitches[0];
- bdata.PixelFormat = PixelFormat.Format32bppRgb;
- bdata.Width = bitmap.Width;
- bdata.Height = bitmap.Height;
- BitmapData bdata2 = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
- ImageLockMode.ReadOnly | ImageLockMode.UserInputBuffer,
- PixelFormat.Format32bppRgb, bdata);
- image.ReleaseAccess(ref idata);
- bitmap.UnlockBits(bdata2);
- if (IsSimilar(cface, image))
- {
- MessageBox.Show("Yes it works!");
- }
- }
- //
- };
- class Pipe
- {
- public pipeline pipe = new pipeline();
- public void pipelines()
- {
- if (pipe.Init())
- {
- PXCMSession ses;
- pxcmStatus status = PXCMSession.CreateInstance(out ses);
- if (status < pxcmStatus.PXCM_STATUS_NO_ERROR)
- {
- DialogResult res = MessageBox.Show("Error: Unable to create the session! Try restarting the program!", "Error!", MessageBoxButtons.OK);
- if (res == DialogResult.OK)
- {
- Environment.Exit(0);
- Application.Exit();
- }
- return;
- }
- PXCMBase audio;
- status = ses.CreateImpl(PXCMVoiceRecognition.CUID, out audio);
- if (status < pxcmStatus.PXCM_STATUS_NO_ERROR)
- {
- DialogResult res = MessageBox.Show("Error: Could not find voice recognition device! This program requires one to run!", "Error!", MessageBoxButtons.OK);
- if (res == DialogResult.OK)
- {
- Environment.Exit(0);
- Application.Exit();
- }
- return;
- }
- while (pipe != null)
- {
- pipe.OnNewFrame();
- if (!pipe.AcquireFrame(true)) break;
- pipe.ReleaseFrame();
- }
- }
- pipe.Close();
- pipe.Dispose();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement