Advertisement
Guest User

Untitled

a guest
Dec 29th, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace WindowsFormsApplication2
  12. {
  13.     class pipeline : UtilMPipeline
  14.     {
  15.         protected int newframes;
  16.         public pipeline()
  17.             : base()
  18.         {
  19.             EnableImage(PXCMImage.ColorFormat.COLOR_FORMAT_DEPTH, 320, 240);
  20.             EnableFaceLandmark();
  21.             EnableFaceLocation();
  22.             newframes = 0;
  23.         }
  24.         public PXCMFaceAnalysis face;
  25.  
  26.         public override void OnFaceLandmarkSetup(ref PXCMFaceAnalysis.Landmark.ProfileInfo finfo)
  27.         {
  28.  
  29.  
  30.             base.OnFaceLandmarkSetup(ref finfo);
  31.         }
  32.         public override void OnFaceSetup(ref PXCMFaceAnalysis.ProfileInfo profile)
  33.         {
  34.  
  35.         }
  36.  
  37.         PXCMFaceAnalysis.Landmark faceland;
  38.         public override void OnFaceDetectionSetup(ref PXCMFaceAnalysis.Detection.ProfileInfo finfo)
  39.         {
  40.             PXCMFaceAnalysis.Landmark faceland = QueryFace().DynamicCast<PXCMFaceAnalysis.Landmark>(PXCMFaceAnalysis.Landmark.CUID);
  41.  
  42.             base.OnFaceDetectionSetup(ref finfo);
  43.         }
  44.  
  45.  
  46.      
  47.         public override bool OnDisconnect()
  48.         {
  49.             MessageBox.Show("Warning: You're camera has been disconnected! Reconnect it immediately!", "Warning!", MessageBoxButtons.OK);
  50.  
  51.             return base.OnDisconnect();
  52.         }
  53.  
  54.         public override void OnReconnect()
  55.         {
  56.  
  57.             MessageBox.Show("The camera has been reconnected!", "Reconnected!!", MessageBoxButtons.OK);
  58.  
  59.         }
  60.  
  61.          public override bool OnNewFrame()
  62.         {
  63.             for (uint i = 0; ; i++)
  64.             {
  65.  
  66.                 PXCMFaceAnalysis ft = QueryFace();
  67.                 int fid; ulong ts;
  68.                 if (ft.QueryFace(i, out fid, out ts) < pxcmStatus.PXCM_STATUS_NO_ERROR) break;
  69.                 PXCMFaceAnalysis.Detection ftd = ft.DynamicCast<PXCMFaceAnalysis.Detection>(PXCMFaceAnalysis.Detection.CUID);
  70.                 PXCMFaceAnalysis.Detection.Data ddata;
  71.                 if (ftd.QueryData(fid, out ddata) >= pxcmStatus.PXCM_STATUS_NO_ERROR)
  72.                     checkfaces();
  73.             }
  74.  
  75.            
  76.                
  77.             return (++newframes < 50000);
  78.             }
  79.  
  80.         bool IsSimilar(PXCMImage image1, PXCMImage image2)
  81.         {
  82.  
  83.             PXCMFaceAnalysis.Recognition.Model[] reference = new PXCMFaceAnalysis.Recognition.Model[]{
  84.             createModelFromImage(image1)};
  85.             PXCMFaceAnalysis.Recognition.Model cur = createModelFromImage(image2);
  86.             uint index;
  87.          
  88.                 return cur.Compare(reference, null, out index) >= pxcmStatus.PXCM_STATUS_NO_ERROR;
  89.            
  90.         }
  91.      
  92.         PXCMFaceAnalysis.Recognition.Model createModelFromImage(PXCMImage image)
  93.         {
  94.          
  95.  
  96.                 PXCMFaceAnalysis face3 = QueryFace();
  97.                 PXCMImage[] images = new PXCMImage[] { image, null, null, null };
  98.                 PXCMScheduler.SyncPoint sp;
  99.                
  100.                 face3.ProcessImageAsync(images, out sp);
  101.                 sp.Synchronize();
  102.                 sp.Dispose();
  103.                 int fid; ulong ts;
  104.                 face3.QueryFace(0, out fid, out ts);
  105.  
  106.                 PXCMFaceAnalysis.Recognition recta = face3.DynamicCast<PXCMFaceAnalysis.Recognition>(PXCMFaceAnalysis.Recognition.CUID);
  107.                 PXCMFaceAnalysis.Recognition.Model m111;
  108.                 recta.CreateModel(fid, out m111);
  109.                 return m111;
  110.                
  111.            
  112.  
  113.         }
  114.  
  115.         string savepathd = @"c:Faces\Users";
  116.  
  117.         private bool availablefaces = false;
  118.         private bool checkedfaces = false;
  119.         public void checkfaces()
  120.         {
  121.             if (!Directory.Exists(savepathd))
  122.             {
  123.                 Directory.CreateDirectory(savepathd);
  124.                 availablefaces = false;
  125.                 registerface();
  126.             }
  127.             else
  128.             {
  129.                 checkusers(savepathd);
  130.                 if (containsfiles = true)
  131.                 {
  132.  
  133.                 }
  134.                 else
  135.                 {
  136.  
  137.                 }
  138.  
  139.             }
  140.         }
  141.         public void registerface()
  142.         {
  143.  
  144.  
  145.         }
  146.         private bool containsfiles = false;
  147.         private void checkusers(string directory)
  148.         {
  149.             if (Directory.GetFiles(directory).Length > 0)
  150.             {
  151.                 processusers(savepathd);
  152.                 availablefaces = true;
  153.             }
  154.             else
  155.             {
  156.                 MessageBox.Show("Empty!");
  157.                 availablefaces = false;
  158.                 registerface();
  159.             }
  160.         }
  161.  
  162.         public void processusers(string directory)
  163.         {
  164.             string[] fileEntries = Directory.GetFiles(directory);
  165.             foreach (string fileName in fileEntries)
  166.             {
  167.                 if (fileName.Contains("user"))
  168.                 {
  169.                     PXCMImage face22 = this.QueryImage(PXCMImage.ImageType.IMAGE_TYPE_COLOR);
  170.                     Image nface = Image.FromFile(fileName, false);
  171.                     convertimage(fileName, face22);
  172.  
  173.                 }
  174.             }
  175.         }
  176.         public void convertimage(string file, PXCMImage cface)
  177.         {
  178.             PXCMSession session = this.QuerySession();
  179.             Bitmap bitmap = (Bitmap)Image.FromFile(file);
  180.             PXCMAccelerator accel;
  181.             session.CreateAccelerator(out accel);
  182.  
  183.             PXCMImage.ImageInfo iinfo = new PXCMImage.ImageInfo();
  184.             iinfo.width = (uint)bitmap.Width;
  185.             iinfo.height = (uint)bitmap.Height;
  186.             iinfo.format = PXCMImage.ColorFormat.COLOR_FORMAT_RGB32;
  187.             PXCMImage image;
  188.             accel.CreateImage(ref iinfo, out image);
  189.             accel.Dispose();
  190.             PXCMImage.ImageData idata;
  191.             image.AcquireAccess(PXCMImage.Access.ACCESS_WRITE, out idata);
  192.  
  193.             BitmapData bdata = new BitmapData();
  194.             bdata.Scan0 = idata.buffer.planes[0];
  195.             bdata.Stride = idata.buffer.pitches[0];
  196.             bdata.PixelFormat = PixelFormat.Format32bppRgb;
  197.             bdata.Width = bitmap.Width;
  198.             bdata.Height = bitmap.Height;
  199.             BitmapData bdata2 = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
  200.                       ImageLockMode.ReadOnly | ImageLockMode.UserInputBuffer,
  201.                       PixelFormat.Format32bppRgb, bdata);
  202.             image.ReleaseAccess(ref idata);
  203.             bitmap.UnlockBits(bdata2);
  204.            
  205.             if (IsSimilar(cface, image))
  206.             {
  207.                 MessageBox.Show("Yes it works!");
  208.             }
  209.  
  210.         }
  211.             //
  212.          };
  213.  
  214.     class Pipe
  215.     {
  216.         public pipeline pipe = new pipeline();
  217.          public void pipelines()
  218.         {
  219.            
  220.             if (pipe.Init())
  221.             {
  222.            
  223.                
  224.                 PXCMSession ses;
  225.                 pxcmStatus status = PXCMSession.CreateInstance(out ses);
  226.                
  227.                 if (status < pxcmStatus.PXCM_STATUS_NO_ERROR)
  228.                 {
  229.                    
  230.                     DialogResult res = MessageBox.Show("Error: Unable to create the session! Try restarting the program!", "Error!", MessageBoxButtons.OK);
  231.                     if (res == DialogResult.OK)
  232.                     {
  233.                         Environment.Exit(0);
  234.                         Application.Exit();
  235.                     }
  236.  
  237.  
  238.                     return;
  239.                 }
  240.  
  241.  
  242.  
  243.  
  244.                 PXCMBase audio;
  245.                 status = ses.CreateImpl(PXCMVoiceRecognition.CUID, out audio);
  246.  
  247.                 if (status < pxcmStatus.PXCM_STATUS_NO_ERROR)
  248.                 {
  249.                     DialogResult res = MessageBox.Show("Error: Could not find voice recognition device! This program requires one to run!", "Error!", MessageBoxButtons.OK);
  250.                     if (res == DialogResult.OK)
  251.                     {
  252.                         Environment.Exit(0);
  253.                         Application.Exit();
  254.                     }
  255.                     return;
  256.                 }
  257.                
  258.  
  259.                 while (pipe != null)
  260.                 {
  261.  
  262.                  
  263.                     pipe.OnNewFrame();
  264.                  
  265.                     if (!pipe.AcquireFrame(true)) break;
  266.                     pipe.ReleaseFrame();
  267.                 }
  268.                
  269.  
  270.  
  271.             }
  272.        
  273.          
  274.            pipe.Close();
  275.            pipe.Dispose();
  276.          
  277.         }
  278.        
  279.  
  280.    
  281.  
  282.     }
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement