Skip to content

Commit 36af58e

Browse files
committed
Edits1 in readme and BFR_Sample_Rest notebook
1 parent 000af61 commit 36af58e

File tree

5 files changed

+146
-48
lines changed

5 files changed

+146
-48
lines changed

Image-Processing/BFR_Sample_Rest.ipynb

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
"# Azure Cognitive Search sample \n",
88
"## Passing Images as Binary File References\n",
99
"\n",
10-
"Cognitive Search skillsets that need to pass images to custom skills use a binary file reference to serialize the images to pass them to and from skills. This sample demonstrates an example of how skills can be configured to accept an image as an input from the skillset and return images as outputs to the skillset. This example does nothing more than segment an image based on the layout from OCR. The sole purpose of this sample is to demonstrate how you pass images to skills and how skills can return images.\n",
10+
"Skillsets that pass images to custom skills use a binary file reference to serialize the images before passing them to other skills. This sample demonstrates how skills can be configured to accept image inputs and return image outputs. \n",
11+
"\n",
12+
"While the other steps in this skillset, such as OCR and redaction, have relevance, the key takeaway is configuring and passing binary file references. The custom skill does the heavy lifting. Each input record contains an image that is serialized as a `Base64` encoded string. The input also contains the layout text of image, as returned from the OCR skill. Upon receiving the input, the custom skill segments the image into smaller images based on the coordinates of the layout text. It then returns a list of images, each `Base64` encoded, back to the skillset. While this is not a particularly realistic exercise, it demonstrates techniques that could be leverage in more interesting ways, such as in a [Custom Vision](https://github.com/Azure-Samples/azure-search-power-skills/tree/master/Vision/CustomVision) skill that performs useful inferences on your images.\n",
13+
"\n",
14+
"For more information about the skills used in this example, see [OCR skill](https://docs.microsoft.com/azure/search/cognitive-search-skill-ocr), [PII skill](https://docs.microsoft.com/azure/search/cognitive-search-skill-pii-detection), and [custom skills](https://docs.microsoft.com/azure/search/cognitive-search-custom-skill-web-api).\n",
1115
"\n"
1216
]
1317
},
@@ -17,34 +21,36 @@
1721
"source": [
1822
"### Prerequisites \n",
1923
"\n",
20-
"Provision the required services:\n",
21-
"1. [Azure Cognitive Search](https://docs.microsoft.com/azure/search/search-create-service-portal)\n",
22-
"2. [Azure Functions](https://docs.microsoft.com/azure/azure-functions/) used for hosting an API endpoint.\n",
23-
"3. [Storage Account](https://docs.microsoft.com/azure/storage/blobs/)\n"
24+
"+ [Azure subscription](https://Azure.Microsoft.com/subscription/free)\n",
25+
"+ [Azure Cognitive Search service](https://docs.microsoft.com/azure/search/search-create-service-portal) (get the full service endpoint and an admin API key)\n",
26+
"+ [Azure Blob storage service](https://docs.microsoft.com/azure/storage/common/storage-account-create) (get the connection string)\n",
27+
"+ [Azure Cognitive Services](https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account) (get the account name)\n",
28+
"+ [Python 3.6+](https://www.python.org/downloads/)\n",
29+
"+ [Jupyter Notebook](https://jupyter.org/install)\n",
30+
"+ [Visual Studio Code](https://code.visualstudio.com/download) with the [Azure Functions extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions) and the [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python)\n"
2431
]
2532
},
2633
{
2734
"cell_type": "markdown",
2835
"metadata": {},
2936
"source": [
30-
"### Deploy the Azure functions app \n",
31-
"The ```SplitImage``` folder contains an Azure function that will accept an input in the [custom skill format](https://docs.microsoft.com/azure/search/cognitive-search-custom-skill-web-api#skill-inputs). \n",
32-
"Each input record contains an image that is serialized as a ```Base64``` encoded string and the layout text returned from the OCR skill.\n",
33-
"The skill then segments the image into smaller images based on the coordinates of the layout text. It then returns a list of images, each ```Base64``` encoded back to the skillset. While this is not very useful, you could build a [Custom Vision](https://github.com/Azure-Samples/azure-search-power-skills/tree/master/Vision/CustomVision) skill to perform a useful inference on your images.\n",
37+
"### Configure inputs\n",
3438
"\n",
35-
"Follow the [Azure Functions tutorial](https://docs.microsoft.com/azure/developer/python/tutorial-vs-code-serverless-python-05) to deploy the function. Once the deployment completes, navigate to the function app in the portal, select the function (SplitImage) and click the Get Function Url button. Save the function url as we will use it in the next step."
39+
"Follow the instructions in the [readme](https://github.com/Azure-Samples/azure-search-python-samples/blob/master/Image-Processing/README.md) to set up the inputs used by the indexer, data source, and skillset.\n",
40+
"\n",
41+
"Besides connection information, you will need a blob container for the sample JPEG file, and a function app that provides the code used in the custom skill. All the necessary files are provided. The `SplitImage` folder contains an Azure function that will accept an input in the [custom skill format](https://docs.microsoft.com/azure/search/cognitive-search-custom-skill-web-api#skill-inputs). "
3642
]
3743
},
3844
{
3945
"cell_type": "markdown",
4046
"metadata": {},
4147
"source": [
4248
"### Create the enrichment pipeline\n",
43-
"In the next few steps we will configure the Cognitive Search enrichment pipeline with the following steps:\n",
44-
"1. Create a blob storage data source. Ensure you have a blob storage container with at least one file containing images.\n",
45-
"2. Create a skillset to enrich the documents in the data source\n",
46-
"3. Create an index\n",
47-
"4. Create an indexer to move documents from the data source to the index while invoking the skillset\n"
49+
"In the next few steps, configure the Cognitive Search enrichment pipeline, creating these objects on your search service:\n",
50+
"1. Create an indexer data source. The data source references a blob storage container with at least one image file.\n",
51+
"2. Create a skillset that performs image analysis. The skillset references a Cognitive Services account, a custom function app, and a knowledge store.\n",
52+
"3. Create a search index.\n",
53+
"4. Create an indexer to move documents from the data source to the index while invoking the skillset.\n"
4854
]
4955
},
5056
{
@@ -59,36 +65,40 @@
5965
"import json\n",
6066
"import requests\n",
6167
"\n",
62-
"# Configure all required variables for Cognitive Search. Replace each with the credentials from your accounts.\n",
68+
"# Configure all required variables for this exerences. Replace each with the credentials from your accounts.\n",
6369
"\n",
64-
"# Replace with Search Service name, API key, and endpoint from the Azure portal.\n",
65-
"search_service = \"\" # In the format \"https://searchservicename.search.windows.net\"\n",
66-
"api_key = 'your search service API key'\n",
70+
"# Replace with a full search service endpoint the format \"https://searchservicename.search.windows.net\"\n",
71+
"# Paste in an admin API key. Both values can be obtained from the Azure portal.\n",
72+
"search_service = \"https://<YOUR-SEARCH-SERVICE>.search.windows.net\"\n",
73+
"api_key = '<YOUR-ADMIN-API-KEY>'\n",
6774
"\n",
6875
"# Leave the API version and content_type as they are listed here.\n",
6976
"api_version = '2020-06-30'\n",
7077
"content_type = 'application/json'\n",
7178
"\n",
72-
"# Replace with a Cognitive Services all in one key.\n",
79+
"# Replace with a Cognitive Services account name and all-in-one key.\n",
7380
"cog_svcs_key = '' #Required only if processing more than 20 documents\n",
74-
"cog_svcs_acct = 'your cog services account name'\n",
81+
"cog_svcs_acct = '<YOUR-COGNITIVE-SERVICE-ACCOUNT-NAME>'\n",
7582
"\n",
76-
"#Connection string to the storage account. This will be used for the datasource, knowledge store and cache\n",
77-
"STORAGECONNSTRING = \"DefaultEndpointsProtocol=https;AccountName=<Storage Acct>;AccountKey=<KEY>;EndpointSuffix=core.windows.net\"\n",
78-
"# The container with your files containing images\n",
79-
"datasource_container = 'bfrsample' # Replace with the container containging your files\n",
80-
"# This sample assumes you will use the same storage account for the datasource, knowledge store and indexer cache. The knowledge store will contain the projected images\n",
83+
"# Your Azure Storage account will be used for the datasource, knowledge store and cache\n",
84+
"# Replace with a connection string to your Azure Storage account. \n",
85+
"STORAGECONNSTRING = \"DefaultEndpointsProtocol=https;AccountName=<YOUR-ACCOUNT>;AccountKey=<YOUR-ACCOUNT-KEY>;EndpointSuffix=core.windows.net\"\n",
86+
"# Replace with the blob container containing your image files\n",
87+
"datasource_container = 'bfr-sample' \n",
88+
"# Use the same storage account for knowledge store and indexer cache. The knowledge store will contain the projected images\n",
8189
"know_store_cache = STORAGECONNSTRING\n",
82-
"# Container where the sliced images will be projected to\n",
90+
"# Container where the sliced images will be projected to. Use the value provided below.\n",
8391
"know_store_container = \"obfuscated\"\n",
84-
"skill_uri = \"https://<skillname>.azurewebsites.net/api/SplitImage?code=CODE\""
92+
"\n",
93+
"# Replace with the Function HTTP URL of the app deployed to Azure Function\n",
94+
"skill_uri = \"<YOUR-FUNCTION-APP-URL>\""
8595
]
8696
},
8797
{
8898
"cell_type": "markdown",
8999
"metadata": {},
90100
"source": [
91-
"Create a helper function to invoke the Cognitive Search REST API"
101+
"Create a helper function to invoke the Cognitive Search REST APIs. "
92102
]
93103
},
94104
{
@@ -153,7 +163,9 @@
153163
"cell_type": "markdown",
154164
"metadata": {},
155165
"source": [
156-
"#### Create the skillset"
166+
"#### Create the skillset\n",
167+
"\n",
168+
"Besides skills, a skillset also specifies the knowledge store that will contain the final output."
157169
]
158170
},
159171
{
@@ -344,7 +356,9 @@
344356
"cell_type": "markdown",
345357
"metadata": {},
346358
"source": [
347-
"#### Create the index"
359+
"#### Create the index\n",
360+
"\n",
361+
"This exercise doesn't have steps for using the index, but having an index is an indexer requirement. You can use Search Explorer in the Azure portal to query the index on your own."
348362
]
349363
},
350364
{
@@ -515,7 +529,9 @@
515529
"cell_type": "markdown",
516530
"metadata": {},
517531
"source": [
518-
"#### Create the indexer"
532+
"#### Create the indexer\n",
533+
"\n",
534+
"The indexer connects to the data source, invokes the skillset, and outputs results. This indexer is scheduled to run every two hours. In the following step, you'll run the indexer to start the process immediately."
519535
]
520536
},
521537
{
@@ -597,7 +613,7 @@
597613
"metadata": {},
598614
"source": [
599615
"### View Results\n",
600-
"The following cell downloads the image so that you can verify skillset success."
616+
"The following cell downloads the output image so that you can verify skillset success."
601617
]
602618
},
603619
{
@@ -630,8 +646,17 @@
630646
"metadata": {},
631647
"source": [
632648
"### Next Steps\n",
633-
"You now know how to pass images into skills and even return images to the skillset. As a next step, you can start from scratch and build a [custom AML Skill](https://docs.microsoft.com/en-us/azure/search/cognitive-search-aml-skill) to perform inferences on images or use the Custom Vision service to build a skill. The power skills github repository has a [sample custom vision skill](https://github.com/Azure-Samples/azure-search-power-skills/tree/master/Vision/CustomVision) to help you get started."
649+
"You now know how to pass images into skills and return the modified images to the skillset for further processing. \n",
650+
"\n",
651+
"As a next step, you can start from scratch and build a [custom AML Skill](https://docs.microsoft.com/azure/search/cognitive-search-aml-skill) to perform inferences on images, or use the Custom Vision service to build a skill. The power skills github repository has a [sample custom vision skill](https://github.com/Azure-Samples/azure-search-power-skills/tree/master/Vision/CustomVision) to help you get started."
634652
]
653+
},
654+
{
655+
"cell_type": "code",
656+
"execution_count": null,
657+
"metadata": {},
658+
"outputs": [],
659+
"source": []
635660
}
636661
],
637662
"metadata": {
@@ -650,7 +675,7 @@
650675
"name": "python",
651676
"nbconvert_exporter": "python",
652677
"pygments_lexer": "ipython3",
653-
"version": "3.7.4"
678+
"version": "3.7.3"
654679
}
655680
},
656681
"nbformat": 4,

Image-Processing/README.md

Lines changed: 85 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,96 @@
1+
---
2+
page_type: sample
3+
languages:
4+
- python
5+
name: Image processing in Python
6+
products:
7+
- azure
8+
- azure-cognitive-search
9+
description: |
10+
Skillsets in Cognitive Search can process images, making that content usable in other scenarios. This sample demonstrates an image file workflow, using OCR and redaction of personal information.
11+
urlFragment: python-sample-image-processing
12+
---
13+
114
# Image Processing Sample
215

3-
Cognitive Search can enrich images with text or images with other images. This sample demonstrates how to pass images to a custom skill and return images from the custom skill back to the skillset.
16+
Cognitive Search can analyze images with text, or images with other images, to create searchable or analyzable text. This sample focuses on a specific aspect of image analysis in a Cognitive Search pipeline: passing images to a custom skill, and return images back to the skillset for further processing.
17+
18+
In this demonstration, you will use a sample JPEG file, services and tools, and fully formulated requests in notebook to perform the following tasks:
19+
20+
1. Crack a sample source JPG file from Blob storage and scrape the image for text, using the [Optical Character Recognition (OCR) skill](https://docs.microsoft.com/azure/search/cognitive-search-skill-ocr) in Cognitive Search.
21+
1. Analyze the resulting text for personal information, such as phone numbers, using the [PII skill](https://docs.microsoft.com/azure/search/cognitive-search-skill-pii-detection).
22+
1. Split the text into smaller units and blur the units that contain phone numbers. Use a [custom skill](https://docs.microsoft.com/azure/search/cognitive-search-custom-skill-web-api) for this task.
23+
1. Reconstitute the image in Blob storage. Use the [TextMerge skill](https://docs.microsoft.com/azure/search/cognitive-search-skill-textmerger) for this step.
24+
25+
Post-OCR, the skillset runs the extracted text through the PII detection skill to identify personal information (phone numbers). The custom skill then obfuscates the phone numbers by accepting as inputs the image, the layout text from OCR step, and the identified personal information. Output of the custom skill is the image with obfuscated sections. The output is then returned to the skillset and projected to the knowledge store.
26+
27+
The source input and resulting output are stored in Azure Blob storage, so you will need a storage account to complete this tutorial.
28+
29+
Predefined skills, such as OCR skill, are backed by Cognitive Services. You will need the Cognitive Services account name for this tutorial, but because the number of transformations is limited, there is no charge to your account.
30+
31+
Custom skills must be hosted as a URL-accessible module. This tutorial uses Azure Functions to satisfy this requirement, but you could use another mechanism for your own solutions.
32+
33+
## Prerequisites
34+
35+
+ [Azure subscription](https://Azure.Microsoft.com/subscription/free)
36+
+ [Azure Cognitive Search service](https://docs.microsoft.com/azure/search/search-create-service-portal) (get the full service endpoint and an admin API key)
37+
+ [Azure Blob storage service](https://docs.microsoft.com/azure/storage/common/storage-account-create) (get the connection string)
38+
+ [Azure Cognitive Services](https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account) (get the account name)
39+
+ [Python 3.6+](https://www.python.org/downloads/)
40+
+ [Jupyter Notebook](https://jupyter.org/install)
41+
+ [Visual Studio Code](https://code.visualstudio.com/download) with the [Azure Functions extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions) and the [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python)
42+
43+
## Configure the components
44+
45+
Before you open the notebook, assemble the resources that are referenced by the skillset.
46+
47+
1. Download the **azure-search-python-samples** repository and extract its contents.
48+
49+
1. Open the **image-processing** sample folder to find the files used in this sample.
450

5-
## Redacting PII information from images
51+
### In Azure portal
652

7-
This sample deploys a skill to obfuscate or redact phone numbers from images. The skillset contains three skills:
8-
1. OCR
9-
2. PII detection
10-
3. Custom Skill to redact PII
53+
1. Set up the data source. In Azure portal, in Azure Blob storage, create a container named "brf-sample", and then upload the sample JPEG file (microsoft.jpg) from the sample folder.
1154

12-
The skillset OCR's the images and runs the extracted text through the PII detection skill to identify PII information. The custom skill then takes the image, layout text from OCR and the identified PII information to obfuscate the image. The image with the PII infomration obfuscted is then returned to the skillset and projected to the knwoledge store.
55+
1. From **Keys**, copy the Azure Storage connection string and paste it into NotePad.
1356

14-
## Confingure the components
57+
1. Navigate to your search service, copy the search endpoint (http://<SERVICE-NAME>.search.windows.net) and an admin API key.
1558

16-
This sample contains a Azure function and a Jupyter Python3 .ipynb file. Start by deploying the Azure function and saving the URL and code.
59+
1. Navigate to Cognitive Services, copy the account name.
1760

18-
The folder also contains a sample image with a phone number. Save this image to a container in a storage account. This container will be your data source for the enrichment pipeline.
61+
### In Visual Studio Code
1962

20-
Open the norebook in this folder and set the URL and other required variables in the first cell of the notebook, execute the cells of the notebook to configure and run the solution.
63+
1. Set up the function app that contains the custom code. In Visual Studio code, navigate to the **image-processing** sample folder.
64+
65+
1. Right-click the **SplitImage** folder and select **Deploy to Function App**. You will be prompted for a subscription, region, and other properties that are required to set up the app.
66+
67+
![Deploy as function](media/image-process-split-image-deploy-function-app.png)
68+
69+
1. Monitor notifications in the Output window (**View** > **Output**) for a "Deployment successful" message.
70+
71+
1. Still in Visual Studio Code, switch to **Azure: Function** explorer.
72+
73+
1. Open the subscription folder and find the app you just deployed.
74+
75+
1. Open the Functions folder, right-click **ImageSkill HTTP**, and then copy the function URL. Save it Notepad.
76+
77+
![Copy function URL](media/image-process-function-url.png)
78+
79+
## Run the Python code in the notebook
80+
81+
1. Open the BFR_Sample_Rest.ipynb file in Jupyter Notebook.
82+
83+
1. In the first cell, paste in the following information:
84+
85+
* search service endpoint
86+
* search service admin API key
87+
* Azure Storage connection string
88+
* Cognitive Services account name
89+
* Blob container that contains the JPEG
90+
* Function app URL
91+
92+
1. Run each cell.
2193

2294
## Validation
23-
Once the indexer completes, you will see a container `obfuscated` in the knowledge store with the phone number redacted. For comparision the original images are stored in a container `images`.
95+
96+
Once the indexer completes, use the Azure portal and Storage explorer in your Azure Storage account to find the final output. You will see a container `obfuscated` in the knowledge store that contains the same image you started with, but with the phone number redacted. For comparison, the original image is stored in a container `images`.
49.9 KB
Loading
61.7 KB
Loading

0 commit comments

Comments
 (0)