微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我如何发布和订阅图像在python中使用mqtt

如何解决我如何发布和订阅图像在python中使用mqtt

我使用百度loT Core。我使用两个设备作为客户端,一个数据库(TSDB)作为服务器。目标功能一个客户端将图像发送到数据库,然后将数据发送给另一个客户端。我从How can I publish a file using Mosquitto in python?获得帮助 但这仍然行不通。

发送图片

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

    HOST = '************'
    PORT = 1883
    client_id = '************'
    username = '***********'
    password = '******************'
    topic = '******'
    
    
    
    # obj = userdata
    # mqttc = client
    def on_connect(mqttc,obj,flags,rc):
        print("rc: " + str(rc))
    
    
    def on_message(mqttc,msg):
        print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload.decode('utf-8')))
    
    
    def on_publish(mqttc,mid):
        print("mid: " + str(mid))
    
    
    def on_subscribe(mqttc,mid,granted_qos):
        print("Subscribed: " + str(mid) + " " + str(granted_qos))
    
    
    def on_log(mqttc,level,string):
        print(string)
    
    
    def on_disconnect(mqttc,rc):
        print("unsuccess connect %s" % rc)

    mqttc = mqtt.Client(client_id)
    mqttc.username_pw_set(username,password) # thanks correction. I found I forget to connect broker.But the question is still 
    mqttc.on_message = on_message
    mqttc.on_connect = on_connect
    mqttc.on_publish = on_publish
    mqttc.on_subscribe = on_subscribe
    mqttc.on_disconnect = on_disconnect
    # Uncomment to enable debug messages
    mqttc.on_log = on_log
    mqttc.connect(HOST,PORT,60)

    image = 'E:/_TempPhoto/0.jpg'
print(type(image))
def imagetoStr(image):
    with open(image,'rb') as f:
        image_byte = base64.b64encode(f.read())
        print(type(image_byte))
    image_str = image_byte.decode('ascii')  # byte to str
    print(type(image_str))
    return image_str

image1 = imagetoStr(image)
data = {
    "engineeringdata": {
        "date": 12,"value": "59.3;98.5","image": image1
    }
}
json_mod = json.dumps(data)
mqttc.publish(topic,json_mod,0)
mqttc.loop_forever()

接收图像

    import paho.mqtt.client as mqtt
    import cv2
    import numpy as np
    import json
    
    HOST = '*********'
    PORT = 1883
    client_id = '************'
    username = '*****************'
    password = '******************'
    topic = '***************'
    
    
    def on_connect(client,userdata,rc):
        print("Connected with result code: " + str(rc))

    def strToImage(str,filename):
       image_str= str.encode('ascii')
       image_byte = base64.b64decode(image_str)
       image_json = open(filename,'wb')
       image_json.write(image_byte)  #将图片存到当前文件的fileimage文件中
       image_json.close()    
    
    def on_message(client,msg):
        print(msg.topic + " " + str(msg.payload)) 
        strToImage(str(msg.payload),'E:/_TempPhoto/restore001.jpg')


    
    client = mqtt.Client(client_id)
    client.username_pw_set(username,password)
    client.on_connect = on_connect
    client.connect(HOST,60) 
    client.subscribe(topic,0)
    client.on_message = on_message
    client.loop_forever()
    # while True:
    #    client.loop(15)
    #    time.sleep(2)

这是我的发送图片的消息和日志。我用“ ......”擦掉图像的部分打印数据。

Sending CONNECT (u1,p1,wr0,wq0,wf0,c1,k60) client_id=b'client_for_test'
<class 'str'>
<class 'bytes'>
<class 'str'>
Sending PUBLISH (d0,q0,r0,m1),'b'$iot/client_for_test/user/fortest'',... (102800 bytes)
mid: 1
Received CONNACK (0,0)
rc: 0
Sending PINGREQ
Received PINGRESP
Sending PINGREQ
Received PINGRESP

这是接收图像的打印消息:(似乎正确)

Connected with result code: 0

我的错误案例是5。而且我没有提供图像。但似乎发送成功。

也许是因为发送图像发送一次并结束,但是接收图像无法正确接收。 我只需要两个客户最简单的代码即可通过mqtt 发送接收图片,请! 帮帮我!

感谢您的帮助。先前的问题已解决。但是这里有新的麻烦。我编辑了新代码,我无法收到图片 请更正我的代码

解决方法

您可以在发布图像之前将图像编码为base64,这样主题的内容将为字符串,例如:

import base64
 
with open("t.png","rb") as imageFile:
    str = base64.b64encode(imageFile.read())
    print str
,

Publisher.py

import paho.mqtt.publish as publish     
f= open("myimage.jpg")
content = f.read()
mybyteArray = bytearray(content)

mqttc.publish(topic,mybyteArray,1)

Receiver.py

def on_message(client,userdata,msg):
  f = open('myReceivedImage.jpg','w')
  f.write(msg.payload)
  f.close()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。