forked from google-ai-edge/mediapipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request google-ai-edge#20 from googlesamples/feature/pytho…
…n_samples_for_preview Added Colab notebooks for Python demos.
- Loading branch information
Showing
5 changed files
with
1,591 additions
and
0 deletions.
There are no files selected for viewing
446 changes: 446 additions & 0 deletions
446
examples/gesture_recognizer/python/gesture_recognizer.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
385 changes: 385 additions & 0 deletions
385
examples/image_classification/python/image_classification.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
309 changes: 309 additions & 0 deletions
309
examples/object_detection/python/object_detection.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
171 changes: 171 additions & 0 deletions
171
examples/text_classification/python/text_classifier.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "h2q27gKz1H20" | ||
}, | ||
"source": [ | ||
"##### Copyright 2022 The MediaPipe Authors. All Rights Reserved." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"cellView": "form", | ||
"id": "TUfAcER1oUS6" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", | ||
"# you may not use this file except in compliance with the License.\n", | ||
"# You may obtain a copy of the License at\n", | ||
"#\n", | ||
"# https://www.apache.org/licenses/LICENSE-2.0\n", | ||
"#\n", | ||
"# Unless required by applicable law or agreed to in writing, software\n", | ||
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n", | ||
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", | ||
"# See the License for the specific language governing permissions and\n", | ||
"# limitations under the License." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "L_cQX8dWu4Dv" | ||
}, | ||
"source": [ | ||
"# Text Classifier with MediaPipe Tasks\n", | ||
"\n", | ||
"This notebook shows you how to use MediaPipe Tasks Python API to classify text. Check out the [MediaPipe documentation](https://developers.google.com/mediapipe/solutions/text/text_classifier/python) to learn more about configuration options that this solution supports." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "99IjoWCyDk7g" | ||
}, | ||
"source": [ | ||
"## Preparation\n", | ||
"\n", | ||
"Let's start with installing MediaPipe." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": { | ||
"id": "gxbHBsF-8Y_l" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"!pip install -q flatbuffers==2.0.0\n", | ||
"!pip install -q mediapipe==0.9.0" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "QGNTJpASRDpI" | ||
}, | ||
"source": [ | ||
"Then download an off-the-shelf model. Check out the [MediaPipe documentation](https://developers.google.com/mediapipe/solutions/text/text_classifier#models) for more text classification models that you can use." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": { | ||
"id": "OMjuVQiDYJKF" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"!wget -O classifier.tflite -q https://storage.googleapis.com/mediapipe-tasks/text_classifier/bert_text_classifier.tflite " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "Iy4r2_ePylIa" | ||
}, | ||
"source": [ | ||
"## Running inference\n", | ||
"\n", | ||
"Here are the steps to run text classification using MediaPipe:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "Yl_Oiye4mUuo", | ||
"outputId": "994627d4-56cd-40af-9f5d-abef38fa4592" | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"positive (0.99)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# STEP 1: Import the necessary modules.\n", | ||
"from mediapipe.tasks import python\n", | ||
"from mediapipe.tasks.python import text\n", | ||
"\n", | ||
"# STEP 2: Create an TextClassifier object.\n", | ||
"base_options = python.BaseOptions(model_asset_path=\"classifier.tflite\")\n", | ||
"options = text.TextClassifierOptions(base_options=base_options)\n", | ||
"classifier = python.text.TextClassifier.create_from_options(options)\n", | ||
"\n", | ||
"# STEP 3: Classify the input text.\n", | ||
"INPUT_TEXT = \"I'm looking forward to what will come next.\"\n", | ||
"classification_result = classifier.classify(INPUT_TEXT)\n", | ||
"\n", | ||
"# STEP 4: Process the classification result. In this case, print out the most likely category.\n", | ||
"top_category = classification_result.classifications[0].categories[0]\n", | ||
"print(f'{top_category.category_name} ({top_category.score:.2f})')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "WPO6rvNJTkPd" | ||
}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"colab": { | ||
"provenance": [] | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |