Text Recognition From Image Using Firebase ML Kit — iOS Development (Swift)
Hello friends, today I will tell you how to text recognition in image using Firebase ML (Machine Learning) Kit and Swift 5. Firstly, what is Firebase ML Kit ?
The definition of Google for Firebase ML Kit is " ML Kit is a mobile SDK that brings Google’s machine learning expertise to Android and iOS apps in a powerful yet easy-to-use package " . So briefly, we can use Google's trained machine learning models in our mobile apps.
To use the Firebase ML Kit I open an iOS application from the Firebase console. (I’m not going to show this in this article) Then we add google.plist to our project and connect the project with Firebase. As pod, we add the following pods to our project and run the “ pod install ” command from the terminal.
2 ways to do text recognition with using Firebase ML Kit:
- On-Device Text Recognition
- Cloud Text Recognition
We will use On-Device Text Recognition in this project. In the project, the user will upload a photo with text in it, and we will recognize it and print it on the screen. The UI of the application is as follows.
After editing the UI, we import the “FirebaseMLVision” library into the View Controller.
And we need to implement UIImagePickerControllerDelegate & UINavigationControllerDelegate for taking images from user. We should add "Privacy — Photo Library Usage Description" and "Privacy — Camera Usage Description" in "Info.plist" for access photo library of user.
We create object of "VisionTextRecognizer" using "vision" object. After create imagePicker object and set the delegate property self for taking images.
Now, we can implement delegate methods. We unwrapping picked image, set image of "textImage" UIImageView and send "userPickedImage" to "runTextRecognition" method for recognition text from picked image in "imagePickerController" delegate method.
In the "runTextRecognition" method, we take user's picked image. We create a "VisionImage" object using user's picked image. We send the "visionImage" object to the "process" method of the "textRecognizer" object that we created before. I unwrap "features" using "guard let" that means if "features" is return nil app will crash and send "fatalError" else text of "textView" is set recognized text in the user's picked image.
The final version of the application is as follows. But remember Firebase ML Kit is still in Beta, so it might be a little buggy :)
Thank you for reading this article. If you have any questions, you can contact me. I would appreciate it if you clap my article. Hope to see you in other articles in the future, take care of yourself :)