public static async Task <List <BarcodeResult> > ScanFromImage(byte[] imageArray) { UIImage image = new UIImage(NSData.FromArray(imageArray)); var visionImage = new VisionImage(image); VisionImageMetadata metadata = new VisionImageMetadata(); VisionApi vision = VisionApi.Create(); VisionBarcodeDetector barcodeDetector = vision.GetBarcodeDetector(Configuration.BarcodeDetectorSupportFormat); VisionBarcode[] barcodes = await barcodeDetector.DetectAsync(visionImage); if (barcodes == null || barcodes.Length == 0) { return(new List <BarcodeResult>()); } List <BarcodeResult> resultList = new List <BarcodeResult>(); foreach (var barcode in barcodes) { resultList.Add(new BarcodeResult { BarcodeType = Methods.ConvertBarcodeResultTypes(barcode.ValueType), DisplayValue = barcode.DisplayValue, RawValue = barcode.RawValue }); } return(resultList); }
public static async Task <List <BarcodeResult> > ScanFromImage(byte[] imageArray) { UIImage image = new UIImage(NSData.FromArray(imageArray)); var visionImage = new VisionImage(image); VisionImageMetadata metadata = new VisionImageMetadata(); VisionApi vision = VisionApi.Create(); VisionBarcodeDetector barcodeDetector = vision.GetBarcodeDetector(Configuration.BarcodeDetectorSupportFormat); VisionBarcode[] barcodes = await barcodeDetector.DetectAsync(visionImage); if (barcodes == null || barcodes.Length == 0) { return(new List <BarcodeResult>()); } List <BarcodeResult> resultList = new List <BarcodeResult>(); foreach (var barcode in barcodes) { var points = barcode.CornerPoints.ToList().ConvertAll(nsvalue => nsvalue.PointFValue); resultList.Add(new BarcodeResult { BarcodeType = Methods.ConvertBarcodeResultTypes(barcode.ValueType), DisplayValue = barcode.DisplayValue, Points = points.Select(p => (p.X / (double)image.Size.Width, p.Y / (double)image.Size.Height)).ToList() });
public void UploadAndGetTagsForImageTest() { var result = VisionApi.UploadAndGetTagsForImage(imageFilePath); Assert.IsNotNull(result); // Console.WriteLine($"==============={result.ToJson()}"); }
public CaptureVideoDelegate() { metadata = new VisionImageMetadata(); vision = VisionApi.Create(); barcodeDetector = vision.GetBarcodeDetector(Configuration.BarcodeDetectorSupportFormat); // Using back-facing camera var devicePosition = AVCaptureDevicePosition.Back; var deviceOrientation = UIDevice.CurrentDevice.Orientation; switch (deviceOrientation) { case UIDeviceOrientation.Portrait: metadata.Orientation = devicePosition == AVCaptureDevicePosition.Front ? VisionDetectorImageOrientation.LeftTop : VisionDetectorImageOrientation.RightTop; break; case UIDeviceOrientation.LandscapeLeft: metadata.Orientation = devicePosition == AVCaptureDevicePosition.Front ? VisionDetectorImageOrientation.BottomLeft : VisionDetectorImageOrientation.TopLeft; break; case UIDeviceOrientation.PortraitUpsideDown: metadata.Orientation = devicePosition == AVCaptureDevicePosition.Front ? VisionDetectorImageOrientation.RightBottom : VisionDetectorImageOrientation.LeftBottom; break; case UIDeviceOrientation.LandscapeRight: metadata.Orientation = devicePosition == AVCaptureDevicePosition.Front ? VisionDetectorImageOrientation.TopRight : VisionDetectorImageOrientation.BottomRight; break; case UIDeviceOrientation.FaceUp: case UIDeviceOrientation.FaceDown: case UIDeviceOrientation.Unknown: metadata.Orientation = VisionDetectorImageOrientation.LeftTop; break; } }
void AnalizeImage() { vision = VisionApi.Create(); switch (currentModelName.ToString()) { case nameof(Model.TextRecognition): UseTextRecognitionModel(); break; case nameof(Model.FaceDetection): UseFaceDetectionModel(); break; case nameof(Model.BarcodeScanning): UseBarcodeScanningModel(); break; case nameof(Model.LandmarkRecognition): UseLandmarkRecognitionModel(); break; default: TxtData.Text = "Model not supported"; break; } }
/// <summary> /// Uses the Microsoft Vision API to generate a picture that crops automatically to whatever size you choose. /// </summary> /// <param name="originalSourcePath">The original SourcePath to a file on Device OR An url to a picture</param> /// <param name="width">Width of the cropped image</param> /// <param name="height">Height of the cropped image</param> /// <param name="addToFilename">What string should be after the originalSourcePath. if original is img20161203.jpg and addToFileName is -thumbnail then the outcome will be img20161203-thumbnail.jpg</param> /// <param name="removeFromOriginalSourceFilename">a string that should be removed from original source ex. originalSourcepath = "Image-fullImage.jpg" removeFromOriginalSourceFilename = "-fullImage" the resulting path string will be "Image"+"addToFilename+".jpg"</param> /// <returns></returns> async public Task <string> SmartCrop(string originalSourcePath, int width, int height, string addToFilename, string removeFromOriginalSourceFilename = null) { string newPath = null; if (string.IsNullOrEmpty(VisionApi.Key)) { throw new Exception("You must set VisionApi.Key"); } var originalBytes = File.ReadAllBytes(originalSourcePath); var thumbNailByteArray = await VisionApi.GetThumbNail(originalBytes, VisionApi.Key, width, height); var orSourcePath = originalSourcePath; if (!string.IsNullOrEmpty(removeFromOriginalSourceFilename)) { orSourcePath = orSourcePath.Replace(removeFromOriginalSourceFilename, ""); } var extension = orSourcePath.Substring(orSourcePath.LastIndexOf(".")); newPath = orSourcePath.Replace(extension, addToFilename + extension); File.WriteAllBytes(newPath, thumbNailByteArray); return(newPath); }
/// <summary> /// Uses the Microsoft Vision API to generate a picture that crops automatically to whatever size you choose. /// </summary> /// <param name="stream">Stream of an image that is used to send to Vision api</param> /// <param name="width">Width of the cropped image</param> /// <param name="height">Height of the cropped image</param> /// <returns>Byte array of new image</returns> async public Task <byte[]> SmartCrop(Stream stream, int width, int height) { if (stream.Length > 4000000) { throw new NotSupportedException("You are trying to SmartCrop a Stream that is bigger than 4Mb"); } return(await VisionApi.GetThumbNail(stream.ToByteArray(), width, height)); }
public ChatPageViewModel(SpellCheckerApi spellCheckerApi, VisionApi visionApi, DialogManager dialogManager) { Check.Required <ArgumentNullException>(() => spellCheckerApi != null); Check.Required <ArgumentNullException>(() => visionApi != null); Check.Required <ArgumentNullException>(() => dialogManager != null); _spellCheckerApi = spellCheckerApi; _visionApi = visionApi; _dialogManager = dialogManager; }
/// <summary> /// Uses the Microsoft Vision API to generate a picture that crops automatically to whatever size you choose. /// </summary> /// <param name="stream">Stream of an image that is used to send to Vision api</param> /// <param name="width">Width of the cropped image</param> /// <param name="height">Height of the cropped image</param> /// <param name="newFilePath">path to file that is going to be created</param> /// <returns>The path to the cropped image</returns> async public Task <string> SmartCrop(Stream stream, int width, int height, string newFilePath) { if (stream.Length > 4000000) { throw new NotSupportedException("You are trying to SmartCrop a Stream that is bigger than 4Mb"); } var thumbNailByteArray = await VisionApi.GetThumbNail(stream.ToByteArray(), width, height); File.WriteAllBytes(newFilePath, thumbNailByteArray); return(newFilePath); }
public void UploadAndAnalyzeInDomainImageTest() { //var imageFilePath = @"E:\Senparc\AzureDemo\WebSite\Senparc.Web\Upload\Emotion\happy.jpg"; var model = new Model() { Name = "categories", Categories = new[] { "人" } }; var result = VisionApi.UploadAndAnalyzeInDomainImage(imageFilePath, model); Assert.IsNotNull(result); //Console.WriteLine($"==============={result.ToJson()}"); }
public void Init() { if (isLocal) { instance = new VisionApi("http://127.0.0.1:18100"); } else { instance = new VisionApi(); } instance.Configuration.AccessToken = Environment.GetEnvironmentVariable("SPHEREON_TEST_ACCESSTOKEN"); }
/// <summary> /// Uses the Microsoft Vision API to generate a picture that crops automatically to whatever size you choose. /// </summary> /// <param name="originalSourcePath">The original SourcePath to a file on Device OR An url to a picture</param> /// <param name="width">Width of the cropped image</param> /// <param name="height">Height of the cropped image</param> /// <returns>Byte array of new image</returns> async public Task <byte[]> SmartCrop(string originalSourcePath, int width, int height) { if (originalSourcePath.IsUrl()) { return(await VisionApi.GetThumbNail(originalSourcePath, width, height)); } else { var downSampledPath = await FixMaxImageSize(originalSourcePath, 4000000); var originalBytes = File.ReadAllBytes(downSampledPath); return(await VisionApi.GetThumbNail(originalBytes, width, height)); } }
/// <summary> /// <see cref="LabelReaderBase.GetFullTextFromImage(object)"/> /// </summary> /// <param name="image"></param> /// <returns></returns> public override async Task <string> GetFullTextFromImageAsync(object image) { CoreMedia.CMSampleBuffer iOSImage = (CoreMedia.CMSampleBuffer)image; VisionImage visionImage = new VisionImage(iOSImage); visionImage.Metadata = new VisionImageMetadata { Orientation = GetOrientation() }; VisionApi api = VisionApi.Create(); VisionTextRecognizer textRecognizer = api.GetOnDeviceTextRecognizer(); VisionText textResult = await textRecognizer.ProcessImageAsync(visionImage); return(textResult?.Text); }
/// <summary> /// <see cref="LabelReaderBase.GetRawBarcodeTextFromImageAsync(object)"/> /// </summary> /// <param name="image"></param> /// <returns></returns> public override async Task <string> GetRawBarcodeTextFromImageAsync(object image) { CoreMedia.CMSampleBuffer iOSImage = (CoreMedia.CMSampleBuffer)image; VisionImage visionImage = new VisionImage(iOSImage); visionImage.Metadata = new VisionImageMetadata { Orientation = GetOrientation() }; VisionApi api = VisionApi.Create(); VisionBarcodeDetector barcodeDetector = api.GetBarcodeDetector(new VisionBarcodeDetectorOptions(VisionBarcodeFormat.QRCode)); VisionBarcode[] barcodes = await barcodeDetector.DetectAsync(visionImage); if (barcodes.Length <= 0) { return(String.Empty); } return(barcodes.First().RawValue); }
/// <summary> /// 获取计算机视觉 /// </summary> /// <param name="picUrl"></param> /// <param name="name"></param> /// <returns></returns> public Input GetVisionInput(string picUrl, string name) { var absoluteUrl = Server.GetMapPath(picUrl.StartsWith("~/") ? picUrl : "~" + picUrl); var result = VisionApi.UploadAndGetTagsForImage(absoluteUrl); var data = new VisionParameters() { Pic = picUrl, Result = result != null ? result.Tags.Select(z => new VisionTags(z.Confidence, z.Hint, z.Name)).ToList() : new List <VisionTags>() }; var currentInput = new Input() { new VisionApiParameterSetting(name, data) }; return(currentInput); }
/// <summary> /// Uses the Microsoft Vision API to generate a picture that crops automatically to whatever size you choose. /// </summary> /// <param name="originalSourcePath">The original SourcePath to a file on Device OR An url to a picture</param> /// <param name="width">Width of the cropped image</param> /// <param name="height">Height of the cropped image</param> /// <param name="addToFilename">What string should be after the originalSourcePath. if original is img20161203.jpg and addToFileName is -thumbnail then the outcome will be img20161203-thumbnail.jpg</param> /// <param name="removeFromOriginalSourceFilename">a string that should be removed from original source ex. originalSourcepath = "Image-fullImage.jpg" removeFromOriginalSourceFilename = "-fullImage" the resulting path string will be "Image"+"addToFilename+".jpg"</param> /// <returns></returns> public async Task <string> SmartCrop(string originalSourcePath, int width, int height, string addToFilename, string removeFromOriginalSourceFilename = null) { string newPath = null; byte[] thumbNailByteArray = null; if (originalSourcePath.IsUrl()) { thumbNailByteArray = await VisionApi.GetThumbNail(originalSourcePath, width, height); } else { var downSampledPath = await FixMaxImageSize(originalSourcePath, 4000000); var originalBytes = File.ReadAllBytes(downSampledPath); thumbNailByteArray = await VisionApi.GetThumbNail(originalBytes, width, height); } newPath = SetupNewSourcePath(originalSourcePath, removeFromOriginalSourceFilename, addToFilename); File.WriteAllBytes(newPath, thumbNailByteArray); return(newPath); }
void AnalizeImage() { vision = VisionApi.Create(); switch (currentModelName.ToString()) { case nameof(Model.TextRecognition): UseTextRecognitionModel(); break; case nameof(Model.FaceDetection): UseFaceDetectionModel(); break; case nameof(Model.BarcodeScanning): UseBarcodeScanningModel(); break; case nameof(Model.ImageLabeling): if (currentApiResource == ApiResource.OnDevice) { UseImageLabelingModel(); } else { UseCloudImageLabelingModel(); } break; case nameof(Model.LandmarkRecognition): UseLandmarkRecognitionModel(); break; default: TxtData.Text = "Model not supported"; break; } }
public ActionResult Edit( [Bind(Prefix = "AppRedPackageActivity")] APP_RedPackage_Activity redPackageActivity_Form, HttpPostedFileBase[] picContainerApiFile) { picContainerApiFile = picContainerApiFile ?? new HttpPostedFileBase[0]; bool isEdit = redPackageActivity_Form.Id > 0; this.Validator(redPackageActivity_Form.Name, "活动名称", "AppRedPackageActivity.Name", false); this.Validator(redPackageActivity_Form.BeginTime, "开始时间", "AppRedPackageActivity.BeginTime", false); this.Validator(redPackageActivity_Form.EndTime, "结束时间", "AppRedPackageActivity.EndTime", false) .IsFalse(z => DateTime.Compare(redPackageActivity_Form.BeginTime, z) > 0, "结束时间不能小于当前时间", true); //解析所有图片并且调用API var moduleList = redPackageActivity_Form.Rule.FromJson <List <BaseModule> >(); var picContainerApiModuleList = moduleList.Where(z => z is PicContainerApiModule).ToList(); int i = 0; //if (picContainerApiFile.Length > 0) foreach (var file in picContainerApiFile) { // HttpPostedFileBase hpf = file as HttpPostedFileBase; if (file == null || file.ContentLength == 0) { i++; continue; } string uploadResult = Upload.UpLoadProductPic(file); bool uploadSuccess = Upload.CheckUploadSuccessful(uploadResult); if (uploadSuccess) { //调用计算机视觉API TODO:图片有可能会失败 var result = VisionApi.UploadAndGetTagsForImage(Server.MapPath(uploadResult)); ((PicContainerApiModule)picContainerApiModuleList[i]).Parameters = new VisionParameters() { Pic = uploadResult, Result = result != null ? result.Tags.Select(z => new VisionTags(z.Confidence, z.Hint, z.Name)).ToList() : new List <VisionTags>() }; } i++; } if (!ModelState.IsValid) { var vd = new RedPackageActivity_EditVD() { AppRedPackageActivity = redPackageActivity_Form, ModuleTemplateList = new List <BaseModule>() { new RegisterModule(), new RegisterImageModule(), new EmotionApiModule(), new RedPackageResultModule(), new GradeResultModule(), new VisionApiModule(), new PicContainerApiModule(), new ContrastPicModule() }, }; return(View(vd)); } APP_RedPackage_Activity appRedPackageActivity = null; if (isEdit) { appRedPackageActivity = _appRedPackageActivityService.GetObject(redPackageActivity_Form.Id); if (appRedPackageActivity == null) { return(RenderError("活动不存在!")); } } else { appRedPackageActivity = new APP_RedPackage_Activity() { AddTime = DateTime.Now, TotalMoney = 0, }; } TryUpdateModel(appRedPackageActivity, "AppRedPackageActivity", null, new[] { "Id", "TotalMoney", "RemainingMoney" }); appRedPackageActivity.Rule = moduleList.SerializeToString(); appRedPackageActivity.RemainingMoney += redPackageActivity_Form.RemainingMoney; appRedPackageActivity.TotalMoney += redPackageActivity_Form.RemainingMoney; _appRedPackageActivityService.SaveObject(appRedPackageActivity); SetMessager(MessageType.success, "保存成功。"); //清除当前活动所有缓存 var fullAccountWorkFlowCache = ObjectFactory.GetInstance <FullAccountWorkFlowCache>(); fullAccountWorkFlowCache.RemoveActivityAllFullAccountWorkFlow(appRedPackageActivity.Id); return(RedirectToAction("Edit", "RedPackageActivity", new { id = appRedPackageActivity.Id })); }