Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal changes only #69

Merged
merged 1 commit into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 71 additions & 0 deletions tensorflow_lite_support/examples/task/vision/desktop/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package(
default_visibility = [
"//tensorflow_lite_support:users",
],
licenses = ["notice"], # Apache 2.0
)

cc_binary(
name = "image_classifier_demo",
srcs = ["image_classifier_demo.cc"],
deps = [
"//tensorflow_lite_support/cc/port:statusor",
"//tensorflow_lite_support/cc/task/core:external_file_handler",
"//tensorflow_lite_support/cc/task/core/proto:external_file_proto_inc",
"//tensorflow_lite_support/cc/task/vision:image_classifier",
"//tensorflow_lite_support/cc/task/vision/proto:class_proto_inc",
"//tensorflow_lite_support/cc/task/vision/proto:classifications_proto_inc",
"//tensorflow_lite_support/cc/task/vision/proto:image_classifier_options_proto_inc",
"//tensorflow_lite_support/cc/task/vision/utils:frame_buffer_common_utils",
"//tensorflow_lite_support/examples/task/vision/desktop/utils:image_utils",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/flags:parse",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings:str_format",
"@org_tensorflow//tensorflow/core:lib",
],
)

cc_binary(
name = "object_detector_demo",
srcs = ["object_detector_demo.cc"],
deps = [
"//tensorflow_lite_support/cc/port:statusor",
"//tensorflow_lite_support/cc/task/core:external_file_handler",
"//tensorflow_lite_support/cc/task/core/proto:external_file_proto_inc",
"//tensorflow_lite_support/cc/task/vision:object_detector",
"//tensorflow_lite_support/cc/task/vision/proto:bounding_box_proto_inc",
"//tensorflow_lite_support/cc/task/vision/proto:class_proto_inc",
"//tensorflow_lite_support/cc/task/vision/proto:detections_proto_inc",
"//tensorflow_lite_support/cc/task/vision/proto:object_detector_options_proto_inc",
"//tensorflow_lite_support/cc/task/vision/utils:frame_buffer_common_utils",
"//tensorflow_lite_support/examples/task/vision/desktop/utils:image_utils",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/flags:parse",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@org_tensorflow//tensorflow/core:lib",
],
)

cc_binary(
name = "image_segmenter_demo",
srcs = ["image_segmenter_demo.cc"],
deps = [
"//tensorflow_lite_support/cc/port:statusor",
"//tensorflow_lite_support/cc/task/core:external_file_handler",
"//tensorflow_lite_support/cc/task/core/proto:external_file_proto_inc",
"//tensorflow_lite_support/cc/task/vision:image_segmenter",
"//tensorflow_lite_support/cc/task/vision/proto:image_segmenter_options_proto_inc",
"//tensorflow_lite_support/cc/task/vision/proto:segmentations_proto_inc",
"//tensorflow_lite_support/cc/task/vision/utils:frame_buffer_common_utils",
"//tensorflow_lite_support/examples/task/vision/desktop/utils:image_utils",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/flags:parse",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@org_tensorflow//tensorflow/core:lib",
],
)
181 changes: 181 additions & 0 deletions tensorflow_lite_support/examples/task/vision/desktop/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# CLI Demos for C++ Vision Task APIs

This folder contains simple command-line tools for easily trying out the C++
Vision Task APIs.

## Image Classifier

#### Prerequisites

You will need:

* a TFLite image classification model (e.g. [aiy/vision/classifier/birds_V1]
(https://tfhub.dev/google/lite-model/aiy/vision/classifier/birds_V1/2),
a bird classification model available on TensorFlow Hub),
* a PNG, JPEG or GIF image to run classification on, e.g.:

![sparrow](g3doc/sparrow.jpg)

#### Usage

In the console, run:

```bash
# Download the model:
curl \
-L https://tfhub.dev/google/lite-model/aiy/vision/classifier/birds_V1/2?lite-format=tflite \
-o /tmp/aiy_vision_classifier_birds_V1_2.tflite

# Run the classification tool:
bazel run -c opt examples/task/vision/desktop:image_classifier_demo -- \
--model_path=/tmp/aiy_vision_classifier_birds_V1_2.tflite \
--image_path=$(pwd)/examples/task/vision/desktop/g3doc/sparrow.jpg \
--max_results=3
```

#### Results

In the console, you should get:

```
Results:
Rank #0:
index : 671
score : 0.91797
class name : /m/01bwbt
display name: Passer montanus
Rank #1:
index : 670
score : 0.00391
class name : /m/0193xp
display name: Troglodytes hiemalis
Rank #2:
index : 495
score : 0.00391
class name : /m/05sjn7
display name: Mimus gilvus
```

## Object Detector

#### Prerequisites

TODO(b/161960089): the model used in this example has an off-by-one error in its
label map, which will cause the model to return "cat" instead of "dog" in the
following example. It will soon be updated on tfhub.dev with a fix.

You will need:

* a TFLite object detection model (e.g. [ssd_mobilenet_v1]
(https://tfhub.dev/tensorflow/lite-model/ssd_mobilenet_v1/1/metadata/1),
a generic object detection model available on TensorFlow Hub),
* a PNG, JPEG or GIF image to run detection on, e.g.:

![dogs](g3doc/dogs.jpg)

#### Usage

In the console, run:

```bash
# Download the model:
curl \
-L https://tfhub.dev/tensorflow/lite-model/ssd_mobilenet_v1/1/metadata/1?lite-format=tflite \
-o /tmp/ssd_mobilenet_v1_1_metadata_1.tflite

# Run the detection tool:
bazel run -c opt examples/task/vision/desktop:object_detector_demo -- \
--model_path=/tmp/ssd_mobilenet_v1_1_metadata_1.tflite \
--image_path=$(pwd)/examples/task/vision/desktop/g3doc/dogs.jpg \
--output_png=/tmp/detection-output.png \
--max_results=2
```

#### Results

In the console, you should get:

```
Results saved to: /tmp/detection-output.png
Results:
Detection #0 (red):
Box: (x: 355, y: 133, w: 190, h: 206)
Top-1 class:
index : 17
score : 0.73828
class name : dog
Detection #1 (green):
Box: (x: 103, y: 15, w: 138, h: 369)
Top-1 class:
index : 17
score : 0.73047
class name : dog
```

And `/tmp/detection-output.jpg` should contain:

![detection-output](g3doc/detection-output.png)

## Image Segmenter

#### Prerequisites

TODO(b/161957922): the model used in this example doesn't include a label map,
which will cause the console output to be less complete than in the example
below. It will soon be updated on tfhub.dev with a fix.

You will need:

* a TFLite image segmentation model (e.g. [deeplab_v3]
(https://tfhub.dev/tensorflow/lite-model/deeplabv3/1/metadata/1),
a generic segmentation model available on TensorFlow Hub),
* a PNG, JPEG or GIF image to run segmentation on, e.g.:

![plane](g3doc/plane.jpg)

#### Usage

In the console, run:

```bash
# Download the model:
curl \
-L https://tfhub.dev/tensorflow/lite-model/deeplabv3/1/metadata/1?lite-format=tflite \
-o /tmp/deeplabv3_1_metadata_1.tflite

# Run the segmentation tool:
bazel run -c opt examples/task/vision/desktop:image_segmenter_demo -- \
--model_path=/tmp/deeplabv3_1_metadata_1.tflite \
--image_path=$(pwd)/examples/task/vision/desktop/g3doc/plane.jpg \
--output_mask_png=/tmp/segmentation-output.png
```

#### Results

In the console, you should get:

```
Category mask saved to: /tmp/segmentation-output.png
Color Legend:
(r: 000, g: 000, b: 000):
index : 0
class name : background
(r: 128, g: 000, b: 000):
index : 1
class name : aeroplane

# (omitting multiple lines for conciseness) ...

(r: 128, g: 192, b: 000):
index : 19
class name : train
(r: 000, g: 064, b: 128):
index : 20
class name : tv
Tip: use a color picker on the output PNG file to inspect the output mask with
this legend.
```

And `/tmp/segmentation-output.jpg` should contain the segmentation mask:

![segmentation-output](g3doc/segmentation-output.png)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.