Skip to content

Commit 17cc3b2

Browse files
authored
Merge pull request google-ai-edge#25 from googlesamples/feature/python_samples_for_preview
Updated Python API's sample notebooks
2 parents e56922c + b804e17 commit 17cc3b2

File tree

7 files changed

+5
-1591
lines changed

7 files changed

+5
-1591
lines changed

Diff for: examples/gesture_recognizer/python/gesture_recognizer.ipynb

+1-446
Large diffs are not rendered by default.

Diff for: examples/hand_landmarker/python/hand_landmark.ipynb renamed to examples/hand_landmarker/python/hand_landmarker.ipynb

+1-280
Large diffs are not rendered by default.

Diff for: examples/image_classification/python/image_classification.ipynb

-385
This file was deleted.

Diff for: examples/image_classification/python/image_classifier.ipynb

+1
Large diffs are not rendered by default.

Diff for: examples/object_detection/python/object_detection.ipynb

-309
This file was deleted.

Diff for: examples/object_detection/python/object_detector.ipynb

+1
Large diffs are not rendered by default.
+1-171
Original file line numberDiff line numberDiff line change
@@ -1,171 +1 @@
1-
{
2-
"cells": [
3-
{
4-
"cell_type": "markdown",
5-
"metadata": {
6-
"id": "h2q27gKz1H20"
7-
},
8-
"source": [
9-
"##### Copyright 2022 The MediaPipe Authors. All Rights Reserved."
10-
]
11-
},
12-
{
13-
"cell_type": "code",
14-
"execution_count": null,
15-
"metadata": {
16-
"cellView": "form",
17-
"id": "TUfAcER1oUS6"
18-
},
19-
"outputs": [],
20-
"source": [
21-
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
22-
"# you may not use this file except in compliance with the License.\n",
23-
"# You may obtain a copy of the License at\n",
24-
"#\n",
25-
"# https://www.apache.org/licenses/LICENSE-2.0\n",
26-
"#\n",
27-
"# Unless required by applicable law or agreed to in writing, software\n",
28-
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
29-
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
30-
"# See the License for the specific language governing permissions and\n",
31-
"# limitations under the License."
32-
]
33-
},
34-
{
35-
"cell_type": "markdown",
36-
"metadata": {
37-
"id": "L_cQX8dWu4Dv"
38-
},
39-
"source": [
40-
"# Text Classifier with MediaPipe Tasks\n",
41-
"\n",
42-
"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."
43-
]
44-
},
45-
{
46-
"cell_type": "markdown",
47-
"metadata": {
48-
"id": "99IjoWCyDk7g"
49-
},
50-
"source": [
51-
"## Preparation\n",
52-
"\n",
53-
"Let's start with installing MediaPipe."
54-
]
55-
},
56-
{
57-
"cell_type": "code",
58-
"execution_count": 3,
59-
"metadata": {
60-
"id": "gxbHBsF-8Y_l"
61-
},
62-
"outputs": [],
63-
"source": [
64-
"!pip install -q flatbuffers==2.0.0\n",
65-
"!pip install -q mediapipe==0.9.0"
66-
]
67-
},
68-
{
69-
"cell_type": "markdown",
70-
"metadata": {
71-
"id": "QGNTJpASRDpI"
72-
},
73-
"source": [
74-
"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."
75-
]
76-
},
77-
{
78-
"cell_type": "code",
79-
"execution_count": 2,
80-
"metadata": {
81-
"id": "OMjuVQiDYJKF"
82-
},
83-
"outputs": [],
84-
"source": [
85-
"!wget -O classifier.tflite -q https://storage.googleapis.com/mediapipe-tasks/text_classifier/bert_text_classifier.tflite "
86-
]
87-
},
88-
{
89-
"cell_type": "markdown",
90-
"metadata": {
91-
"id": "Iy4r2_ePylIa"
92-
},
93-
"source": [
94-
"## Running inference\n",
95-
"\n",
96-
"Here are the steps to run text classification using MediaPipe:"
97-
]
98-
},
99-
{
100-
"cell_type": "code",
101-
"execution_count": 9,
102-
"metadata": {
103-
"colab": {
104-
"base_uri": "https://localhost:8080/"
105-
},
106-
"id": "Yl_Oiye4mUuo",
107-
"outputId": "994627d4-56cd-40af-9f5d-abef38fa4592"
108-
},
109-
"outputs": [
110-
{
111-
"name": "stdout",
112-
"output_type": "stream",
113-
"text": [
114-
"positive (0.99)\n"
115-
]
116-
}
117-
],
118-
"source": [
119-
"# STEP 1: Import the necessary modules.\n",
120-
"from mediapipe.tasks import python\n",
121-
"from mediapipe.tasks.python import text\n",
122-
"\n",
123-
"# STEP 2: Create an TextClassifier object.\n",
124-
"base_options = python.BaseOptions(model_asset_path=\"classifier.tflite\")\n",
125-
"options = text.TextClassifierOptions(base_options=base_options)\n",
126-
"classifier = python.text.TextClassifier.create_from_options(options)\n",
127-
"\n",
128-
"# STEP 3: Classify the input text.\n",
129-
"INPUT_TEXT = \"I'm looking forward to what will come next.\"\n",
130-
"classification_result = classifier.classify(INPUT_TEXT)\n",
131-
"\n",
132-
"# STEP 4: Process the classification result. In this case, print out the most likely category.\n",
133-
"top_category = classification_result.classifications[0].categories[0]\n",
134-
"print(f'{top_category.category_name} ({top_category.score:.2f})')"
135-
]
136-
},
137-
{
138-
"cell_type": "code",
139-
"execution_count": null,
140-
"metadata": {
141-
"id": "WPO6rvNJTkPd"
142-
},
143-
"outputs": [],
144-
"source": []
145-
}
146-
],
147-
"metadata": {
148-
"colab": {
149-
"provenance": []
150-
},
151-
"kernelspec": {
152-
"display_name": "Python 3 (ipykernel)",
153-
"language": "python",
154-
"name": "python3"
155-
},
156-
"language_info": {
157-
"codemirror_mode": {
158-
"name": "ipython",
159-
"version": 3
160-
},
161-
"file_extension": ".py",
162-
"mimetype": "text/x-python",
163-
"name": "python",
164-
"nbconvert_exporter": "python",
165-
"pygments_lexer": "ipython3",
166-
"version": "3.7.13"
167-
}
168-
},
169-
"nbformat": 4,
170-
"nbformat_minor": 0
171-
}
1+
{"cells":[{"cell_type":"markdown","metadata":{"id":"h2q27gKz1H20"},"source":["##### Copyright 2022 The MediaPipe Authors. All Rights Reserved."]},{"cell_type":"code","execution_count":1,"metadata":{"cellView":"form","id":"TUfAcER1oUS6","executionInfo":{"status":"ok","timestamp":1670014043908,"user_tz":480,"elapsed":5,"user":{"displayName":"Khanh LeViet","userId":"02074344541050284452"}}},"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."]},{"cell_type":"markdown","metadata":{"id":"99IjoWCyDk7g"},"source":["## Preparation\n","\n","Let's start with installing MediaPipe.\n","\n","*Notes:*\n","* *If you see an error about `flatbuffers` incompatibility, it's fine to ignore it. MediaPipe requires a newer version of flatbuffers (v2), which is incompatible with the older version of Tensorflow (v2.9) currently preinstalled on Colab.*\n","* *If you install MediaPipe outside of Colab, you only need to run `pip install mediapipe`. It isn't necessary to explicitly install `flatbuffers`.*"]},{"cell_type":"code","execution_count":2,"metadata":{"id":"gxbHBsF-8Y_l","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1670014059359,"user_tz":480,"elapsed":15455,"user":{"displayName":"Khanh LeViet","userId":"02074344541050284452"}},"outputId":"f465eff2-324a-4183-c741-0cbeec839d10"},"outputs":[{"output_type":"stream","name":"stdout","text":["\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n","tensorflow 2.9.2 requires flatbuffers<2,>=1.12, but you have flatbuffers 2.0 which is incompatible.\u001b[0m\n","\u001b[K |████████████████████████████████| 33.0 MB 1.4 MB/s \n","\u001b[?25h"]}],"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":3,"metadata":{"id":"OMjuVQiDYJKF","executionInfo":{"status":"ok","timestamp":1670014060065,"user_tz":480,"elapsed":710,"user":{"displayName":"Khanh LeViet","userId":"02074344541050284452"}}},"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.\n","\n","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":"code","source":["# Define the input text that you wants the model to classify.\n","INPUT_TEXT = \"I'm looking forward to what will come next.\""],"metadata":{"id":"VwROOdg9l1KM","executionInfo":{"status":"ok","timestamp":1670014060066,"user_tz":480,"elapsed":4,"user":{"displayName":"Khanh LeViet","userId":"02074344541050284452"}}},"execution_count":4,"outputs":[]},{"cell_type":"code","execution_count":5,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Yl_Oiye4mUuo","outputId":"27efcd55-7c17-493e-d591-2fe0c838e4df","executionInfo":{"status":"ok","timestamp":1670014061338,"user_tz":480,"elapsed":1275,"user":{"displayName":"Khanh LeViet","userId":"02074344541050284452"}}},"outputs":[{"output_type":"stream","name":"stdout","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","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":5,"metadata":{"id":"WPO6rvNJTkPd","executionInfo":{"status":"ok","timestamp":1670014061338,"user_tz":480,"elapsed":3,"user":{"displayName":"Khanh LeViet","userId":"02074344541050284452"}}},"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 commit comments

Comments
 (0)