Issues with binding to Google Mobile Vision for iOS

RichardMasseRichardMasse USMember ✭✭

Hi,

I try to do a binding for Google Mobile Vision for iOS. Everything look good and my project build with the binding.

But when i try to get a detector, i always get a null object.

I already try different dictionary (with and without any value).

var textDetector = GMVDetector.DetectorOfType(Constants.GMVDetectorTypeText, new NSDictionary());

Did anybody already done a binding with success for Google Mobile Vision ?
Did you have any idea what i can look for ?

Posts

  • RichardMasseRichardMasse USMember ✭✭

    For your information,

    My solution was to create a framework with cocoapods in Xcode (objectiv-c) and create a binding to this framework in my xamarin project.
    It's working great. I was able to do OCR and Barcode in iOS with Google Mobile Vision.

  • lalitbokdelalitbokde USMember ✭✭

    Hi Rechard, Can you please elaborate the steps what you did ? or the link to refer ? I am getting the same issue var textDetector = GMVDetector.DetectorOfType(Constants.GMVDetectorTypeText, new NSDictionary()); with null value.
    but not getting the exact solution for it.

  • RichardMasseRichardMasse USMember ✭✭

    Hello,

    I don't have any link to refer. You have to create a framework in Xcode and include . You need to learn how to create a framework in xcode with objective-C. When your framework was done you can use it in Xamarin like other library (with binding project)

    For my need, i simply add this line of code in my framework.

    (NSString *)detectTextInImage:(UIImage *)image {

    textDetector = [GMVDetector detectorOfType:GMVDetectorTypeText options:nil];
    
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    GMVImageOrientation orientation = [GMVUtility
                                       imageOrientationFromOrientation:deviceOrientation
                                       withCaptureDevicePosition:AVCaptureDevicePositionBack
                                       defaultDeviceOrientation: UIDeviceOrientationPortrait];
    NSDictionary *options = @{ GMVDetectorImageOrientation : @(orientation) };
    
    NSArray<GMVFeature *> *features = [textDetector featuresInImage:image options:options];
    
    //NSLog(@"Detected %lu textes.", (unsigned long)features.count);
    
    result = @"";
    
    for (GMVTextBlockFeature *textBlock in features) {
        //NSLog(@"lang: %@ value: %@", textBlock.language, textBlock.value);
    
        // For each text block, iterate over each line.
        for (GMVTextLineFeature *textLine in textBlock.lines) {
            //NSLog(@"lang: %@ value: %@", textLine.language, textLine.value);
    
            // For each line, iterate over each word.
            for (GMVTextElementFeature *textElement in textLine.elements) {
                NSLog(@"value: %@", textElement.value);
                result = [result stringByAppendingString: textElement.value];
            }
        }
    }
    
    return result;
    

    }

    • (NSString *)detectBarcodeInImage:(UIImage *)image {

      NSDictionary *options = @{ GMVDetectorTypeBarcode : @( GMVDetectorBarcodeFormatCode128 | GMVDetectorBarcodeFormatPDF417) };

      barcodeDetector = [GMVDetector detectorOfType:GMVDetectorTypeBarcode options:options];

      NSArray *features = [barcodeDetector featuresInImage:image options:nil];

      //NSLog(@Detected %lu textes., (unsigned long)features.count);

      result = @"";

      for (GMVBarcodeFeature *barcode in features) {
      //NSLog(@lang: %@ value: %@", textBlock.language, textBlock.value);
      result = [result stringByAppendingString: barcode.rawValue];
      }

      return result;
      }

  • Hi @RichardMasse , I'm trying to bind the Google Mobile Vision library, to use the TextDetector for my Xamarin Forms project (Only in the iOS Project, there is already a binding for android).

    But when i'm trying to call GMVDetector.DetectorOfType(Constants.GMVDetectorTypeText, new NSDictionary()); i get a selector instance error.

    Did you change something in the ApiDefinition for the GMVDetector interface or anything else apart from the types?

    I'm already one week trying to implement this binding, any kind of help will be extremely appreciated.

    Thank you very much.

  • RichardMasseRichardMasse USMember ✭✭

    @andymorinigo take a look at my last response in this thread. I have not done a direct binding to the google mobile vision. I have to create a project in xcode and after that do a binding in xamarin for my custom google vision framework.

    @RichardMasse said:

    Hello,

    I don't have any link to refer. You have to create a framework in Xcode and include . You need to learn how to create a framework in xcode with objective-C. When your framework was done you can use it in Xamarin like other library (with binding project)

    For my need, i simply add this line of code in my framework.

    (NSString *)detectTextInImage:(UIImage *)image {

    textDetector = [GMVDetector detectorOfType:GMVDetectorTypeText options:nil];

    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    GMVImageOrientation orientation = [GMVUtility
                                       imageOrientationFromOrientation:deviceOrientation
                                       withCaptureDevicePosition:AVCaptureDevicePositionBack
                                       defaultDeviceOrientation: UIDeviceOrientationPortrait];
    NSDictionary *options = @{ GMVDetectorImageOrientation : @(orientation) };
    
    NSArray<GMVFeature *> *features = [textDetector featuresInImage:image options:options];
    
    //NSLog(@"Detected %lu textes.", (unsigned long)features.count);
    
    result = @"";
    
    for (GMVTextBlockFeature *textBlock in features) {
        //NSLog(@"lang: %@ value: %@", textBlock.language, textBlock.value);
        
        // For each text block, iterate over each line.
        for (GMVTextLineFeature *textLine in textBlock.lines) {
            //NSLog(@"lang: %@ value: %@", textLine.language, textLine.value);
            
            // For each line, iterate over each word.
            for (GMVTextElementFeature *textElement in textLine.elements) {
                NSLog(@"value: %@", textElement.value);
                result = [result stringByAppendingString: textElement.value];
            }
        }
    }
    
    return result;
    

    }

    • (NSString *)detectBarcodeInImage:(UIImage *)image { NSDictionary *options = @{ GMVDetectorTypeBarcode : @( GMVDetectorBarcodeFormatCode128 | GMVDetectorBarcodeFormatPDF417) }; barcodeDetector = [GMVDetector detectorOfType:GMVDetectorTypeBarcode options:options]; NSArray *features = [barcodeDetector featuresInImage:image options:nil]; //NSLog(@Detected %lu textes., (unsigned long)features.count); result = @""; for (GMVBarcodeFeature *barcode in features) {
      //NSLog(@lang: %@ value: %@", textBlock.language, textBlock.value);
      result = [result stringByAppendingString: barcode.rawValue];
      } return result;
      }
  • amnellamnell USMember
    edited November 2018

    I've struggled with this quite a bit over the last couple of days, but I just got things working as I wanted it for my project. I created a framework in Xcode, using cocoapods for the GoogleMobileVision dependencies and then wrapped the functionality in an objective-c class named GoogleMobileVisionWrapper. I've only implemented text recognition, but I guess adding more functionality should be trivial.
    I've created a public repo on Github. This forum prevents me from posting links, so just search for GoogleMobileVision.Bindings.iOS on Github, or Browse the Github username amnell to find it.

    Just run build_and_copy_framework.sh from the project directory to build the frameworks and add them to the GoogleMobileVision.Xamarin.Bindings project. Then just add the Bindings project to your Xamarin project and reference it from the iOS target.
    Using the library is easy. Just use GoogleMobileVisionWrapper().DetectTextInImage(image)

    I'm by no means fluent in Xamarin, so feel free to add pull request

    Worth noting is that the build script creates fat frameworks. So make sure you do proper linking in your project to decrease the binary size.

  • Thanks for your answer @RichardMasse and @amnell.
    I tried to do the way you did Richard, just that my knowledge of objective c and xcode is not helping.
    Thanks so much for sharing your Binding @amnell! I just tried and works perfect, and also helped me to understand how to do it next time i try to bind something like Vision :)

  • HuyVuHuyVu USMember ✭✭

    Hi, i'm new to these kind of Bindings stuff, so could anyone show me how could i reference this binding to my projects?
    I'm really appreciate your help!
    @andymorinigo

Sign In or Register to comment.