I created two programs to send and receive video feed using ZeroMQ. However, the receiving program always gets stuck on the .recv()
-method.
I have used two libraries of ZeroMQ for this program: one, the native zmq
, the other a derived imagezmq
. The imagezmq
is used for sending and receiving frame data from the video while the native zmq
library is used for sending and receiving the time, when the image has been sent.
The imagezmq
part works fine.
The program only gets stuck on the zmq
part.
Following are my two programs :
FinalCam.py
import struct
import time
import imutils
import imagezmq
import cv2
import zmq
import socket
import pickle
# # image sending
sender = imagezmq.ImageSender(connect_to='tcp://localhost:5555')
hostName = socket.gethostname() # send RPi hostname with each image
vid_dir = "/root/redis-opencv-videostream/vtest.avi"
cap = cv2.VideoCapture(vid_dir) # init the camera
context = zmq.Context() # setup for sending time
socket = context.socket(zmq.PUB)
socket.connect("tcp://localhost:6666")
while True: # send images as stream until Ctrl-C
ret, frame = cap.read()
frame = imutils.resize(frame, width=400) # resize without compressionq
captureTime = time.time()
sender.send_image(hostName, frame)
print (captureTime)
captureTime = struct.pack('d', captureTime)
#msg = pickle.dumps(captureTime)
print("message primed")
socket.send(captureTime)
print("time sent")
which generated this output :
1591824603.5772414
message primed
time sent
FinalRecieve.py
import cv2
import imagezmq
import time
import zmq
import struct
FRAMES = 5
image_hub = imagezmq.ImageHub() # image socket
context = zmq.Context() # time socket
socket = context.socket(zmq.SUB)
socket.bind("tcp://*:6666")
while True: # show streamed images until Ctrl-C
loopTime = time.time()
for i in range (0, FRAMES):
hostName, frame = image_hub.recv_image()
image_hub.send_reply(b'OK')
print("recieved image, waiting for time")
captureTime = socket.recv()
print("meow")
print(captureTime)
finishTime = time.time()
fpsTime = finishTime - loopTime
fps = FRAMES / fpsTime
print(fps)
which generated this output :
received image, waiting for time