Exemplo n.º 1
0
        static void getTestBenchamrkRes(CaffeModel model, string iscorrect)
        {
            string        fatherDir  = @"C:\Data\Alignment_TestBenchmark_Equalized\" + iscorrect;
            int           count      = 0;
            DirectoryInfo fatherInfo = new DirectoryInfo(fatherDir);

            DirectoryInfo[] childrenInfos = fatherInfo.GetDirectories();
            foreach (var childrenInfo in childrenInfos)
            {
                FileInfo[] files = childrenInfo.GetFiles("*.jpg", SearchOption.AllDirectories);
                if (files.Length != 2)
                {
                    continue;
                }
                else
                {
                    Bitmap[] bits1      = new[] { new Bitmap(files[0].FullName) };
                    Bitmap[] bits2      = new[] { new Bitmap(files[1].FullName) };
                    float[]  a          = model.ExtractBitmapOutputs(bits1, "ip1", 0);
                    float[]  b          = model.ExtractBitmapOutputs(bits2, "ip1", 0);
                    float    confidency = CaffeModel.CosineDistanceProb(a, b);
                    Write(@"C:\Users\BALTHASAR\Documents\MATLAB\FaceTest\" + iscorrect + ".txt", confidency.ToString());
                }
                count++;
                Console.WriteLine(count);
            }
        }
Exemplo n.º 2
0
        public static RecogReply PredictionFunc(Guid id, int timeBudgetInMS, RecogRequest req)
        {
            byte[] imgBuf      = req.Data;
            byte[] imgType     = System.Text.Encoding.UTF8.GetBytes("jpg");
            Guid   imgID       = BufferCache.HashBufferAndType(imgBuf, imgType);
            string imgFileName = imgID.ToString() + ".jpg";

            string filename = Path.Combine(saveImageDir, imgFileName);

            if (!File.Exists(filename))
            {
                FileTools.WriteBytesToFileConcurrent(filename, imgBuf);
            }

            Stopwatch timer = Stopwatch.StartNew();

            CaffeModel.SetDevice(gpu);
            string resultString = predictor.Predict(filename);

            timer.Stop();

            File.Delete(filename);
            numImageRecognized++;
            Console.WriteLine("Image {0}:{1}:{2}: {3}", numImageRecognized, imgFileName, timer.Elapsed, resultString);
            return(VHubRecogResultHelper.FixedClassificationResult(resultString, resultString));
        }
Exemplo n.º 3
0
        private static float[] ImageAligement(Bitmap bit, float[][] feature, int j, CaffeModel caffe5IPTs, int deviceId)
        {
            try
            {
                Image <Bgr, byte> image = new Image <Bgr, byte>(bit);
                Rectangle         draw  = new Rectangle(Convert.ToInt32(feature[0][j * 4 + 0] * image.Width),
                                                        Convert.ToInt32(feature[0][j * 4 + 1] * image.Height),
                                                        Convert.ToInt32(feature[0][j * 4 + 2] * image.Width - feature[0][j * 4 + 0] * image.Width),
                                                        Convert.ToInt32(feature[0][j * 4 + 3] * image.Height - feature[0][j * 4 + 1] * image.Height));

                //image.Draw(draw, new Bgr(Color.Aqua), image.Width/64);
                //
                int       Margin_Height = Convert.ToInt32(draw.Height * 1.2f);
                int       Margin_X      = Convert.ToInt32(draw.X - draw.Width * 0.1f);
                int       Margin_Y      = Convert.ToInt32(draw.Y - draw.Height * 0.1f);
                Rectangle Margindraw    = new Rectangle(Margin_X, Margin_Y, Margin_Height, Margin_Height);//What's you really need
                //image.Draw(Margindraw, new Bgr(Color.Crimson), image.Width / 64);
                //*****************
                image.ROI = Margindraw;
                Image <Bgr, byte> C    = new Image <Bgr, byte>(image.Bitmap);
                float[]           pts5 = caffe5IPTs.ExtractBitmapOutputs(new[] { C.Bitmap }, "fc2", deviceId);

                for (int i = 0; i < pts5.Length; i += 2)
                {
                    pts5[i]     = (pts5[i] * Margindraw.Width - Margindraw.Width / 12.0f + draw.X - image.Width / 7.0f) / image.Width / 1.4f;
                    pts5[i + 1] = (pts5[i + 1] * Margindraw.Height - Margindraw.Height / 12.0f + draw.Y - image.Height / 7.0f) / image.Height / 1.4f;
                }
                return(pts5);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Exemplo n.º 4
0
        void whatever()
        {
            string     net1   = @"IPBBox_deploy.prototxt";
            string     model1 = @"IPBBox_iter_iter_60000.caffemodel";
            CaffeModel IPBbox = new CaffeModel(net1, model1, false);

            string     net2   = @"5IPTs_deploy.prototxt";
            string     model2 = @"5IPTs_iter_iter_300000.caffemodel";
            CaffeModel IPTs5  = new CaffeModel(net2, model2, false);

            FastFace ff = new FastFace(1.1f, 3, 24);

            Bitmap bmp = new Bitmap(@"D:\小咖秀\20153月296月10\20170115150220800-315.jpg");


            FaceInfo info = ff.Facedetect_Multiview_Reinforce(bmp);

            //Bitmap[] bits = new Bitmap[info.count];
            Bitmap[]  bits    = new[] { bmp };
            Rectangle rect    = new Rectangle(0, 0, bmp.Width, bmp.Height);
            int       StartX  = Convert.ToInt32(rect.X - rect.Width * 0.2);
            int       StartY  = Convert.ToInt32(rect.Y - rect.Height * 0.2);
            int       iWidth  = Convert.ToInt32(rect.Width * 1.4);
            int       iHeight = Convert.ToInt32(rect.Height * 1.4);

            bits[0] = KiCut(bmp, StartX, StartY, iWidth, iHeight);
            for (int i = 0; i < info.count; i++)
            {
                //int StartX = Convert.ToInt32(info.r[i].X - info.r[i].Width * 0.2);
                //int StartY = Convert.ToInt32(info.r[i].Y - info.r[i].Height * 0.2);
                //int iWidth = Convert.ToInt32(info.r[i].Width * 1.4);
                //int iHeight = Convert.ToInt32(info.r[i].Height * 1.4);
                //bits[i] = KiCut(bmp, StartX, StartY, iWidth, iHeight);

                //Image<Bgr, byte> image = new Image<Bgr, byte>(bmp);
                //Rectangle rect = info.r[i];
                //rect.X = Convert.ToInt32(rect.X - rect.Width * 0.2);
                //rect.Y = Convert.ToInt32(rect.Y - rect.Height * 0.2);
                //rect.Width = Convert.ToInt32(rect.Width * 1.4);
                //rect.Height = Convert.ToInt32(rect.Height * 1.4);
                //image.ROI = rect;
                //bits[i] = image.Bitmap;
                //image.ROI = Rectangle.Empty; ;
            }
            if (/*info.count>0*/ true)
            {
                float[][]   aa    = IPBbox.ExtractBitmapOutputs(bits, new[] { "fc2", "fc3" }, 0);
                Rectangle[] rects = new Rectangle[info.count];
                Bitmap[]    C     = CaffeModel.Align_Step1(bits, rects, aa[0], aa[1]);

                float[]  bb = IPTs5.ExtractBitmapOutputs(C, "fc2", 0);
                Bitmap[] F  = CaffeModel.Align_Step2(bits, C, bb, rects, 128, 128);

                F[0].Save(@"C:\Users\BALTHASAR\Desktop\test.jpg");
                float[][] a =
                    IPBbox.ExtractFileOutputs(new[] { @"D:\Research\FacialLandmarks\Data\5PTS\batch1_1.jpg" },
                                              new[] { "fc2", "fc3" }, 0);
            }
        }
Exemplo n.º 5
0
        public static bool InitializeRecognizer(VHubBackendStartParam pa)
        {
            var bInitialized = true;
            var x            = ImageCaptionInstance.Current;

            if (!Object.ReferenceEquals(x, null))
            {
                /// <remarks>
                /// To implement your own image recognizer, please obtain a connection Guid by contacting [email protected]
                /// </remarks>
                x.RegisterAppInfo(new Guid("843EF294-C635-42DA-9AD8-E79E82F9A357"), "0.0.0.1");
                Func <Guid, int, RecogRequest, RecogReply> del = PredictionFunc;
                /// <remarks>
                /// Register your prediction function here.
                /// </remarks>
                string exeDir = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
                x.RegisterClassifierCS("#ImageCaption", Path.Combine(exeDir, "logo.jpg"), 100, del);

                string wordDetectorProto      = Path.Combine(rootDir, @"Model\wordDetector.proto");
                string wordDetectorModel      = Path.Combine(rootDir, @"Model\wordDetector.caffemodel");
                string featureDetectorProto   = Path.Combine(rootDir, @"Model\FeaturesDetector.proto");
                string featureDetectorModel   = Path.Combine(rootDir, @"Model\FeaturesDetector.caffemodel");
                string thresholdFile          = Path.Combine(rootDir, @"Model\thresholds.txt");
                string wordLabelMapFile       = Path.Combine(rootDir, @"Model\labelmap.txt");
                string languageModel          = Path.Combine(rootDir, @"Model\sentencesGeneration.lblmmodel");
                string referenceDssmModelFile = Path.Combine(rootDir, @"Model\reference.dssmmodel");
                string candidateDssmModelFile = Path.Combine(rootDir, @"Model\candidate.dssmmodel");
                string trigramFile            = Path.Combine(rootDir, @"Model\coco.cap.l3g");
                CaffeModel.SetDevice(gpu); //This needs to be set before instantiating the net
                var lmTestArguments = new LangModel.Arguments
                {
                    NumWorkers            = 8,
                    MaxSentenceLength     = 19,
                    NumSentences          = 500,
                    BeamWidth             = 200,
                    AttributesCoverageBar = 5,
                    KTopWords             = 100
                };

                predictor = new Predictor(wordDetectorProto, wordDetectorModel, featureDetectorProto, featureDetectorModel,
                                          thresholdFile, wordLabelMapFile,
                                          languageModel, lmTestArguments, referenceDssmModelFile, candidateDssmModelFile, trigramFile);
            }
            else
            {
                bInitialized = false;
            }
            return(bInitialized);
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            //CaffeModel temp = new CaffeModel(@"C:\Users\BALTHASAR\Desktop\Feature.prototxt", @"C:\Users\BALTHASAR\Desktop\Feature.caffemodel");
            CaffeModel resnet101 = new CaffeModel(@"C:\Research\FaceRecognition\model\centerloss_flip_deploy.prototxt", @"C:\Research\FaceRecognition\snapshot\centerloss34_fine_iter_20000.caffemodel", 0);

            //resnet101.GetUsefulPart(@"C:\Research\FaceRecognition\snapshot\centerloss34_usefulpart.caffemodel");
            //SortedList<int, float> results = lfw_fe(@"D:\Research\FaceRecognition\snapshot\GlasssixNet_train_iter_30000.caffemodel",
            //    "GlasssixNet.prototxt", "lfw_pairs.txt", @"D:\Alignment_lfw_Equalized\");
            //float best_th;
            //float ACC = lfw_acc(results, out best_th);
            //lfw_error(best_th, results);
            //Console.WriteLine(ACC);
            getTestBenchamrkRes(resnet101, "Wrong");
            getTestBenchamrkRes(resnet101, "Correct");
        }
Exemplo n.º 7
0
        public void Init(string protoFile, string modelFile, string meanFile, string labelMapFile, string entityInfoFile, int gpu)
        {
            // Init caffe model
            CaffeModel.SetDevice(gpu);
            _caffeModel = new CaffeModel(protoFile, modelFile);
            _caffeModel.SetMeanFile(meanFile);
            Console.WriteLine("Succeed: Load Model File!\n");

            _labelMap = File.ReadAllLines(labelMapFile)
                        .Select(line => line.Split('\t')[0])
                        .ToArray();

            if (!string.IsNullOrEmpty(entityInfoFile))
            {
                _entityInfo = File.ReadLines(entityInfoFile)
                              .Select(line => line.Split('\t'))
                              .ToDictionary(cols => cols[0], cols => cols[1]);
            }
        }
Exemplo n.º 8
0
        static SortedList <int, float> lfw_fe(string model, string net, string pair, string imgDir)
        {
            SortedList <int, float> results = new SortedList <int, float>();
            var          fe = new CaffeModel(net, model);
            StreamReader sr = new StreamReader(pair, Encoding.Default);
            string       line;
            int          label = 1;

            while ((line = sr.ReadLine()) != null)
            {
                string[] sArray = line.Split('\t');
                if (sArray.Length == 3)
                {
                    DirectoryInfo child      = new DirectoryInfo(imgDir + sArray[0]);
                    FileInfo[]    file       = child.GetFiles("*.jpg", SearchOption.AllDirectories);
                    float[]       a          = fe.ExtractFileOutputs(new[] { file[Convert.ToInt32(sArray[1]) - 1].FullName }, "eltmax_fc5", 0);
                    float[]       b          = fe.ExtractFileOutputs(new[] { file[Convert.ToInt32(sArray[2]) - 1].FullName }, "eltmax_fc5", 0);
                    float         confidency = CaffeModel.CosineDistanceProb(a, b);
                    results.Add(label, confidency);
                }
                if (sArray.Length == 4)
                {
                    DirectoryInfo child = new DirectoryInfo(imgDir + sArray[0]);
                    FileInfo[]    file  = child.GetFiles("*.jpg", SearchOption.AllDirectories);
                    float[]       a     = fe.ExtractFileOutputs(new[] { file[Convert.ToInt32(sArray[1]) - 1].FullName }, "eltmax_fc5", 0);
                    //
                    child = new DirectoryInfo(imgDir + sArray[2]);
                    file  = child.GetFiles("*.jpg", SearchOption.AllDirectories);
                    float[] b          = fe.ExtractFileOutputs(new[] { file[Convert.ToInt32(sArray[3]) - 1].FullName }, "eltmax_fc5", 0);
                    float   confidency = CaffeModel.CosineDistanceProb(a, b);
                    results.Add(-1 * label, confidency);
                }
                Console.WriteLine(label);
                label++;
            }
            return(results);
        }
Exemplo n.º 9
0
        static async void XiaoKaXiu()
        {
            string            model1            = @"IPBBox_iter_iter_60000.caffemodel";
            string            net1              = @"IPBBox_deploy.prototxt";
            string            model2            = @"5IPTs_iter_iter_300000.caffemodel";
            string            net2              = @"5IPTs_deploy.prototxt";
            var               IPBBox            = new CaffeModel(net1, model1, false);
            var               IPTs              = new CaffeModel(net2, model2, false);
            var               ff                = new FastFace(1.1f, 3, 48);
            string            fatherDir         = @"D:\CASIA\Cleaned_CASIA";
            FaceServiceClient faceServiceClient = new FaceServiceClient(SubscriptionKey);

            int count = 0;
            //Random rd=new Random(45985369);
            DirectoryInfo fatherInfo = new DirectoryInfo(fatherDir);

            DirectoryInfo[] childrenInfos = fatherInfo.GetDirectories();
            foreach (var childrenInfo in childrenInfos)
            {
                //DirectoryInfo[] SubInfos = childrenInfo.GetDirectories();

                FileInfo[] files = childrenInfo.GetFiles("*.jpg", SearchOption.AllDirectories);


                int filecounter = 0;
                foreach (var file in files)
                {
                    filecounter++;
                    try
                    {
                        if (count <= -1)
                        {
                            continue;
                        }
                        Bitmap   bmp  = new Bitmap(file.FullName);
                        FaceInfo info = ff.Facedetect_Frontal_Surveillance(bmp);
                        if (info.count != 1)
                        {
                            //Write("lost.txt", file.FullName);
                            continue;
                        }

                        ////**************
                        Bitmap[] bits = new Bitmap[1];
                        bits[0] = bmp;
                        Rectangle rect    = new Rectangle(0, 0, bmp.Width, bmp.Height);
                        int       StartX  = Convert.ToInt32(0 - bmp.Width * 0.2);
                        int       StartY  = Convert.ToInt32(0 - bmp.Height * 0.2);
                        int       iWidth  = Convert.ToInt32(bmp.Width * 1.4);
                        int       iHeight = Convert.ToInt32(bmp.Height * 1.4);
                        bits[0] = KiCut(bmp, StartX, StartY, iWidth, iHeight);

                        float[][] feature = IPBBox.ExtractBitmapOutputs(bits, new[] { "fc2", "fc3" }, 0);
                        if (Math.Abs(feature[1][0] * 90) >= 30)
                        {
                            bits[0].Dispose();
                            bmp.Dispose();
                            continue;
                        }

                        Rectangle[] rects = new Rectangle[bits.Length];
                        Bitmap[]    C     = CaffeModel.Align_Step1(bits, rects, feature[0], feature[1]);
                        float[]     bb    = IPTs.ExtractBitmapOutputs(C, "fc2", 0);
                        Bitmap[]    F     = CaffeModel.Align_Step2(bits, C, bb, rects, 128, 128);

                        if (!Directory.Exists(@"C:\Data\Alignment_CASIA\" + childrenInfo.Name))
                        {
                            Directory.CreateDirectory(@"C:\Data\Alignment_CASIA\" + childrenInfo.Name);
                        }
                        Image <Gray, byte> gray = new Image <Gray, byte>(F[0]);
                        //Image<Gray, byte> temp = gray.Resize(32, 32, Inter.Linear);
                        //temp = temp.Resize(64, 64, Inter.Linear);
                        gray._EqualizeHist();
                        Image <Bgr, byte> colorimg = new Image <Bgr, byte>(new Image <Gray, byte>[] { gray, gray, gray });
                        colorimg.Save(@"C:\Data\Alignment_CASIA\" + childrenInfo.Name + "\\" + filecounter + ".jpg");
                        //temp._EqualizeHist();
                        //temp.Save(@"C:\Data\Alignment_Registed_Equalized\" + childrenInfo.Name + "\\" + filecounter + "_per_blur.jpg");
                        //Console.WriteLine(@"C:\Data\Alignment_CASIA_Equalized\" + childrenInfo.Name + "\\" + filecounter + " complete!");
                        ////*****
                        colorimg.Dispose();
                        gray.Dispose();
                        F[0].Dispose();
                        C[0].Dispose();
                        bits[0].Dispose();
                        bmp.Dispose();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());

                        continue;
                        throw;
                    }

                    //}
                    //Console.WriteLine(filecounter);
                }
                Console.WriteLine(count);
                count++;
            }
        }