Exemple #1
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));
        }
Exemple #2
0
        public static RecogReply PredictionFunc(Guid id, int timeBudgetInMS, RecogRequest req)
        {
            byte[] imgBuf = req.Data;

            using (var ms = new MemoryStream(imgBuf))
                using (var img = (Bitmap)Bitmap.FromStream(ms))
                {
                    string result = caffePredictor.Predict(img, cmd.topK, cmd.confThreshold);

                    numDataProcessed++;
                    Console.WriteLine("Image {0}: {1}", numDataProcessed, result);
                    return(VHubRecogResultHelper.FixedClassificationResult(result, result));
                }
        }
Exemple #3
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 resultString = "";

            try
            {
                string filename = Path.Combine(saveImageDir, imgFileName);
                if (!File.Exists(filename))
                {
                    FileTools.WriteBytesToFileConcurrent(filename, imgBuf);
                }

                Directory.SetCurrentDirectory(rootDir);
                var processStartInfo = new ProcessStartInfo
                {
                    FileName  = @"main.bat",
                    Arguments = filename,
                    //RedirectStandardOutput = true,
                    UseShellExecute = false
                };
                var process = Process.Start(processStartInfo);
                //var resultString = process.StandardOutput.ReadToEnd();
                process.WaitForExit();
                if (File.Exists(filename + ".caption"))
                {
                    resultString = File.ReadAllLines(filename + ".caption")[0];
                }

                File.Delete(filename);
                File.Delete(filename + "_1.txt");
                File.Delete(filename + "_1.txt.detections.prec.txt");
                File.Delete(filename + "_1.txt.detections.sc.txt");
                File.Delete(filename + "_1.txt.img.dmsm.fea");
                File.Delete(filename + ".caption");
            }
            catch (Exception e)
            {
                Logger.Log(LogLevel.Error, e.Message);
                resultString = "Service exception. Please try again.";
            }
            numImageRecognized++;
            Console.WriteLine("Image {0}: {1}", numImageRecognized, resultString);
            return(VHubRecogResultHelper.FixedClassificationResult(resultString, resultString));
        }
Exemple #4
0
        public static RecogReply PredictionFunc(Guid id, int timeBudgetInMS, RecogRequest req)
        {
            byte[] data         = req.Data;
            byte[] dataType     = System.Text.Encoding.UTF8.GetBytes("mllr");
            Guid   dataID       = BufferCache.HashBufferAndType(data, dataType);
            string dataFileName = dataID.ToString() + ".mllr";

            string filename = Path.Combine(saveDataDir, dataFileName);

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

            Directory.SetCurrentDirectory(rootDir);
            var processStartInfo = new ProcessStartInfo
            {
                FileName  = @"main.bat",
                Arguments = filename,
                //RedirectStandardOutput = true,
                UseShellExecute = false
            };
            var process = Process.Start(processStartInfo);

            //var resultString = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            string resultString = "";

            if (File.Exists(filename + ".mllr"))
            {
                resultString = File.ReadAllText(filename + ".mllr");
            }

            File.Delete(filename);
            File.Delete(filename + ".mllr");

            numDataProcessed++;
            Console.WriteLine("Data {0}: {1}", numDataProcessed, resultString);
            return(VHubRecogResultHelper.FixedClassificationResult(resultString, resultString));
        }
Exemple #5
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);
            }

            string result_words   = wordDetector.DetectWords(filename);
            string result_feature = wordDetector.ExtractFeature(filename);

            string resultString = result_words + "\n" + result_feature;

            File.Delete(filename);
            numImageRecognized++;
            Console.WriteLine("Image {0}: {1}", numImageRecognized, resultString);
            return(VHubRecogResultHelper.FixedClassificationResult(resultString, resultString));
        }
Exemple #6
0
        public static RecogReply QueryFunc(Guid id, int timeBudgetInMS, RecogRequest req)
        {
            var resultString = "Unknown";

            return(VHubRecogResultHelper.FixedClassificationResult(resultString, resultString));
        }
Exemple #7
0
        public static RecogReply PredictionFunc(Guid id, int timeBudgetInMS, RecogRequest req)
        {
            Dictionary <string, string> cmdDict;

            try
            {
                string request = Encoding.Default.GetString(req.Data);
                cmdDict = request.Split(';')
                          .Select(x => x.Split(':'))
                          .ToDictionary(f => f[0].Trim(), f => f[1].Trim(), StringComparer.OrdinalIgnoreCase);
            }
            catch
            {
                string msg = "Invalid request.";
                return(VHubRecogResultHelper.FixedClassificationResult(msg, msg));
            }

            string cmd         = cmdDict["cmd"];
            string serviceGuid = cmdDict["serviceGuid"];

            string result = string.Empty;

            if (string.Compare(cmd, "Start", true) == 0 || string.Compare(cmd, "Resume", true) == 0)
            {
                int       instanceNum = Convert.ToInt32(cmdDict["instanceNum"]);
                Evaluator evaluator   = dictEvaluator.GetOrAdd(serviceGuid, guid =>
                {
                    var eval = new Evaluator(gateWay, guid, evalSetting);
                    Task.Run(async() =>
                    {
                        bool isResume = string.Compare(cmd, "Resume", true) == 0;
                        await eval.Eval(instanceNum, eval.cancellationTokenSource.Token, isResume);
                    })
                    .ContinueWith(task =>
                    {
                        Evaluator value;
                        dictEvaluator.TryRemove(guid, out value);
                        // dispose cancellationTokenSource
                        value.cancellationTokenSource.Dispose();
                    });
                    return(eval);
                });

                result = string.Format("Evaluation started for service: {0}.", serviceGuid);
            }
            else if (string.Compare(cmd, "Cancel", true) == 0)
            {
                Evaluator evaluator;
                if (dictEvaluator.TryGetValue(serviceGuid, out evaluator))
                {
                    evaluator.cancellationTokenSource.Cancel();
                    result = string.Format("Evaluation is being cancelled.");
                }
                else
                {
                    result = string.Format("Evaluation is not running.");
                }
            }
            else if (string.Compare(cmd, "Check", true) == 0)
            {
                Evaluator evaluator;
                if (dictEvaluator.TryGetValue(serviceGuid, out evaluator))
                {
                    TimeSpan span = DateTime.UtcNow - evaluator.timeStart;
                    result = string.Format("{0}: processed: {1}, succeeded: {2} / {3}, throughput: {4:F2} images/sec", serviceGuid, evaluator.TriedNumOfImages, evaluator.SucceededNumOfImages, evaluator.TotalNumOfImages,
                                           (float)evaluator.TriedNumOfImages / span.TotalSeconds);
                }
                else
                {
                    result = string.Format("Evaluation for service {0} is not found", serviceGuid);
                }
            }
            else if (string.Compare(cmd, "List", true) == 0)
            {
                var lines = dictEvaluator.Select(kv =>
                {
                    TimeSpan span = DateTime.UtcNow - kv.Value.timeStart;

                    var evaluator = kv.Value;
                    return(string.Format("{0}: processed: {1}, succeeded: {2} / {3}, throughput: {4:F2} images/sec", kv.Key, evaluator.TriedNumOfImages, evaluator.SucceededNumOfImages, evaluator.TotalNumOfImages,
                                         (float)kv.Value.TriedNumOfImages / span.TotalSeconds));
                });
                result = string.Join("\n", lines);
            }
            else
            {
                result = string.Format("Request is not supported: {0}", cmd);
            }

            Console.WriteLine(result);
            return(VHubRecogResultHelper.FixedClassificationResult(result, result));
        }