google_vision 1.2.0 google_vision: ^1.2.0 copied to clipboard
Allows you to add Google Visions image labeling, face, logo, and landmark detection, OCR, and detection of explicit content, into applications.
Google Vision Images REST API Client #
Native Dart package that integrates Google Vision features, including image labeling, face, logo, and landmark detection, optical character recognition (OCR), and detection of explicit content, into applications.
- Cloud Vision API - Documentation Reference
Please feel free to submit PRs for any additional helper methods, or report an issue for a missing helper method and I'll add it if I have time available.
Table of Contents #
- Recent Changes
- Getting Started
- Usage with Flutter
- Vision cli (google_vision at the command prompt)
- Contributors
- Contributing
Recent Changes #
New for v1.0.8 #
- web entities and pages detection https://cloud.google.com/vision/docs/detecting-web, provides urls of web pages that match the specified image
New for v1.0.7 #
JLuisRojas has provided code for:
- detect text in images
- detect handwriting in images
In addition support for the following has also been added:
- detect crop hints
- detect image properties
- detect landmarks
- detect logos
New for v1.0.6 #
imhafeez has provided code for Safe Search
functionality.
New for v1.0.3 #
4ctobias has provided code for Label Detection
functionality.
Getting Started #
pubspec.yaml #
To use this package, add the dependency to your pubspec.yaml
file:
dependencies:
...
google_vision: ^1.1.1+3
Obtaining Authorization Credentials #
Authenticating to the Cloud Vision API requires a JSON file with the JWT token information, which you can obtain by creating a service account in the API console.
Usage of the Cloud Vision API #
final googleVision =
await GoogleVision.withJwtFile('my_jwt_credentials.json');
final painter =
Painter.fromFilePath('example/young-man-smiling-and-thumbs-up.jpg');
// cropping an image can save time uploading the image to Google
final cropped = painter.copyCrop(70, 30, 640, 480);
// to see the cropped image
await cropped.writeAsJpeg('example/cropped.jpg');
final requests = AnnotationRequests(requests: [
AnnotationRequest(image: cropped, features: [
Feature(maxResults: 10, type: 'FACE_DETECTION'),
Feature(maxResults: 10, type: 'OBJECT_LOCALIZATION')
])
]);
final annotatedResponses =
await googleVision.annotate(requests: requests);
for (var annotatedResponse in annotatedResponses.responses) {
for (var faceAnnotation in annotatedResponse.faceAnnotations) {
GoogleVision.drawText(
cropped,
faceAnnotation.boundingPoly.vertices.first.x + 2,
faceAnnotation.boundingPoly.vertices.first.y + 2,
'Face - ${faceAnnotation.detectionConfidence}');
GoogleVision.drawAnnotations(
cropped, faceAnnotation.boundingPoly.vertices);
}
}
for (var annotatedResponse in annotatedResponses.responses) {
annotatedResponse.localizedObjectAnnotations
.where((localizedObjectAnnotation) =>
localizedObjectAnnotation.name == 'Person')
.toList()
.forEach((localizedObjectAnnotation) {
GoogleVision.drawText(
cropped,
(localizedObjectAnnotation.boundingPoly.normalizedVertices.first.x *
cropped.width)
.toInt(),
(localizedObjectAnnotation.boundingPoly.normalizedVertices.first.y *
cropped.height)
.toInt() -
16,
'Person - ${localizedObjectAnnotation.score}');
GoogleVision.drawAnnotationsNormalized(
cropped, localizedObjectAnnotation.boundingPoly.normalizedVertices);
});
}
// output the results as a new image file
await cropped.writeAsJpeg('resulting_image.jpg');
Usage with Flutter #
For a quick intro into the use of this package in a Flutter app see take a look at the vision_demo
sample app in the example/flutter
folder of the project's GitHub repository.
In getting the package to work with Flutter it's usually necessary to convert an object that is presented as an Asset
or a Stream
into a File
for use by the google_vision
package. The vision_demo
app gives sample code that shows how to convert an Asset
into a File
. A similar process can be used for any Stream
of data that represents an image supported by google_vision
.
Vision cli (google_vision at the command prompt) #
This package included a cli utility that can be used to return data for any API call currently supported by the package. If you want to get started quickly with the cli utility run these commands in a terminal session:
Install using dart pub
:
dart pub global activate google_vision
Install using brew
:
brew tap faithoflifedev/google_vision
brew install vision
Run the following command to see help:
vision --help
Result,
A command line interface for making API requests to the Google Vision.
Usage: vision <command> [arguments]
Global options:
-h, --help Print this usage information.
--credential-file=<credentials file path> (defaults to "/Users/chris/.vision/credentials.json")
Available commands:
crop_hints Set of crop hints that are used to generate new crops when serving images.
detect Run image detection and annotation for an images.
highlight Draw a box to highlight any objects detected.
safe_search SafeSearch Detection detects explicit content such as adult content or violent content within an image.
score For OBJECT_LOCALIZATION, get the score(s) for the object specified with "look-for".
Please see the cli documentation README.md for more detailed usage information.
Contributors #
Contributing #
Any help from the open-source community is always welcome and needed:
- Found an issue?
- Please fill a bug report with details.
- Need a feature?
- Open a feature request with use cases.
- Are you using and liking the project?
- Promote the project: create an article or post about it
- Make a donation
- Do you have a project that uses this package
- let's cross promote, let me know and I'll add a link to your project
- Are you a developer?
- Fix a bug and send a pull request.
- Implement a new feature.
- Improve the Unit Tests.
- Have you already helped in any way?
- Many thanks from me, the contributors and everybody that uses this project!
If you donate 1 hour of your time, you can contribute a lot, because others will do the same, just be part and start with your 1 hour.