Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From JPG to b64encode to cv2.imread()

For a program I am writing, I am transferring an image from one computer - using base64.b64encode(f.read(image)) - and trying to read it in the receiving script without saving it to hard drive (in an effort to minimize process time). I'm having a hard time figuring out how to read the image into OpenCV without saving it locally.

Here is what my code for sending the image looks like:

f = open(image.jpg)
sendthis = f.read()
f.close()
databeingsent = base64.b64encode(sendthis)
client.publish('/image',databeingsent,0) 
# this is an MQTT publish, details for SO shouldn't be relevant

Meanwhile, here is the code receiving it. (This is in an on_message function, since I'm using MQTT for the transfer.)

def on_message(client, userdata, msg): # msg.payload is incoming data
    img = base64.b64decode(msg.payload)
    source = cv2.imread(img)
    cv2.imshow("image", source)

After the message decodes, I have the error: "TypeError: Your input type is not a numpy array".

I've done some searching, and I can't seem to find a relevant solution - some exist regarding converting from text files to numpy using b64, but none really relate to using an image and immediately reading that decoded data into OpenCV without the intermediary step of saving it to the harddrive (using the inverse process used to read the file in the "send" script).

I'm still pretty new to Python and OpenCV, so if there's a better encoding method to send the image - whatever solves the problem. How the image is sent is irrelevant, so long as I can read it in on the receiving end without saving it as a .jpg to disk.

Thanks!

like image 371
godfreap Avatar asked Nov 04 '15 12:11

godfreap


People also ask

What is cv2 Imread ()?

cv2. imread() method loads an image from the specified file. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format) then this method returns an empty matrix.

Can cv2 read JPEG?

So, OpenCV can always read JPEGs, PNGs, and TIFFs.

Why is cv2 Imread not working?

If you are receiving a NoneType error and your code is calling cv2. imread , then the likely cause of the error is an invalid file path supplied to cv2.

How do I read Imread images?

A = imread( filename ) reads the image from the file specified by filename , inferring the format of the file from its contents. If filename is a multi-image file, then imread reads the first image in the file.


1 Answers

You can get a numpy array from you decoded data using:

import numpy as np
...
img = base64.b64decode(msg.payload)
npimg = np.fromstring(img, dtype=np.uint8)

Then you need imdecode to read the image from a buffer in memory. imread is meant to load an image from a file.

So:

import numpy as np
...
def on_message(client, userdata, msg): # msg.payload is incoming data
    img = base64.b64decode(msg.payload); 
    npimg = np.fromstring(img, dtype=np.uint8); 
    source = cv2.imdecode(npimg, 1)
like image 161
Miki Avatar answered Oct 21 '22 02:10

Miki