AWS DeepLens Guia página 109

Tabla de contenido
result_path = '/tmp/results.mjpeg'
# Create the FIFO file if it doesn't exist.
if not os.path.exists(result_path):
os.mkfifo(result_path)
# This call will block until a consumer is available
with open(result_path, 'w') as fifo_file:
while not self.stop_request.isSet():
try:
# Write the data to the FIFO file. This call will block
# meaning the code will come to a halt here until a consumer
# is available.
fifo_file.write(self.frame.tobytes())
except IOError:
continue
def set_frame_data(self, frame):
""" Method updates the image data. This currently encodes the
numpy array to jpg but can be modified to support other encodings.
frame - Numpy array containing the image data of the next frame
in the project stream.
"""
ret, jpeg = cv2.imencode('.jpg', cv2.resize(frame, self.resolution))
if not ret:
raise Exception('Failed to set frame data')
self.frame = jpeg
def join(self):
self.stop_request.set()
class HeadDetection():
""" Custom class that helps us post process the data. In particular it draws
a ball that moves across the screen depending on the head pose.
It also draws a rectangle indicating the region that the person's head is pointing
to. We divide the frame into 9 distinct regions.
"""
def __init__(self, circ_cent_x, circ_cent_y):
""" circ_cent_x - The x coordinate for the center of the frame
circ_cent_y - The y coordinate for the center of the frame
"""
self.result_thread = LocalDisplay('480p')
self.result_thread.start()
self.circ_cent_x = circ_cent_x
self.circ_cent_y = circ_cent_y
# Compute the maximum x and y coordinates.
self.x_max = 2 * circ_cent_x
self.y_max = 2 * circ_cent_y
# Number of quadrants to split the image into.
# This is model dependent.
self.quadrants = 9
def update_coords(self, frame, change_x, change_y, label):
""" Helper method that draws a rectangle in the region the person is looking at.
It also draws a red ball that changes its speed based on how long a person
has been looking at a particular direction.
frame - The frame where the ball and rectangle should be drawn
change_x - The amount to increment the x axis after drawing the red ball.
change_y - The amount to increment the y axis after drawing the red ball.
label - Label corresponding to the region that the person's head is looking at.
"""
# Set coordinates of the rectangle that will be drawn in the region that the
# person is looking at.
rect_margin = 10
rect_width = frame.shape[1] // 3 - rect_margin * 2
rect_height = frame.shape[0] // 3 - rect_margin * 2
# Set the draw options.
overlay = frame.copy()
AWS DeepLens Guía para desarrolladores
Crear y ejecutar el proyecto de
detección de posición de cabeza
105
Tabla de contenido
loading

Tabla de contenido