I'm using the Face cognitive service from Microsoft and my workflow has the image as a numpy array
MS allow images to be passed in as a url or as data in the header
If data is passed in the header, it can be created with a binary read as follows:
# cropped is the image as a numpy array
# the three attempts below do not work
# as microsoft does not recognise the result
# as an image
image_data = cropped.tobytes()
image_data = cropped.tobytes("F")
image_data = cropped.tobytes("C")
# the following method does work but seems
# a bit ridiculous
cv2.imwrite("temp.png", cropped)
with open(path_to_image, 'rb') as f:
image_data = f.read()
I can get my numpy array in the correct format by saving to disk with opencv imwrite and then reading it back in as above, but that doesn't seem like a sensible thing to do.
I tried using the numpy function tobytes("F") and tobytes("C") but MS doesn't recognise the result as a valid image
How can I use numpy to turn my image array into the same format as if I'd read the image from disk?
Standard image formats consist of a header with meta data about the image and image data encoded to match the specification of that format. When you call tobytes
on an array, numpy simply flattens the image data and encodes it as uncompressed bytes. The software you're using can't use this data because there's no header describing how the image is encoded. Is it compress or uncompressed? Is the image BW or color? Are the pixels encoded using RGB or HUV?
It sounds like your best option is to write your image data in a standard image format before passing it to Microsoft. You can avoid using a file by using BytesIO but the basic idea is what you already have.
from io import BytesIO
import PIL
image = PIL.Image.fromarray(cropped, mode="RGB")
with BytesIO() as temp_buffer:
image.save(temp_buffer, format='png')
image_data = temp_buffer.getvalue()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.