Ejemplo n.º 1
0
        public async override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
        {
            var imageSet = new ImageSet();

            var provider = await content.ReadAsMultipartAsync();

            //extract model
            var modelContent = provider.Contents.FirstOrDefault(c => c.Headers.ContentDisposition.Name.NormalizeName() == "imageset");
            if (modelContent != null)
            {
                imageSet = await modelContent.ReadAsAsync<ImageSet>();
            }

            if (!imageSet.Images.Any())
            {
                //try extract from image content
                var fileContents = provider.Contents.Where(c => c.Headers.ContentDisposition.Name.NormalizeName().Matches(@"image\d+")).ToList();
                foreach (var fileContent in fileContents)
                {
                    imageSet.Images.Add(new Image
                    {
                        FileName = fileContent.Headers.ContentDisposition.FileName.NormalizeName(),
                        MimeType = fileContent.Headers.ContentType.MediaType,
                        Side = GetSide(fileContent),
                        ImageData = await fileContent.ReadAsByteArrayAsync()
                    });
                }
            }

            return imageSet;
        }
Ejemplo n.º 2
0
        public Dictionary<string, object> Recognize(ImageSet model)
        {
            var res = new Dictionary<string, object>();

            string condition = model.Name ?? "";
            int i = 0;

            foreach (var image in model.Images)
            {
                if (i > 1)
                {
                    break;
                }

                switch (image.Side)
                {
                    case SideEnum.None:
                        if (i == 0)
                        {
                            condition = string.Concat(condition, "_front");
                        }
                        else if (i == 1)
                        {
                            condition = string.Concat(condition, "_back");
                        }
                        break;
                    case SideEnum.Front:
                        condition = string.Concat(condition, "_front");
                        break;
                    case SideEnum.Back:
                        condition = string.Concat(condition, "_back");
                        break;
                }

                Logger.Instance.InfoFormat("Start recognize setName: {0}, side: {1}, imageName: {2}, mimeType: {3}, length: {4}, condition: {5}",
                    !string.IsNullOrEmpty(model.Name) ? model.Name : "n/a",
                    image.Side,
                    image.FileName,
                    image.MimeType,
                    image.ImageData.Length,
                    condition);

                var dic = RecognitionProcessor.Instance.GetRecognizeData(image.ImageData, condition);
                foreach (var d in dic)
                {
                    res.Add(d.Key, d.Value);
                }

                i++;
            }

            Logger.Instance.InfoFormat("Return recognize data with {0} key(s)", res.Keys.Count);

            return res;
        }
Ejemplo n.º 3
0
        public static void Test1()
        {
            string fileName = "op_1_front.jpg";

            System.Drawing.Image imgFile = System.Drawing.Image.FromFile(fileName);
            var imageSet = new ImageSet()
            {
                Name = "op_1",
            };

            var image = new Image()
            {
                FileName = fileName,
                MimeType = "image/jpeg",
                Side = SideEnum.Front,
                ImageData = imgFile.ToArray(System.Drawing.Imaging.ImageFormat.Jpeg),
            };

            imageSet.Images.Add(image);

            var multipartContent = new MultipartFormDataContent();

            var imageSetJson = JsonConvert.SerializeObject(imageSet, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });

            multipartContent.Add(new StringContent(imageSetJson, Encoding.UTF8, "application/json"), "imageset");

            int counter = 0;
            foreach (var img in imageSet.Images)
            {
                var imageContent = new ByteArrayContent(img.ImageData);
                imageContent.Headers.ContentType = new MediaTypeHeaderValue(img.MimeType);
                imageContent.Headers.Add("Side", img.Side.ToString());
                multipartContent.Add(imageContent, "image" + counter++, img.FileName);
            }

            string url = "http://93.153.125.236/Impresto.Ocr/api/recognize";
            //string url = "http://localhost:65000/api/recognize";

            var response = new HttpClient()
                .PostAsync(url, multipartContent)
                .Result;

            var responseContent = response.Content.ReadAsStringAsync().Result;

            Console.WriteLine(responseContent);
        }