1.
pip3 install azure-cognitiveservices-vision-customvision
2.
pip3 install azure-iot-device
3.
pip3 install asyncio
4.
pip3 install azure-storage-blob
5.
sudo apt-get install fswebcam
01.
from
azure.cognitiveservices.vision.customvision.prediction
import
CustomVisionPredictionClient
02.
msrest.authentication
ApiKeyCredentials
03.
azure.storage.blob
ContentSettings, BlobClient
04.
azure.iot.device
Message
05.
azure.iot.device.aio
IoTHubDeviceClient
06.
07.
os
08.
PIL
Image
09.
ImageDraw
10.
matplotlib.pyplot as plt
11.
asyncio
12.
13.
# custom vision api
14.
credentials
=
ApiKeyCredentials(in_headers
{
"Prediction-key"
:
"Your Key"
})
15.
predictor
CustomVisionPredictionClient(
"https://***.cognitiveservices.azure.com/"
, credentials)
16.
projectID
"******"
17.
publish_iteration_name
"Iteration*"
18.
19.
#Bolb Storage
20.
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/;"
21.
container_name
"raspberrypic"
22.
blob_name
"bird"
23.
# Create instance of the device client
24.
blob_client
BlobClient.from_connection_string(conn_str, container_name, blob_name)
25.
26.
# Azure IotHub
27.
CONNECTION_STRING
"HostName=***.azure-devices.net;DeviceId=***;SharedAccessKey=***"
28.
29.
iothub_client
IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
30.
target_num
0
31.
PAYLOAD
'{{"No. of target detected": {target_num}}}'
32.
33.
async
def
CustomVisionApp():
34.
35.
print
(
"===== Describe an image - camera ====="
)
36.
# capture the image with USB webcamera
37.
a
os.system(
"fswebcam --no-banner -r 1280x720 capture.jpg"
38.
(a)
39.
# open and detect the captured image
40.
with open(
"capture.jpg"
, mode
"rb"
) as captured_image:
41.
results
predictor.detect_image(projectID, publish_iteration_name, captured_image)
42.
# Display the results.
43.
for
prediction
in
results.predictions:
44.
global
45.
if
prediction.probability>
0.6
46.
+
1
47.
"\t"
prediction.tag_name
": {0:.2f}%"
.format(prediction.probability
*
100
))
48.
bbox
prediction.bounding_box
49.
im
Image.open(
50.
draw
ImageDraw.Draw(im)
51.
draw.rectangle([int(bbox.left
1280
), int(bbox.top
720
), int((bbox.left
bbox.width)
), int((bbox.top
bbox.height)
)],outline
'red'
,width
5
52.
im.save(
"detect.jpg"
53.
de
54.
plt.figure(
"Result"
55.
plt.imshow(de)
56.
plt.show()
57.
58.
#data formating
59.
data
PAYLOAD.format(target_num
target_num)
60.
message
Message(data)
61.
# Send a message to the IoT hub
62.
(f
"Sending message: {message}"
63.
await iothub_client.send_message(message)
64.
"Message successfully sent"
65.
66.
# upload the image to Azure Blob Storage, Overwrite if it already exists!
67.
image_content_setting
ContentSettings(content_type
'image/jpeg'
68.
,
) as data:
69.
blob_client.upload_blob(data,overwrite
True
,content_settings
image_content_setting)
70.
"Upload completed"
71.
72.
__name__
'__main__'
73.
asyncio.run(CustomVisionApp())