This repository has been archived by the owner on Dec 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
129 lines (99 loc) · 3.25 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from __future__ import absolute_import
import base64
from base64 import b64decode
import contextlib
import io
import json
import os
import time
import shlex
import shutil
import subprocess
import sys
import tempfile
import boto3
from IPython.display import HTML, Audio
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
def get_execution_role(role_name="sagemaker", aws_account=None, aws_region=None):
"""
Create sagemaker execution role to perform sagemaker task
Args:
role_name (string): name of the role to be created
aws_account (string): aws account of the ECR repo
aws_region (string): aws region where the repo is located
"""
session = boto3.Session()
aws_account = aws_account or session.client("sts").get_caller_identity()['Account']
aws_region = aws_region or session.region_name
assume_role_policy_document = json.dumps({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": ["sagemaker.amazonaws.com", "robomaker.amazonaws.com"]
},
"Action": "sts:AssumeRole"
}
]
})
client = session.client('iam')
try:
client.get_role(RoleName=role_name)
except client.exceptions.NoSuchEntityException:
client.create_role(
RoleName=role_name,
AssumeRolePolicyDocument=str(assume_role_policy_document)
)
print("Created new sagemaker execution role: %s" % role_name)
client.attach_role_policy(
PolicyArn='arn:aws:iam::aws:policy/AmazonSageMakerFullAccess',
RoleName=role_name
)
return client.get_role(RoleName=role_name)['Role']['Arn']
def show_webcam():
quality = 0.8
size = (512, 384)
VIDEO_HTML = """
<h3>Live Feed</h3>
<video autoplay height=%d></video>
<h3>Predicted Feed</h3>
<canvas id="annotated" width=%d height=%d></canvas>
<script>
var video = document.querySelector('video')
var canvas = document.getElementById('annotated')
navigator.mediaDevices.getUserMedia({ video: true })
.then(stream=> video.srcObject = stream)
function handle_output(output) {
console.log(output)
var canvas = document.getElementById('annotated')
var ctx = canvas.getContext("2d");
var image = new Image();
image.onload = function() {
ctx.drawImage(image, 0, 0);
};
image.src = output.content.text
update_image()
}
function update_image() {
var video = document.querySelector('video')
var canvas = document.createElement('canvas')
var [w,h] = [video.offsetWidth, video.offsetHeight]
canvas.width = w
canvas.height = h
canvas.getContext('2d')
.drawImage(video, 0, 0, w, h)
command = 'get_annotated_image("' + canvas.toDataURL('image/jpeg', %f) + '")'
var callbacks = {
iopub : {
output : handle_output,
}
}
IPython.notebook.kernel.execute(command, callbacks)
}
setTimeout(update_image, 1000);
</script>
"""
display(HTML(VIDEO_HTML % ( size[1], size[0], size[1], quality)))