{"id":4065,"date":"2023-11-04T23:14:02","date_gmt":"2023-11-04T23:14:02","guid":{"rendered":"http:\/\/localhost:10003\/building-an-image-recognition-solution-with-azure-cognitive-services\/"},"modified":"2023-11-05T05:48:02","modified_gmt":"2023-11-05T05:48:02","slug":"building-an-image-recognition-solution-with-azure-cognitive-services","status":"publish","type":"post","link":"http:\/\/localhost:10003\/building-an-image-recognition-solution-with-azure-cognitive-services\/","title":{"rendered":"Building an image recognition solution with Azure Cognitive Services"},"content":{"rendered":"
In recent years, the field of computer vision has seen significant advancements, and image recognition has become an essential tool for various industries. With the help of deep learning techniques, we can train powerful neural networks that can recognize objects, faces, and other patterns from images. Azure Cognitive Services offers pre-built APIs that simplify the process of building a custom image recognition solution. In this tutorial, we will learn how to use Azure Cognitive Services to build an image recognition solution that can detect and classify objects in a set of images.<\/p>\n
To follow along with this tutorial, you should have a few things already set up:<\/p>\n
Azure Cognitive Services is a suite of pre-built APIs and SDKs that enable developers to integrate intelligent features into their applications. These APIs can be used to recognize faces, analyze text, and extract insights from images.<\/p>\n
To set up Azure Cognitive Services, follow these steps:<\/p>\n
Azure Storage is a cloud-based storage solution that allows you to store and manage data in the cloud. We will be using Azure Storage to store the images that we will use to train our model.<\/p>\n
To set up Azure Storage, follow these steps:<\/p>\n
Now that we have set up Azure Storage, we can upload the images that we will use to train our model. For this tutorial, we will be using the “Pascal VOC” dataset that contains images of objects from 20 different categories. You can download the dataset from http:\/\/host.robots.ox.ac.uk\/pascal\/VOC\/voc2012\/.<\/p>\n
To upload the images to Azure Storage, follow these steps:<\/p>\n
pip install azure-storage-blob\n<\/code><\/pre>\n\n- Import the necessary libraries in your Python file:<\/li>\n<\/ol>\n
import os, uuid\nfrom azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient\n<\/code><\/pre>\n\n- Set up the BlobServiceClient to access your Azure Storage account by replacing the connection string placeholders in the code below:<\/li>\n<\/ol>\n
connect_str = \"DefaultEndpointsProtocol=https;AccountName=[Name];AccountKey=[Key];EndpointSuffix=core.windows.net\"\nblob_service_client = BlobServiceClient.from_connection_string(connect_str)\n<\/code><\/pre>\n\n- Create a ContainerClient object by replacing the container name placeholder in the code below:<\/li>\n<\/ol>\n
container_client = blob_service_client.get_container_client(\"[Container name]\")\n<\/code><\/pre>\n\n- Upload the images to your Azure Storage account by running the following code:<\/li>\n<\/ol>\n
path_to_images = \"path\/to\/images\/folder\"\nfor filename in os.listdir(path_to_images):\n with open(os.path.join(path_to_images, filename), \"rb\") as data:\n container_client.upload_blob(name=filename, data=data)\n<\/code><\/pre>\nTrain the Custom Vision Model<\/h2>\n
Azure Cognitive Services offers a Custom Vision API that allows you to train your own custom image recognition model. To train the model, we will use the images that we uploaded to Azure Storage in the previous step.<\/p>\n
To train the Custom Vision Model, follow these steps:<\/p>\n
\n- Go to the Custom Vision website (https:\/\/www.customvision.ai\/) and sign in with your Azure account credentials.<\/li>\n
- Click on “Create new project” and give it a name and description.<\/li>\n
- Choose “Object Detection (preview)” as the project type and “General (compact)” as the domain.<\/li>\n
- Set up the project by selecting your Azure subscription, resource group, and location. Choose an existing storage account or create a new one if you haven’t already.<\/li>\n
- You will now be taken to the “Images” tab. Click on “Add images” and select the images that you uploaded to Azure Storage.<\/li>\n
- After the images have been uploaded, you can start tagging them by drawing bounding boxes around the objects in the images and assigning them to the appropriate categories.<\/li>\n
- Once you have tagged the images, click on the “Train” button to start training the model.<\/li>\n
- The Custom Vision API will now start training the model using the tagged images. This may take several minutes depending on the number of images and the complexity of the model.<\/li>\n
- Once the model has finished training, you can test it by clicking on the “Quick Test” button and uploading an image to see if the model can detect and classify the objects in the image.<\/li>\n<\/ol>\n
Using the Model in Your Application<\/h2>\n
Now that we have trained the custom image recognition model, we can use it in our application. To use the model in our application, we will make use of the Azure Cognitive Services API.<\/p>\n
To use the model, follow these steps:<\/p>\n
\n- Create a new Python file in Visual Studio Code.<\/li>\n
- Import the necessary libraries:<\/li>\n<\/ol>\n
import requests\nimport json\n<\/code><\/pre>\n\n- Set up the API endpoint and subscription key by replacing the placeholders in the code below with the values that you copied from the Azure portal:<\/li>\n<\/ol>\n
subscription_key = \"[Subscription key]\"\nendpoint = \"[Endpoint]\"\n<\/code><\/pre>\n\n- Create a function that will make a POST request to the Custom Vision API, passing in the image file. The API will return a JSON response containing the predicted objects and their coordinates.<\/li>\n<\/ol>\n
def predict_image(image_path):\n url = endpoint + \"\/vision\/v3.0-preview\/detect\"\n headers = {\"Prediction-Key\": subscription_key, \"Content-Type\": \"application\/octet-stream\"}\n data = open(image_path, \"rb\").read()\n response = requests.post(url, headers=headers, data=data)\n response.raise_for_status()\n return response.json()\n<\/code><\/pre>\n\n- Create a function that will display the predicted objects and their coordinates on the original image.<\/li>\n<\/ol>\n
import matplotlib.pyplot as plt\nimport matplotlib.patches as patches\n\ndef display_results(image_path, results):\n img = plt.imread(image_path)\n fig, ax = plt.subplots(1)\n ax.imshow(img)\n\n for prediction in results[\"predictions\"]:\n bbox = prediction[\"boundingBox\"]\n rect = patches.Rectangle((bbox[\"left\"],bbox[\"top\"]),bbox[\"width\"],bbox[\"height\"],linewidth=2,edgecolor='r',facecolor='none')\n ax.add_patch(rect)\n ax.text(bbox[\"left\"],bbox[\"top\"],prediction[\"tagName\"],fontsize=8,color='w')\n\n plt.show()\n<\/code><\/pre>\n\n- Call the functions to make a prediction on an image and display the results.<\/li>\n<\/ol>\n
image_path = \"path\/to\/image.jpg\"\nresults = predict_image(image_path)\ndisplay_results(image_path, results)\n<\/code><\/pre>\nConclusion<\/h2>\n
In this tutorial, we learned how to build a custom image recognition solution with Azure Cognitive Services. We set up Azure Cognitive Services and Azure Storage, trained a custom image recognition model with the Custom Vision API, and used the model in our application with the Azure Cognitive Services API. With the help of Azure Cognitive Services, we can quickly and easily build intelligent features into our applications without having to start from scratch.<\/p>\n","protected":false},"excerpt":{"rendered":"
Building a Custom Image Recognition Solution with Azure Cognitive Services In recent years, the field of computer vision has seen significant advancements, and image recognition has become an essential tool for various industries. With the help of deep learning techniques, we can train powerful neural networks that can recognize objects, Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[39,769,41,51,1129],"yoast_head":"\nBuilding an image recognition solution with Azure Cognitive Services - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n