Skip to content

Commit

Permalink
Merge pull request google-ai-edge#20 from googlesamples/feature/pytho…
Browse files Browse the repository at this point in the history
…n_samples_for_preview

Added Colab notebooks for Python demos.
  • Loading branch information
PaulTR authored Dec 1, 2022
2 parents 2574afe + 7e898ee commit 61411fd
Show file tree
Hide file tree
Showing 5 changed files with 1,591 additions and 0 deletions.
446 changes: 446 additions & 0 deletions examples/gesture_recognizer/python/gesture_recognizer.ipynb

Large diffs are not rendered by default.

280 changes: 280 additions & 0 deletions examples/hand_landmarker/python/hand_landmark.ipynb

Large diffs are not rendered by default.

385 changes: 385 additions & 0 deletions examples/image_classification/python/image_classification.ipynb

Large diffs are not rendered by default.

309 changes: 309 additions & 0 deletions examples/object_detection/python/object_detection.ipynb

Large diffs are not rendered by default.

171 changes: 171 additions & 0 deletions examples/text_classification/python/text_classifier.ipynb
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
}

0 comments on commit 61411fd

Please sign in to comment.