Skip to content

Instantly share code, notes, and snippets.

@h2suzuki
Created March 2, 2017 12:00
Show Gist options
  • Save h2suzuki/dd47d31cfe9e0db13f357e39d00e5676 to your computer and use it in GitHub Desktop.
Save h2suzuki/dd47d31cfe9e0db13f357e39d00e5676 to your computer and use it in GitHub Desktop.
import numpy as np
import time
import scorer
import zmq
import zmq_utils
# Setup ZeroMQ image publisher & receiver
context = zmq.Context()
pub = context.socket(zmq.PUB)
pub.set_hwm(1)
pub.bind("tcp://*:5558")
sub = context.socket(zmq.SUB)
sub.setsockopt_string(zmq.SUBSCRIBE, "image")
sub.set_hwm(1)
sub.bind("tcp://*:5559")
# Setup VideoCaputre Object
cap = scorer.VideoCapture(0)
last_tick_pub = last_tick_sub = time.time()
duration_pub = duration_sub = 0
while True:
frame = cap.read()
if frame == None:
continue
# Publish the image
image = frame.get_bgr()
zmq_utils.send_image(pub, image)
tick = time.time()
duration_pub = tick - last_tick_pub
last_tick_pub = tick
# Receive the transformed image
result = None
try:
result = zmq_utils.recv_image(sub, flags=zmq.NOBLOCK)
except zmq.error.Again:
pass
if result is not None:
tick = time.time()
duration_sub = tick - last_tick_sub
last_tick_sub = tick
# Output
scorer.imshow(1, result)
print("Send {0:>7.3f} ({1:>7.3f} fps) Recv {2:>7.3f} ({3:>7.3f} fps)".
format(duration_pub,
1.0/duration_pub if duration_pub>0 else 0.0,
duration_sub,
1.0/duration_sub if duration_sub>0 else 0.0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment