Skip to content

Commit

Permalink
web platform support
Browse files Browse the repository at this point in the history
  • Loading branch information
faithoflifedev committed Sep 8, 2024
1 parent 0eae6a5 commit 9599ac4
Show file tree
Hide file tree
Showing 33 changed files with 319 additions and 569 deletions.
10 changes: 6 additions & 4 deletions packages/google_vision/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Changelog

## 1.4.0
## 2.0.0

* switch to Singleton
* added web platform support
* removed deprecated classes and methods
* logging with the `loggy` package

## 1.4.0
## 1.4.0+1

* switch to Singleton
* better reporting of web platform support

## 1.4.0

Expand Down
17 changes: 10 additions & 7 deletions packages/google_vision/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Google Vision Images REST API Client


[![pub package](https://img.shields.io/pub/v/google_vision.svg)](https://pub.dartlang.org/packages/google_vision)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Expand All @@ -11,9 +10,9 @@ Native [Dart](https://dart.dev/) package that integrates Google Vision features,
- [Google Vision Images REST API Client](#google-vision-images-rest-api-client)
- [Project Status](#project-status)
- [Recent Changes](#recent-changes)
- [New for v2.0.0](#new-for-v200)
- [New for v1.4.0](#new-for-v140)
- [New for v1.3.0](#new-for-v130)
- [New for v1.2.0](#new-for-v120)
- [Getting Started](#getting-started)
- [pubspec.yaml](#pubspecyaml)
- [Obtaining Authentication/Authorization Credentials](#obtaining-authenticationauthorization-credentials)
Expand All @@ -25,7 +24,6 @@ Native [Dart](https://dart.dev/) package that integrates Google Vision features,
- [Contributors](#contributors)
- [Contributing](#contributing)


## Project Status

[![Build Status](https://github.com/faithoflifedev/google_vision/workflows/Dart/badge.svg)](https://github.com/faithoflifedev/google_vision/actions) [![github last commit](https://shields.io/github/last-commit/faithoflifedev/google_vision)](https://shields.io/github/last-commit/faithoflifedev/google_vision) [![github build](https://img.shields.io/github/actions/workflow/status/faithoflifedev/google_vision_workspace/dart.yaml?branch=main)](https://shields.io/github/workflow/status/faithoflifedev/google_vision/Dart) [![github issues](https://shields.io/github/issues/faithoflifedev/google_vision)](https://shields.io/github/issues/faithoflifedev/google_vision)
Expand All @@ -36,6 +34,14 @@ Please feel free to submit PRs for any additional helper methods, or report an [

## Recent Changes

### New for v2.0.0
- Even though this package worked when used with the `web` platform the **pub.dev** analyzer would not show it as `web` platform compatible due to the use of the `universal_io` package which has a dependency on `dart:io`. This version has removed the `universal_io` dependency from the core package, so some related method signatures have been removed.
- The deprecated methods from in v1.3.x have been removed in this version.
- Logging functionality has been added to the package
```dart
final googleVision = await GoogleVision(LogLevel.all).withJwtFile('service_credentials.json');
```

### New for v1.4.0
- A breaking change from the previous version is that the `GoogleVision` class now follows the Singleton design pattern. Now the object is instantiated as follows:
```dart
Expand All @@ -51,9 +57,6 @@ final googleVision = await GoogleVision().withJwtFile('service_credentials.json'
- This version of the package supports both the `image` and `file` annotation APIs for Google Vision. The previous versions of the package supported only the `image` API.
- A number of methods and classes have been **Deprecated** in this version. All the provided examples still work without any changes, so the changes in this package should not cause any issue to existing code.
- The `file` functionality added to this release allows for the annotation of file formats that have pages or frames, specifically `pdf`, `tiff` and `gif`. Google Vision allows annotation of up to 5 pages/frames.

### New for v1.2.0
- helper methods that simplify any `single` detection so a simple face detection can be performed with the `faceDetection(JsonImage jsonImage)` method, see the table below.

## Getting Started

Expand All @@ -64,7 +67,7 @@ To use this package, add the dependency to your `pubspec.yaml` file:
```yaml
dependencies:
...
google_vision: ^1.4.0
google_vision: ^2.0.0
```
### Obtaining Authentication/Authorization Credentials
Expand Down
16 changes: 9 additions & 7 deletions packages/google_vision/bin/vision.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import 'package:args/command_runner.dart';
import 'package:google_vision/google_vision.dart';
import 'package:google_vision/google_vision_cli.dart';
import 'package:universal_io/io.dart';

/// Attempt to retrieve the 'home' folder of the user if running on a desktop.
String? get _userHome =>
Platform.environment['HOME'] ?? Platform.environment['USERPROFILE'];

void main(List<String> arguments) async {
CommandRunner('vision',
'A command line interface for making API requests to the Google Vision.')
..argParser.addOption(
'pages',
abbr: 'p',
valueHelp: 'comma delimited list of pages to process (max 5)',
)
..argParser.addOption('credential-file',
defaultsTo: '${Util.userHome}/.vision/credentials.json',
defaultsTo: '$_userHome/.vision/credentials.json',
valueHelp: 'credentials file path')
..argParser.addOption('log-level',
allowed: ['all', 'debug', 'info', 'warning', 'error', 'off'],
defaultsTo: 'off')
..addCommand(VisionCropHintCommand())
..addCommand(VisionDetectCommand())
..addCommand(VisionHighlightCommand())
Expand Down
10 changes: 7 additions & 3 deletions packages/google_vision/example/document_text_detection_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import 'package:google_vision/google_vision.dart';
import 'package:universal_io/io.dart';

void main() async {
final googleVision =
await GoogleVision().withJwtFile('service_credentials.json');
final googleVision = await GoogleVision()
.withJwt(File('service_credentials.json').readAsStringSync());

print('checking...');

const int page = 1;

const fileName = 'sample_image/allswell.pdf';

final file = File(fileName);

final annotateFileResponses = await googleVision.file.documentTextDetection(
InputConfig.fromFilePath('sample_image/allswell.pdf'),
InputConfig.fromBuffer(file.readAsBytesSync().buffer),
pages: [page],
);

Expand Down
11 changes: 7 additions & 4 deletions packages/google_vision/example/doument_text_detection.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import 'package:google_vision/google_vision.dart';
import 'package:universal_io/io.dart';

void main() async {
final googleVision =
await GoogleVision().withJwtFile('service_credentials.json');
final googleVision = await GoogleVision()
.withJwt(File('service_credentials.json').readAsStringSync());

final inputImage = await File('sample_image/census2010.jpg').readAsBytes();

print('checking...');

final fullTextAnnotation = await googleVision.documentTextDetection(
JsonImage.fromFilePath('sample_image/census2010.jpg'));
final fullTextAnnotation = await googleVision.image
.documentTextDetection(JsonImage.fromBuffer(inputImage.buffer));

for (var page in fullTextAnnotation!.pages) {
print('Page Language: ${page.property?.detectedLanguages}');
Expand Down
32 changes: 17 additions & 15 deletions packages/google_vision/example/example.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import 'package:google_vision/google_vision.dart';
import 'package:universal_io/io.dart';

void main() async {
final googleVision =
await GoogleVision().withJwtFile('service_credentials.json');
final googleVision = await GoogleVision()
.withJwt(File('service_credentials.json').readAsStringSync());

final requests = AnnotationRequests(requests: [
AnnotationRequest(
// You may want to crop or shrink the image to the area of interest to
// safe bandwidth/upload time, but that's optional.
jsonImage: JsonImage.fromFilePath(
'sample_image/young-man-smiling-and-thumbs-up.jpg'),
features: [
Feature(maxResults: 10, type: AnnotationType.faceDetection),
Feature(maxResults: 10, type: AnnotationType.objectLocalization),
])
]);
final requests = [
AnnotateImageRequest(
// You may want to crop or shrink the image to the area of interest to
// safe bandwidth/upload time, but that's optional.
jsonImage: JsonImage.fromGsUri(
'gs://gvision-demo/young-man-smiling-and-thumbs-up.jpg'),
features: [
Feature(maxResults: 10, type: AnnotationType.faceDetection),
Feature(maxResults: 10, type: AnnotationType.objectLocalization),
],
)
];

print('checking...');

AnnotatedResponses annotatedResponses =
await googleVision.annotate(requests: requests);
final annotatedResponses =
await googleVision.image.annotate(requests: requests);

print(annotatedResponses.responses.first.annotations);
}
5 changes: 3 additions & 2 deletions packages/google_vision/example/label_detection.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:google_vision/google_vision.dart';
import 'package:universal_io/io.dart';

void main() async {
final googleVision =
await GoogleVision().withJwtFile('service_credentials.json');
final googleVision = await GoogleVision()
.withJwt(File('service_credentials.json').readAsStringSync());

print('checking...');

Expand Down
9 changes: 6 additions & 3 deletions packages/google_vision/example/landmark_detection.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import 'package:google_vision/google_vision.dart';
import 'package:universal_io/io.dart';

void main() async {
final googleVision =
await GoogleVision().withJwtFile('service_credentials.json');
final googleVision = await GoogleVision()
.withJwt(File('service_credentials.json').readAsStringSync());

final inputImage = await File('sample_image/cn_tower.jpg').readAsBytes();

print('checking...');

final landmarkAnnotationsResponse = await googleVision.image
.labelDetection(JsonImage.fromFilePath('sample_image/cn_tower.jpg'));
.labelDetection(JsonImage.fromBuffer(inputImage.buffer));

for (var landmarkAnnotation in landmarkAnnotationsResponse) {
print('score: ${landmarkAnnotation.score}');
Expand Down
9 changes: 6 additions & 3 deletions packages/google_vision/example/logo_detection.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import 'package:google_vision/google_vision.dart';
import 'package:universal_io/io.dart';

void main() async {
final googleVision =
await GoogleVision().withJwtFile('service_credentials.json');
final googleVision = await GoogleVision()
.withJwt(File('service_credentials.json').readAsStringSync());

final inputImage = await File('sample_image/logo.png').readAsBytes();

print('checking...');

final logoAnnotationsResponses = await googleVision.image
.logoDetection(JsonImage.fromFilePath('sample_image/logo.png'));
.logoDetection(JsonImage.fromBuffer(inputImage.buffer));

for (var logoAnnotation in logoAnnotationsResponses) {
print('Logo: ${logoAnnotation.description}');
Expand Down
1 change: 1 addition & 0 deletions packages/google_vision/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ dependencies:
pcanvas: ^1.1.0
google_vision:
path: ../
mime: ^1.0.6
universal_io: ^2.2.2
22 changes: 10 additions & 12 deletions packages/google_vision/example/text_detection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ import 'package:google_vision/google_vision.dart';
import 'package:universal_io/io.dart';

void main() async {
final googleVision =
await GoogleVision().withJwtFile('service_credentials.json');
final googleVision = await GoogleVision()
.withJwt(File('service_credentials.json').readAsStringSync());

final imageFile = File('sample_image/structures.png').readAsBytesSync();

print('checking...');

final annotatedResponses = await googleVision.annotate(
requests: AnnotationRequests(
requests: [
AnnotationRequest(
jsonImage: JsonImage(byteBuffer: imageFile.buffer),
features: [
Feature(maxResults: 10, type: AnnotationType.textDetection)
])
],
),
final annotatedResponses = await googleVision.image.annotate(
requests: [
AnnotateImageRequest(
jsonImage: JsonImage(byteBuffer: imageFile.buffer),
features: [
Feature(maxResults: 10, type: AnnotationType.textDetection)
])
],
);

for (var textAnnotation
Expand Down
23 changes: 11 additions & 12 deletions packages/google_vision/example/web_detection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ import 'package:google_vision/google_vision.dart';
import 'package:universal_io/io.dart';

void main() async {
final googleVision =
await GoogleVision().withJwtFile('service_credentials.json');
final googleVision = await GoogleVision()
.withJwt(File('service_credentials.json').readAsStringSync());

final imageFile = File('sample_image/structures.png').readAsBytesSync();

print('checking...');

AnnotatedResponses annotatedResponses = await googleVision.annotate(
requests: AnnotateImageRequests(
requests: [
AnnotateImageRequest(
jsonImage: JsonImage(byteBuffer: imageFile.buffer),
features: [
Feature(maxResults: 10, type: AnnotationType.webDetection)
])
],
),
BatchAnnotateImagesResponse annotatedResponses =
await googleVision.image.annotate(
requests: [
AnnotateImageRequest(
jsonImage: JsonImage(byteBuffer: imageFile.buffer),
features: [
Feature(maxResults: 10, type: AnnotationType.webDetection)
])
],
);

print(annotatedResponses);
Expand Down
10 changes: 0 additions & 10 deletions packages/google_vision/lib/google_vision.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,11 @@ export 'src/google_vision_image.dart';
export 'src/annotate_json_serializable.dart';
export 'src/token_generator.dart';

export 'src/cmd/vision_crop_hint_command.dart';
export 'src/cmd/vision_detect_command.dart';
export 'src/cmd/vision_helper_command.dart';
export 'src/cmd/vision_highlight_command.dart';
export 'src/cmd/vision_safe_search_command.dart';
export 'src/cmd/vision_score_command.dart';
export 'src/cmd/vision_version_command.dart';

export 'src/model/annotate_file_request.dart';
export 'src/model/annotate_file_response.dart';
export 'src/model/annotate_image_response.dart';
export 'src/model/batch_annotate_images_response.dart';
export 'src/model/annotate_image_request.dart';
// TODO: remove this depricated class in the next verion
export 'src/model/annotate_image_requests.dart';
export 'src/model/batch_annotate_files_response.dart';
export 'src/model/block.dart';
export 'src/model/bounding_poly.dart';
Expand Down
9 changes: 9 additions & 0 deletions packages/google_vision/lib/google_vision_cli.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
library google_vision_cli;

export 'src/cmd/vision_crop_hint_command.dart';
export 'src/cmd/vision_detect_command.dart';
export 'src/cmd/vision_helper_command.dart';
export 'src/cmd/vision_highlight_command.dart';
export 'src/cmd/vision_safe_search_command.dart';
export 'src/cmd/vision_score_command.dart';
export 'src/cmd/vision_version_command.dart';
2 changes: 1 addition & 1 deletion packages/google_vision/lib/meta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ library meta;
import 'dart:convert' show json;

final pubSpec = json.decode(
'{"name":"google_vision","version":"1.4.0","homepage":"https://github.com/faithoflifedev/google_vision/tree/main/packages/google_vision","environment":{"sdk":">=3.2.0 <4.0.0"},"description":"Allows you to add Google Visions image labeling, face, logo, and landmark detection, OCR, and detection of explicit content, into cross platform applications.","dependencies":{"args":"^2.5.0","collection":"^1.18.0","crypto_keys_plus":"^0.4.0","dio":"^5.6.0","http":"^1.2.2","image":"^4.1.7","jose_plus":"^0.4.6","json_annotation":"^4.9.0","mime":"^1.0.6","retrofit":"^4.2.0","universal_io":"^2.2.2"},"dev_dependencies":{"build_runner":"^2.4.11","grinder":"^0.9.5","json_serializable":"^6.8.0","lints":"^4.0.0","publish_tools":"^1.0.0+4","retrofit_generator":"^8.2.1"},"executables":{"vision":""},"repository":"https://github.com/faithoflifedev/google_vision","funding":["https://www.buymeacoffee.com/faithoflif2"]}');
'{"name":"google_vision","version":"2.0.0","homepage":"https://github.com/faithoflifedev/google_vision/tree/main/packages/google_vision","environment":{"sdk":">=3.2.0 <4.0.0"},"description":"Allows you to add Google Visions image labeling, face, logo, and landmark detection, OCR, and detection of explicit content, into cross platform applications.","dependencies":{"args":"^2.5.0","collection":"^1.18.0","crypto_keys_plus":"^0.4.0","dio":"^5.7.0","flutter_loggy_dio":"^3.1.0","http":"^1.2.2","image":"^4.1.7","jose_plus":"^0.4.6","json_annotation":"^4.9.0","loggy":"^2.0.3","mime":"^1.0.6","retrofit":"^4.4.0","universal_io":"^2.2.2","universal_platform":"^1.1.0"},"dev_dependencies":{"build_runner":"^2.4.11","grinder":"^0.9.5","json_serializable":"^6.8.0","lints":"^4.0.0","publish_tools":"^1.0.0+4","retrofit_generator":"^8.2.1"},"executables":{"vision":""},"repository":"https://github.com/faithoflifedev/google_vision","funding":["https://www.buymeacoffee.com/faithoflif2"]}');
Loading

0 comments on commit 9599ac4

Please sign in to comment.