From 3e69e204d9d98e3c1d7250bbd0444d2f378d89e7 Mon Sep 17 00:00:00 2001 From: Alexei Karve Date: Tue, 3 Aug 2021 11:57:28 -0400 Subject: [PATCH 1/2] Add demo notebook --- demo.ipynb | 183 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 demo.ipynb diff --git a/demo.ipynb b/demo.ipynb new file mode 100644 index 0000000..f0daad1 --- /dev/null +++ b/demo.ipynb @@ -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 +} From 9fc5543b2b5884ba2a0fa9ec75044b9318d840a6 Mon Sep 17 00:00:00 2001 From: Alexei Karve Date: Tue, 3 Aug 2021 12:13:14 -0400 Subject: [PATCH 2/2] Add demo notebook --- README.md | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b408ef9..b884600 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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.