Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2020-2-capstone-design1
/
HEN_project
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
최지우
2020-11-17 22:08:14 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
fda4ea75a098345c21e6679a6cb089c158b54fc4
fda4ea75
1 parent
03869256
add Lambda code & SageMaker code
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
109 additions
and
0 deletions
Code/Image-classification-fulltraining.ipynb
Code/yogaprojectlambda.py
Code/Image-classification-fulltraining.ipynb
View file @
fda4ea7
This diff is collapsed. Click to expand it.
Code/yogaprojectlambda.py
0 → 100644
View file @
fda4ea7
import
os
import
greengrasssdk
from
threading
import
Timer
import
time
import
awscam
import
cv2
import
mo
from
threading
import
Thread
# Creating a greengrass core sdk client
client
=
greengrasssdk
.
client
(
'iot-data'
)
# The information exchanged between IoT and clould has
# a topic and a message body.
# This is the topic that this code uses to send messages to cloud
iotTopic
=
'$aws/things/{}/infer'
.
format
(
os
.
environ
[
'AWS_IOT_THING_NAME'
])
jpeg
=
None
Write_To_FIFO
=
True
class
FIFO_Thread
(
Thread
):
def
__init__
(
self
):
''' Constructor. '''
Thread
.
__init__
(
self
)
def
run
(
self
):
fifo_path
=
"/tmp/results.mjpeg"
if
not
os
.
path
.
exists
(
fifo_path
):
os
.
mkfifo
(
fifo_path
)
f
=
open
(
fifo_path
,
'w'
)
client
.
publish
(
topic
=
iotTopic
,
payload
=
"Opened Pipe"
)
while
Write_To_FIFO
:
try
:
f
.
write
(
jpeg
.
tobytes
())
except
IOError
as
e
:
continue
def
greengrass_infinite_infer_run
():
try
:
input_width
=
224
input_height
=
224
model_name
=
"image-classification"
error
,
model_path
=
mo
.
optimize
(
model_name
,
input_width
,
input_height
,
aux_inputs
=
{
'--epoch'
:
2
,
'--precision'
:
'FP32'
})
# The aux_inputs is equal to the number of epochs and in this case, it is 300
# Load model to GPU (use {"GPU": 0} for CPU)
mcfg
=
{
"GPU"
:
1
}
model
=
awscam
.
Model
(
model_path
,
mcfg
)
client
.
publish
(
topic
=
iotTopic
,
payload
=
"Model loaded"
)
model_type
=
"classification"
with
open
(
'caltech256_labels.txt'
,
'r'
)
as
f
:
labels
=
[
l
.
rstrip
()
for
l
in
f
]
topk
=
2
results_thread
=
FIFO_Thread
()
results_thread
.
start
()
# Send a starting message to IoT console
client
.
publish
(
topic
=
iotTopic
,
payload
=
"Inference is starting"
)
doInfer
=
True
while
doInfer
:
# Get a frame from the video stream
ret
,
frame
=
awscam
.
getLastFrame
()
# Raise an exception if failing to get a frame
if
ret
==
False
:
raise
Exception
(
"Failed to get frame from the stream"
)
# Resize frame to fit model input requirement
frameResize
=
cv2
.
resize
(
frame
,
(
input_width
,
input_height
))
# Run model inference on the resized frame
inferOutput
=
model
.
doInference
(
frameResize
)
# Output inference result to the fifo file so it can be viewed with mplayer
parsed_results
=
model
.
parseResult
(
model_type
,
inferOutput
)
top_k
=
parsed_results
[
model_type
][
0
:
topk
]
msg
=
'{'
prob_num
=
0
for
obj
in
top_k
:
if
prob_num
==
topk
-
1
:
msg
+=
'"{}": {:.2f}'
.
format
(
labels
[
obj
[
"label"
]],
obj
[
"prob"
]
*
100
)
else
:
msg
+=
'"{}": {:.2f},'
.
format
(
labels
[
obj
[
"label"
]],
obj
[
"prob"
]
*
100
)
prob_num
+=
1
msg
+=
"}"
client
.
publish
(
topic
=
iotTopic
,
payload
=
msg
)
if
top_k
[
0
][
"prob"
]
*
100
>
65
:
cv2
.
putText
(
frame
,
labels
[
top_k
[
0
][
"label"
]]
+
' '
+
str
(
top_k
[
0
][
"prob"
]
*
100
),
(
0
,
22
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
1
,
(
255
,
165
,
20
),
4
)
global
jpeg
ret
,
jpeg
=
cv2
.
imencode
(
'.jpg'
,
frame
)
except
Exception
as
e
:
msg
=
"myModel Lambda failed: "
+
str
(
e
)
client
.
publish
(
topic
=
iotTopic
,
payload
=
msg
)
# Asynchronously schedule this function to be run again in 15 seconds
Timer
(
15
,
greengrass_infinite_infer_run
)
.
start
()
# Execute the function above
greengrass_infinite_infer_run
()
# This is a dummy handler and will not be invoked
# Instead the code above will be executed in an infinite loop for our example
def
function_handler
(
event
,
context
):
return
\ No newline at end of file
Please
register
or
login
to post a comment