Python OpenCV图像到字节字符串以进行JSON传输 [英] Python OpenCV Image to byte string for json transfer

查看:212
本文介绍了Python OpenCV图像到字节字符串以进行JSON传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 python3 numpy,scipy和opencv 结合使用.

我正在尝试将通过OpenCV读取的图像和连接的相机接口转换为二进制字符串,以通过某种网络连接将其发送到json对象中.

I'm trying to convert a image read through OpenCV and connected camera interface into a binary string, to send it within a json object through some network connection.

我尝试将数组编码为jpg并解码UTF-16字符串,但没有得到可用的结果.例如,使用

I have tried enconding the array as jpg and the decode the UTF-16 string, but I get no usable results. as example, with

img = get_image()
converted = cv2.imencode('.jpg', img)[1].tostring()
print(converted)

我得到一个字节字符串作为结果:

I get a byte-string as result:

b'\ xff \ xd8 \ xff \ xe0 \ x00 \ x10JFIF \ x00 \ x01 \ x01 \ x00 \ x00 \ x00 \ x01 \ x00 \ x01 \ x00 \ x00 \ xff \ xdb \ x00C \ x00 \ x02 \ x01 \ x01 \ x01 \ x01 \ x01 \ x02 \ x01 ....

b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x02\x01\x01\x01\x01\x01\x02\x01....

但是该数据不能用作json对象的内容,因为它包含无效字符.有什么办法可以显示此字符串后面的实际字节? 我相信\ xff表示字节值FF,所以我需要像FFD8FFE0 ...这样的String来代替\ xff \ xd8 \ xff \ xe0.我在做什么错了?

But this data cannot be used as content of a json object, because it contains invalid characters. Is there a way I can display the real bytes behind this string? I believe that \xff represents byte value FF, so I need as String like FFD8FFE0... and so on, instead of \xff\xd8\xff\xe0. What am I doing wrong?

在上面的代码之后,我尝试将其编码为UTF-8和UTF16,但是在此方面出现了一些错误:

I tried to encode it as UTF-8 and UTF16 after the code above, but I get several errors on that:

utf_string = converted.decode('utf-16-le')

UnicodeDecodeError:'utf-16-le'编解码器无法解码位置0-1的字节:非法的UTF-16替代

UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 0-1: illegal UTF-16 surrogate

text = strrrrrr.decode('utf-8')

UnicodeDecodeError:'utf-8'编解码器无法解码位置0:无效的起始字节中的字节0xff

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

我想不出办法解决这个问题.

I can't figure out a way to get this right.

我还尝试将其转换为base64编码的字符串,如 http://www.programcreek.com/2013/09/convert-image-to-string-in-python/ 但这也不起作用. (此解决方案不是首选,因为它需要将映像临时写入磁盘,这与我所需要的不完全相同.最好,映像仅应保存在内存中,而不要保存在磁盘上.)

I also tried to convert it into a base64 encoded string, like explained in http://www.programcreek.com/2013/09/convert-image-to-string-in-python/ But that doesn't work either. ( This solution is not preferred, as it requires the image being written temporarly to disk, which is not exactly what I need. Preferrably the image should only be hold in memory, never on disk.)

该解决方案应该包含一种将图像编码为json-conform字符串的方法,以及将其解码回numpy-array的方法,以便可以与cv2.imshow()再次使用.

The solution should contain a way to encode the image as json-conform string and also a way to decode it back to numpy-array, so it can be used again with cv2.imshow().

感谢您的帮助.

推荐答案

您不需要将缓冲区保存到文件中.以下脚本从网络摄像头捕获图像,将其编码为JPG图像,然后将该数据转换为可打印的base64编码,该编码可与JSON一起使用:

You do not need to save the buffer to a file. The following script captures an image from a webcam, encodes it as a JPG image, and then converts that data into a printable base64 encoding which can be used with your JSON:

import cv2
import base64

cap = cv2.VideoCapture(0)
retval, image = cap.read()
retval, buffer = cv2.imencode('.jpg', image)
jpg_as_text = base64.b64encode(buffer)
print(jpg_as_text)
cap.release()

给您一些开始,例如:

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCg


这可以扩展为显示如何将其转换回二进制文件,然后将数据写入测试文件以显示转换成功:


This could be extended to show how to convert it back to binary and then write the data to a test file to show that the conversion was successful:

import cv2
import base64

cap = cv2.VideoCapture(0)
retval, image = cap.read()
cap.release()

# Convert captured image to JPG
retval, buffer = cv2.imencode('.jpg', image)

# Convert to base64 encoding and show start of data
jpg_as_text = base64.b64encode(buffer)
print(jpg_as_text[:80])

# Convert back to binary
jpg_original = base64.b64decode(jpg_as_text)

# Write to a file to show conversion worked
with open('test.jpg', 'wb') as f_output:
    f_output.write(jpg_original)

这篇关于Python OpenCV图像到字节字符串以进行JSON传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆