Python MQTT Publish JSONified Numpy Array











up vote
0
down vote

favorite












I am currently implementing a MQTT protocol to be used between two raspberry pis. The first is a Pi 0 and will have a pi camera connected to it. It will be converting each captured frame to a numpy array and then publish it to the master Pi which will then convert the numpy array to an image using PIL. I am doing it this way since I want the main image processing operations to be performed on the master Pi.



My problem is that no messages are being received by the master Pi. I have preformed debugging and everything is working fine on the servant script. But for some reason nothing is received by the master Pi.



Here are both scripts:



servant.py:



import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
frame_list = frame.tolist()
MQTT_MESSAGE = json.dumps(frame_list)
mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
time.sleep(1)


master.py:



import paho.mqtt.client as mqtt
import numpy as np
import json
import PIL

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

def on_connect(client, userdata, flags, rc):
print("connected with result code " + str(rc))
client.subscribe(MQTT_PATH)

def on_message(client, userdata, msg):
data = json.loads(msg.payload)
array = np.array(data)
img = PIL.Image.fromarray(array)
cv2.imshow('image', img)
cv2.waitKey()

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER, 1883, 60)

client.loop_forever()









share|improve this question









New contributor




Noor Sabbagh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • You need to explain what you have tried, just saying "I have performed debugging" doesn't help
    – hardillb
    yesterday










  • Also you are blocking in the on_message by calling cv2.waitKey() this means the whole subscriber will lock up while processing the first message until you press a key. You should not block messaging handling.
    – hardillb
    yesterday






  • 1




    Don't need to call mqttc.loop_start() in your client (or periodically call loop from within your for loop)?
    – larsks
    yesterday










  • @hardillb sorry i didnt specify the exact debugging techniques but i think it is redundant nonetheless because the servant script is working perfectly fine. But, to answer your question i did a few logging statements on the servant.py and the data is being processed properly and is jsonified. I also made a dummy numpy array and that actually went through and was published. But for some reason the moment i use opencv it stops working. Once again even with the opencv on the servant.py everything works fine, publishing is the problem.
    – Noor Sabbagh
    yesterday










  • @hardillb as for your suggestion I removed that line but there was still nothing showing up. Don't even think that is the problem, because i quickly debugged it using a print statement, first line in the on_message method, and the msg.payload's value is not printed. Which means that somewhere between the publish and the on_message something is going wrong.
    – Noor Sabbagh
    yesterday















up vote
0
down vote

favorite












I am currently implementing a MQTT protocol to be used between two raspberry pis. The first is a Pi 0 and will have a pi camera connected to it. It will be converting each captured frame to a numpy array and then publish it to the master Pi which will then convert the numpy array to an image using PIL. I am doing it this way since I want the main image processing operations to be performed on the master Pi.



My problem is that no messages are being received by the master Pi. I have preformed debugging and everything is working fine on the servant script. But for some reason nothing is received by the master Pi.



Here are both scripts:



servant.py:



import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
frame_list = frame.tolist()
MQTT_MESSAGE = json.dumps(frame_list)
mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
time.sleep(1)


master.py:



import paho.mqtt.client as mqtt
import numpy as np
import json
import PIL

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

def on_connect(client, userdata, flags, rc):
print("connected with result code " + str(rc))
client.subscribe(MQTT_PATH)

def on_message(client, userdata, msg):
data = json.loads(msg.payload)
array = np.array(data)
img = PIL.Image.fromarray(array)
cv2.imshow('image', img)
cv2.waitKey()

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER, 1883, 60)

client.loop_forever()









share|improve this question









New contributor




Noor Sabbagh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • You need to explain what you have tried, just saying "I have performed debugging" doesn't help
    – hardillb
    yesterday










  • Also you are blocking in the on_message by calling cv2.waitKey() this means the whole subscriber will lock up while processing the first message until you press a key. You should not block messaging handling.
    – hardillb
    yesterday






  • 1




    Don't need to call mqttc.loop_start() in your client (or periodically call loop from within your for loop)?
    – larsks
    yesterday










  • @hardillb sorry i didnt specify the exact debugging techniques but i think it is redundant nonetheless because the servant script is working perfectly fine. But, to answer your question i did a few logging statements on the servant.py and the data is being processed properly and is jsonified. I also made a dummy numpy array and that actually went through and was published. But for some reason the moment i use opencv it stops working. Once again even with the opencv on the servant.py everything works fine, publishing is the problem.
    – Noor Sabbagh
    yesterday










  • @hardillb as for your suggestion I removed that line but there was still nothing showing up. Don't even think that is the problem, because i quickly debugged it using a print statement, first line in the on_message method, and the msg.payload's value is not printed. Which means that somewhere between the publish and the on_message something is going wrong.
    – Noor Sabbagh
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am currently implementing a MQTT protocol to be used between two raspberry pis. The first is a Pi 0 and will have a pi camera connected to it. It will be converting each captured frame to a numpy array and then publish it to the master Pi which will then convert the numpy array to an image using PIL. I am doing it this way since I want the main image processing operations to be performed on the master Pi.



My problem is that no messages are being received by the master Pi. I have preformed debugging and everything is working fine on the servant script. But for some reason nothing is received by the master Pi.



Here are both scripts:



servant.py:



import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
frame_list = frame.tolist()
MQTT_MESSAGE = json.dumps(frame_list)
mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
time.sleep(1)


master.py:



import paho.mqtt.client as mqtt
import numpy as np
import json
import PIL

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

def on_connect(client, userdata, flags, rc):
print("connected with result code " + str(rc))
client.subscribe(MQTT_PATH)

def on_message(client, userdata, msg):
data = json.loads(msg.payload)
array = np.array(data)
img = PIL.Image.fromarray(array)
cv2.imshow('image', img)
cv2.waitKey()

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER, 1883, 60)

client.loop_forever()









share|improve this question









New contributor




Noor Sabbagh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am currently implementing a MQTT protocol to be used between two raspberry pis. The first is a Pi 0 and will have a pi camera connected to it. It will be converting each captured frame to a numpy array and then publish it to the master Pi which will then convert the numpy array to an image using PIL. I am doing it this way since I want the main image processing operations to be performed on the master Pi.



My problem is that no messages are being received by the master Pi. I have preformed debugging and everything is working fine on the servant script. But for some reason nothing is received by the master Pi.



Here are both scripts:



servant.py:



import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
frame_list = frame.tolist()
MQTT_MESSAGE = json.dumps(frame_list)
mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
time.sleep(1)


master.py:



import paho.mqtt.client as mqtt
import numpy as np
import json
import PIL

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

def on_connect(client, userdata, flags, rc):
print("connected with result code " + str(rc))
client.subscribe(MQTT_PATH)

def on_message(client, userdata, msg):
data = json.loads(msg.payload)
array = np.array(data)
img = PIL.Image.fromarray(array)
cv2.imshow('image', img)
cv2.waitKey()

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER, 1883, 60)

client.loop_forever()






python numpy raspberry-pi mqtt






share|improve this question









New contributor




Noor Sabbagh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Noor Sabbagh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday





















New contributor




Noor Sabbagh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Noor Sabbagh

12




12




New contributor




Noor Sabbagh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Noor Sabbagh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Noor Sabbagh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • You need to explain what you have tried, just saying "I have performed debugging" doesn't help
    – hardillb
    yesterday










  • Also you are blocking in the on_message by calling cv2.waitKey() this means the whole subscriber will lock up while processing the first message until you press a key. You should not block messaging handling.
    – hardillb
    yesterday






  • 1




    Don't need to call mqttc.loop_start() in your client (or periodically call loop from within your for loop)?
    – larsks
    yesterday










  • @hardillb sorry i didnt specify the exact debugging techniques but i think it is redundant nonetheless because the servant script is working perfectly fine. But, to answer your question i did a few logging statements on the servant.py and the data is being processed properly and is jsonified. I also made a dummy numpy array and that actually went through and was published. But for some reason the moment i use opencv it stops working. Once again even with the opencv on the servant.py everything works fine, publishing is the problem.
    – Noor Sabbagh
    yesterday










  • @hardillb as for your suggestion I removed that line but there was still nothing showing up. Don't even think that is the problem, because i quickly debugged it using a print statement, first line in the on_message method, and the msg.payload's value is not printed. Which means that somewhere between the publish and the on_message something is going wrong.
    – Noor Sabbagh
    yesterday


















  • You need to explain what you have tried, just saying "I have performed debugging" doesn't help
    – hardillb
    yesterday










  • Also you are blocking in the on_message by calling cv2.waitKey() this means the whole subscriber will lock up while processing the first message until you press a key. You should not block messaging handling.
    – hardillb
    yesterday






  • 1




    Don't need to call mqttc.loop_start() in your client (or periodically call loop from within your for loop)?
    – larsks
    yesterday










  • @hardillb sorry i didnt specify the exact debugging techniques but i think it is redundant nonetheless because the servant script is working perfectly fine. But, to answer your question i did a few logging statements on the servant.py and the data is being processed properly and is jsonified. I also made a dummy numpy array and that actually went through and was published. But for some reason the moment i use opencv it stops working. Once again even with the opencv on the servant.py everything works fine, publishing is the problem.
    – Noor Sabbagh
    yesterday










  • @hardillb as for your suggestion I removed that line but there was still nothing showing up. Don't even think that is the problem, because i quickly debugged it using a print statement, first line in the on_message method, and the msg.payload's value is not printed. Which means that somewhere between the publish and the on_message something is going wrong.
    – Noor Sabbagh
    yesterday
















You need to explain what you have tried, just saying "I have performed debugging" doesn't help
– hardillb
yesterday




You need to explain what you have tried, just saying "I have performed debugging" doesn't help
– hardillb
yesterday












Also you are blocking in the on_message by calling cv2.waitKey() this means the whole subscriber will lock up while processing the first message until you press a key. You should not block messaging handling.
– hardillb
yesterday




Also you are blocking in the on_message by calling cv2.waitKey() this means the whole subscriber will lock up while processing the first message until you press a key. You should not block messaging handling.
– hardillb
yesterday




1




1




Don't need to call mqttc.loop_start() in your client (or periodically call loop from within your for loop)?
– larsks
yesterday




Don't need to call mqttc.loop_start() in your client (or periodically call loop from within your for loop)?
– larsks
yesterday












@hardillb sorry i didnt specify the exact debugging techniques but i think it is redundant nonetheless because the servant script is working perfectly fine. But, to answer your question i did a few logging statements on the servant.py and the data is being processed properly and is jsonified. I also made a dummy numpy array and that actually went through and was published. But for some reason the moment i use opencv it stops working. Once again even with the opencv on the servant.py everything works fine, publishing is the problem.
– Noor Sabbagh
yesterday




@hardillb sorry i didnt specify the exact debugging techniques but i think it is redundant nonetheless because the servant script is working perfectly fine. But, to answer your question i did a few logging statements on the servant.py and the data is being processed properly and is jsonified. I also made a dummy numpy array and that actually went through and was published. But for some reason the moment i use opencv it stops working. Once again even with the opencv on the servant.py everything works fine, publishing is the problem.
– Noor Sabbagh
yesterday












@hardillb as for your suggestion I removed that line but there was still nothing showing up. Don't even think that is the problem, because i quickly debugged it using a print statement, first line in the on_message method, and the msg.payload's value is not printed. Which means that somewhere between the publish and the on_message something is going wrong.
– Noor Sabbagh
yesterday




@hardillb as for your suggestion I removed that line but there was still nothing showing up. Don't even think that is the problem, because i quickly debugged it using a print statement, first line in the on_message method, and the msg.payload's value is not printed. Which means that somewhere between the publish and the on_message something is going wrong.
– Noor Sabbagh
yesterday












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










In your publishing code you are not actually giving the client any time to process the message it is trying to send. This is even more of a problem because the message is likely to be larger than a single network packet (being an image). To fix this you have to call the MQTT client loop function (or start the loop in the b



import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
frame_list = frame.tolist()
MQTT_MESSAGE = json.dumps(frame_list)
mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
mqttc.loop()
time.sleep(1)


or like this:



import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

mqttc.start_loop()

while True:
ret, frame = cap.read()
frame_list = frame.tolist()
MQTT_MESSAGE = json.dumps(frame_list)
mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
time.sleep(1)





share|improve this answer





















  • Thank you so much, one more question, do you think this is a good way of doing things or is there a more lightweight way of doing it? Actually in my real script im going to have the pi camera module on the servant.py instead of opencv. Is byte streaming the image to the master going to be more efficient and faster than my current way?
    – Noor Sabbagh
    22 hours ago










  • But the problem is still there. I removed the waitkey and added the loop for the client but the master is still not receiving anything.
    – Noor Sabbagh
    21 hours ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Noor Sabbagh is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372285%2fpython-mqtt-publish-jsonified-numpy-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










In your publishing code you are not actually giving the client any time to process the message it is trying to send. This is even more of a problem because the message is likely to be larger than a single network packet (being an image). To fix this you have to call the MQTT client loop function (or start the loop in the b



import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
frame_list = frame.tolist()
MQTT_MESSAGE = json.dumps(frame_list)
mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
mqttc.loop()
time.sleep(1)


or like this:



import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

mqttc.start_loop()

while True:
ret, frame = cap.read()
frame_list = frame.tolist()
MQTT_MESSAGE = json.dumps(frame_list)
mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
time.sleep(1)





share|improve this answer





















  • Thank you so much, one more question, do you think this is a good way of doing things or is there a more lightweight way of doing it? Actually in my real script im going to have the pi camera module on the servant.py instead of opencv. Is byte streaming the image to the master going to be more efficient and faster than my current way?
    – Noor Sabbagh
    22 hours ago










  • But the problem is still there. I removed the waitkey and added the loop for the client but the master is still not receiving anything.
    – Noor Sabbagh
    21 hours ago















up vote
0
down vote



accepted










In your publishing code you are not actually giving the client any time to process the message it is trying to send. This is even more of a problem because the message is likely to be larger than a single network packet (being an image). To fix this you have to call the MQTT client loop function (or start the loop in the b



import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
frame_list = frame.tolist()
MQTT_MESSAGE = json.dumps(frame_list)
mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
mqttc.loop()
time.sleep(1)


or like this:



import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

mqttc.start_loop()

while True:
ret, frame = cap.read()
frame_list = frame.tolist()
MQTT_MESSAGE = json.dumps(frame_list)
mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
time.sleep(1)





share|improve this answer





















  • Thank you so much, one more question, do you think this is a good way of doing things or is there a more lightweight way of doing it? Actually in my real script im going to have the pi camera module on the servant.py instead of opencv. Is byte streaming the image to the master going to be more efficient and faster than my current way?
    – Noor Sabbagh
    22 hours ago










  • But the problem is still there. I removed the waitkey and added the loop for the client but the master is still not receiving anything.
    – Noor Sabbagh
    21 hours ago













up vote
0
down vote



accepted







up vote
0
down vote



accepted






In your publishing code you are not actually giving the client any time to process the message it is trying to send. This is even more of a problem because the message is likely to be larger than a single network packet (being an image). To fix this you have to call the MQTT client loop function (or start the loop in the b



import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
frame_list = frame.tolist()
MQTT_MESSAGE = json.dumps(frame_list)
mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
mqttc.loop()
time.sleep(1)


or like this:



import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

mqttc.start_loop()

while True:
ret, frame = cap.read()
frame_list = frame.tolist()
MQTT_MESSAGE = json.dumps(frame_list)
mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
time.sleep(1)





share|improve this answer












In your publishing code you are not actually giving the client any time to process the message it is trying to send. This is even more of a problem because the message is likely to be larger than a single network packet (being an image). To fix this you have to call the MQTT client loop function (or start the loop in the b



import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
frame_list = frame.tolist()
MQTT_MESSAGE = json.dumps(frame_list)
mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
mqttc.loop()
time.sleep(1)


or like this:



import paho.mqtt.client as mqtt
import time
import cv2
import numpy
import json

MQTT_SERVER = "iot.eclipse.org"
MQTT_PATH = "test_channel"

mqttc = mqtt.Client()
mqttc.connect(MQTT_SERVER, 1883, 60)

cap = cv2.VideoCapture(0)

mqttc.start_loop()

while True:
ret, frame = cap.read()
frame_list = frame.tolist()
MQTT_MESSAGE = json.dumps(frame_list)
mqttc.publish(MQTT_PATH, MQTT_MESSAGE)
time.sleep(1)






share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









hardillb

23.1k63059




23.1k63059












  • Thank you so much, one more question, do you think this is a good way of doing things or is there a more lightweight way of doing it? Actually in my real script im going to have the pi camera module on the servant.py instead of opencv. Is byte streaming the image to the master going to be more efficient and faster than my current way?
    – Noor Sabbagh
    22 hours ago










  • But the problem is still there. I removed the waitkey and added the loop for the client but the master is still not receiving anything.
    – Noor Sabbagh
    21 hours ago


















  • Thank you so much, one more question, do you think this is a good way of doing things or is there a more lightweight way of doing it? Actually in my real script im going to have the pi camera module on the servant.py instead of opencv. Is byte streaming the image to the master going to be more efficient and faster than my current way?
    – Noor Sabbagh
    22 hours ago










  • But the problem is still there. I removed the waitkey and added the loop for the client but the master is still not receiving anything.
    – Noor Sabbagh
    21 hours ago
















Thank you so much, one more question, do you think this is a good way of doing things or is there a more lightweight way of doing it? Actually in my real script im going to have the pi camera module on the servant.py instead of opencv. Is byte streaming the image to the master going to be more efficient and faster than my current way?
– Noor Sabbagh
22 hours ago




Thank you so much, one more question, do you think this is a good way of doing things or is there a more lightweight way of doing it? Actually in my real script im going to have the pi camera module on the servant.py instead of opencv. Is byte streaming the image to the master going to be more efficient and faster than my current way?
– Noor Sabbagh
22 hours ago












But the problem is still there. I removed the waitkey and added the loop for the client but the master is still not receiving anything.
– Noor Sabbagh
21 hours ago




But the problem is still there. I removed the waitkey and added the loop for the client but the master is still not receiving anything.
– Noor Sabbagh
21 hours ago










Noor Sabbagh is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Noor Sabbagh is a new contributor. Be nice, and check out our Code of Conduct.













Noor Sabbagh is a new contributor. Be nice, and check out our Code of Conduct.












Noor Sabbagh is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372285%2fpython-mqtt-publish-jsonified-numpy-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]