from
djitellopy
import
tello
KeyPressModule as kp
time
cv2
PIL
Image
ImageDraw
azure.storage.blob
ContentSettings, BlobClient
azure.cognitiveservices.vision.customvision.prediction
CustomVisionPredictionClient
msrest.authentication
ApiKeyCredentials
kp.init()
me
=
tello.Tello()
me.connect()
print
(me.get_battery())
me.streamon()
global
img
# connection string for Azure Blob storage
conn_str
"your connection string"
container_name
"raspberrypic"
blob_name
"capture"
# custom vision api
credentials
ApiKeyCredentials(in_headers
{
"Prediction-key"
:
"your key"
})
predictor
CustomVisionPredictionClient(
"your endpoint"
, credentials)
projectID
"your project ID"
publish_iteration_name
"your iteration name"
def
getKeyboardInput():
lr, fb, ud, yv
0
,
speed
50
if
kp.getKey(
"LEFT"
):
lr
-
elif
"RIGHT"
"UP"
fb
"DOWN"
"w"
ud
"s"
"a"
yv
"d"
"q"
): me.land()
"e"
): me.takeoff()
"z"
cv2.imwrite(f
'Resources/Images/capture.jpg'
, img)
time.sleep(
0.1
)
target
# open and detect the captured image
with open(f
, mode
"rb"
) as captured_image:
results
predictor.detect_image(projectID, publish_iteration_name, captured_image)
# Display the results.
for
prediction
in
results.predictions:
prediction.probability >
0.6
1
(
"\t"
+
prediction.tag_name
": {0:.2f}%"
.format(prediction.probability
*
100
))
bbox
prediction.bounding_box
im
Image.open(f
draw
ImageDraw.Draw(im)
draw.rectangle([int(bbox.left
1280
), int(bbox.top
720
), int((bbox.left
bbox.width)
),
int((bbox.top
bbox.height)
)], outline
'red'
, width
5
im.save(f
'Resources/Images/results.jpg'
cv2.imread(f
, cv2.IMREAD_COLOR)
cv2.imshow(
"results"
cv2.waitKey(
1000
cv2.destroyWindow(
else
# upload the image to Azure Blob Storage, Overwrite if it already exists!
blob
BlobClient.from_connection_string(conn_str, container_name, blob_name)
image_content_setting
ContentSettings(content_type
'image/jpeg'
) as data:
try
blob.upload_blob(data, overwrite
True
, content_settings
image_content_setting)
"Blob storage uploading completed"
except
ValueError:
"Blob storage uploading error"
return
[lr, fb, ud, yv]
main():
"Tello Image Custom Vision Demo"
while
vals
getKeyboardInput()
me.send_rc_control(vals[
], vals[
2
3
])
me.get_frame_read().frame
cv2.resize(img, (
cv2.putText(img, str(me.get_current_state()), (
10
60
), cv2.FONT_HERSHEY_PLAIN,
0.9
, (
255
"image"
__name__
"__main__"
main()