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

Add a demo notebook #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ To build and deploy the model to a REST API using Docker, follow these steps:
1. [Build the Model](#1-build-the-model)
2. [Deploy the Model](#2-deploy-the-model)
3. [Use the Model](#3-use-the-model)
4. [Development](#4-development)
5. [Cleanup](#5-cleanup)
4. [Run the Notebook](#4-run-the-notebook)
5. [Development](#5-development)
6. [Cleanup](#6-cleanup)


### 1. Build the Model
Expand Down Expand Up @@ -170,12 +171,23 @@ You should see a JSON response like that below:
]
}
```
### 4. Run the Notebook

### 4. Development
[The demo notebook](demo.ipynb) walks through how to use the model to recognize text in an image and print the results. By default, the notebook uses the [hosted demo instance](http://max-ocr.codait-prod-41208c73af8fca213512856c7a09db52-0000.us-east.containers.appdomain.cloud/), but you can use a locally running instance (see the comments in Cell 3 for details). _Note_ the demo requires `jupyter`, `matplotlib`, `Pillow`, and `requests`.

Run the following command from the model repo base folder, in a new terminal window:

```bash
$ jupyter notebook
```

This will start the notebook server. You can launch the demo notebook by clicking on `demo.ipynb`.

### 5. Development

To run the Flask API app in debug mode, edit `config.py` to set `DEBUG = True` under the application settings. You will
then need to rebuild the Docker image (see [step 1](#1-build-the-model)).

### 5. Cleanup
### 6. Cleanup

To stop the Docker container, type `CTRL` + `C` in your terminal.
183 changes: 183 additions & 0 deletions demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#\n",
"# Copyright 2018-2019 IBM Corp. All Rights Reserved.\n",
"#\n",
"# 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",
"# http://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.\n",
"#"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# OCR - Visualize Results\n",
"\n",
"This example illustrates the usage of [MAX OCR](https://developer.ibm.com/exchanges/models/all/max-ocr) model. This notebook guides you through running the model on a sample image to get the text.\n",
"\n",
"\n",
"## Setup\n",
"\n",
"The notebook calls the `MAX OCR` microservice, which must be running. You can either use the [hosted demo instance](http://max-ocr.codait-prod-41208c73af8fca213512856c7a09db52-0000.us-east.containers.appdomain.cloud), or follow the instructions for [deploying the microservice locally from the Dockerhub image](https://github.com/IBM/MAX-OCR#2-deploy-the-model). "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This notebook requires matplotlib, Pillow and requests\n",
"# You only need to run the line below to install these if you don't already have them installed\n",
"\n",
"! pip install -q matplotlib Pillow requests"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import io\n",
"from PIL import Image\n",
"import matplotlib\n",
"from matplotlib import pyplot as plt\n",
"import matplotlib.patches as patches\n",
"import requests"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This url must point to a running instance of the model microservice\n",
"# By default this assumes you are using the hosted demo instance\n",
"# If you want to use the model that is running locally, pass the `local_port` field.\n",
"\n",
"\n",
"def call_model(input_img, local_port=None):\n",
" \"\"\"\n",
" Takes in input image file path, posts the image to the model and returns face bboxes and emotion predictions\n",
" If local port is not specified, uses the long running instance.\n",
" If local port is specified, uses the local instance.\n",
" \"\"\"\n",
" if local_port:\n",
" url = 'http://localhost:'+ str(local_port)+'/model/predict'\n",
" else:\n",
" url = 'http://max-ocr.codait-prod-41208c73af8fca213512856c7a09db52-0000.us-east.containers.appdomain.cloud/model/predict'\n",
"\n",
" files = {'image': ('image.jpg', open(input_img, 'rb'), 'images/jpeg') }\n",
" r = requests.post(url, files=files).json()\n",
" \n",
" return r"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Visualizing the test image\n",
"\n",
"First we load the image with Pillow and display the image in our notebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"img_path = 'samples/chap4_summary.jpg'\n",
"image = Image.open(img_path)\n",
"image"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Call model to detect objects in the image"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model_response = call_model(img_path)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Visualize model response\n",
"\n",
"The model returns JSON containing a `text` field which is an array of strings, one for each object detected in the image."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Explore the model results - there should be a `text` array\n",
"import json\n",
"print(json.dumps(model_response, indent=2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if model_response['status']=='ok':\n",
" for textgroup in model_response['text']:\n",
" print(' '.join(textgroup))"
]
}
],
"metadata": {
"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.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}