다음을 통해 공유


Azure Cloud Services for Raspberry Pi 4: How to capture and send image to Azure Blob Storage

Introduction

Microsoft Azure provides a variety of cloud computing services including artificial intelligence, machine learning, IoT, storage, security, networking, media, integration and so on. Among tens of thousands of IoT end devices, Raspberry Pi is one of the most popular and useful platforms that used in many areas because of its low cost, modularity, and open design. Raspberry Pi 4 is the latest powerful model with Quad core Cortex-A72 64-bit processor, 2 HDMI ports, 40 pin GPIO header and hardware video decode capability. Azure Blob storage is Microsoft's object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data, for example, images, documents, video and audio files.
In this article, we will walk you through the steps required to capture and send real time image to Azure Blob Storage on Raspberry Pi 4 model B with Azure Storage Blobs client library for Python.

Prerequisites

  1. Raspberry Pi 4 model B
  2. Azure Storage Blobs client library for Python
  3. USB Web Camera or CSI Camera

Hardware Connection

In this project, we use Microsoft LifeCam HD-3000 USB web camera. This true HD camera features 720p HD video chat for a true HD-quality experience and TrueColor technology that automatically delivers a bright and colorful video. 

Install Python Azure IoT SDK and fswebcam

Open your Raspberry Pi terminal and install Azure Storage Blobs client library for Python:

1.pip3 install azure-storage-blob
2.sudo apt-get install fswebcam

Create an Azure Storage Account

We can create an Azure Storage Account using the Azure Portal. Please refer to the “Create a storage account” section on this page: Create a storage account. Then, in order to send the files to Azure Storage, we should get the connection string. The “View account access keys” section is available on this page Manage storage account access keys to help you with this. This connection string will be used by Python code to communicate with the Azure Storage.

Create and Debug Python Code on Raspberry Pi

Currently, there are several Python IDE on Raspberry Pi. Thonny Python IDE is bundled with the Raspbian OS. We can launch it by click Programming -> Thonny Python IDE. Then copy and paste the following code.

01.from azure.storage.blob import ContentSettings, BlobClient
02.import os
03.from PIL import Image
04.import matplotlib.pyplot as plt
05. 
06.conn_str="DefaultEndpointsProtocol=https;AccountName=***;AccountKey=***;BlobEndpoint=https://***.blob.core.windows.net/;QueueEndpoint=https://***.queue.core.windows.net/;TableEndpoint=https://***.table.core.windows.net/;FileEndpoint=https://***.file.core.windows.net/;"
07.container_name="***"
08.blob_name="capture"
09. 
10.def main():
11. 
12.    print("===== Taking an image - camera =====")
13.    # capture the image with USB webcamera
14.    a=os.system("fswebcam --no-banner -r 1280x720 capture.jpg")
15.    print(a)
16.    # upload the image to Azure Blob Storage, Overwrite if it already exists!
17.    blob =  BlobClient.from_connection_string(conn_str, container_name, blob_name)
18.    image_content_setting =  ContentSettings(content_type='image/jpeg')
19.    with open("capture.jpg", "rb") as data:
20.        blob.upload_blob(data,overwrite=True,content_settings=image_content_setting)
21.        print("Upload completed")
22.    # show captured image
23.    de=Image.open("capture.jpg")
24.    plt.figure("Result")
25.    plt.imshow(de)
26.    plt.show()
27.     
28.if __name__ == '__main__':
29.    main()

Please do substitute the connection string with yours that created in section “Create an Azure Storage Account”. Then, press Run or Debug button to start the process. You will see the output in Shell window as shown in Fig. 1.

Fig. 1 Debug information on Shell window

Then, the captured image will be displayed on the screen as follows.

Fig.2. The image captured by web camera

Monitor the Blob Storage with Azure Storage Explorer

Install and run Azure Storage Explorer, choose the Blob Containers that you created. And then click Reflash. We will see the image file as soon as the Python application send out. It is shown in Fig. 3.

Fig. 3 Captured image by Azure IoT Explorer

Summary

In this tutorial, we have presented how to capture image by USB web camera and send it to Azure Blob Storage by Azure Storage Blobs client library for Python.

Resources

  1. MS Docs for Azure Blob storage.
  2. MS Docs for Create a storage account.
  3. MS Docs for Manage storage account access keys

See Also

Azure Cloud Services for Raspberry Pi 4: How to send sensor data to Azure IoT Hub