Skip to content

Instantly share code, notes, and snippets.

@hayatoy
Created May 2, 2019 15:06
Show Gist options
  • Save hayatoy/97f2660cad08e8b6b868fac110a6ef2d to your computer and use it in GitHub Desktop.
Save hayatoy/97f2660cad08e8b6b868fac110a6ef2d to your computer and use it in GitHub Desktop.
picamera+overlay
for foo in camera.capture_continuous(stream,
format='rgb',
use_video_port=True):
# Make Image object from camera stream
stream.truncate()
stream.seek(0)
input = np.frombuffer(stream.getvalue(), dtype=np.uint8)
input = input.reshape((CAMERA_HEIGHT, CAMERA_WIDTH, 3))
image = Image.fromarray(input)
# image.save("out.jpg")
# Make overlay image plane
img = Image.new('RGBA',
(CAMERA_WIDTH, CAMERA_HEIGHT),
(255, 0, 0, 0))
draw = ImageDraw.Draw(img)
# Run detection
start_ms = time.time()
results = engine.DetectWithImage(image,
threshold=0.2, top_k=10)
elapsed_ms = (time.time() - start_ms)*1000.0
if results:
for obj in results:
box = obj.bounding_box.flatten().tolist()
box[0] *= CAMERA_WIDTH
box[1] *= CAMERA_HEIGHT
box[2] *= CAMERA_WIDTH
box[3] *= CAMERA_HEIGHT
# print(box)
# print(labels[obj.label_id])
draw.rectangle(box, outline='red')
draw.text((box[0], box[1]-10), labels[obj.label_id],
font=fnt, fill="red")
camera.annotate_text = "{0:.2f}ms".format(elapsed_ms)
if not overlay_renderer:
overlay_renderer = camera.add_overlay(
img.tobytes(),
size=(CAMERA_WIDTH, CAMERA_HEIGHT), layer=4, alpha=255)
else:
overlay_renderer.update(img.tobytes())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment