星火使用PySpark读取图像 [英] Spark using PySpark read images

查看:480
本文介绍了星火使用PySpark读取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有很多的图像(下百万),我需要做的分类。我使用Spark和管理中的所有图像(文件名,内容1)的格式阅读,(文件名,内容2)... 成一个大RDD。

Hi there I have a lot of images (lower millions) that I need to do classification on. I am using Spark and managed to read in all the images in the format of (filename1, content1), (filename2, content2) ... into a big RDD.

images = sc.wholeTextFiles("hdfs:///user/myuser/images/image/00*")  

不过,我真是糊涂做什么用的UNI code再$ P $图像psentation做。

However, I got really confused what to do with the unicode representation of the image.

下面是一个图像/文件的例子:

Here is an example of one image/file:

(u'hdfs://NameService/user/myuser/images/image/00product.jpg', u'\ufffd\ufffd\ufffd\ufffd\x00\x10JFIF\x00\x01\x01\x01\x00`\x00`\x00\x00\ufffd\ufffd\x01\x1eExif\x00\x00II*\x00\x08\x00\x00\x00\x08\x00\x12\x01\x03\x00\x01\x00\x00\x00\x01\x00\x00\x00\x1a\x01\x05\x00\x01\x00\x00\x00n\x00\x00\x00\x1b\x01\x05\x00\x01\x00\x00\x00v\x00\x00\x00(\x01\x03\x00\x01\x00\x00\x00\x02\x00\x00\x001\x01\x02\x00\x0b\x00\x00\x00~\x00\x00\x002\x01\x02\x00\x14\x00\x00\x00\ufffd\x00\x00\x00\x13\x02\x03\x00\x01\x00\x00\x00\x01\x00\x00\x00i\ufffd\x04\x00\x01\x00\x00\x00\ufffd\x00\x00\x00\x00\x00\x00\x00`\x00\x00\x00\x01\x00\x00\x00`\x00\x00\x00\x01\x00\x00\x00GIMP 2.8.2\x00\x002013:07:29 10:41:35\x00\x07\x00\x00\ufffd\x07\x00\x04\x00\x00\x000220\ufffd\ufffd\x02\x00\x04\x00\x00\x00407\x00\x00\ufffd\x07\x00\x04\x00\x00\x000100\x01\ufffd\x03\x00\x01\x00\x00\x00\ufffd\ufffd\x00\x00\x02\ufffd\x04\x00\x01\x00\x00\x00\x04\x04\x00\x00\x03\ufffd\x04\x00\x01\x00\x00\x00X\x01\x00\x00\x05\ufffd\x04\x00\x01\x00\x00\x00\ufffd\x00\x00\x00\x00\x00\x00\x00\x02\x00\x01\x00\x02\x00\x04\x00\x00\x00R98\x00\x02\x00\x07\x00\x04\x00\x00\x000100\x00\x00\x00\x00\ufffd\ufffd\x04_http://ns.adobe.com/xap/1.0/\x00<?xpacket begin=\'\ufeff\' id=\'W5M0MpCehiHzreSzNTczkc9d\'?>\n<x:xmpmeta xmlns:x=\'adobe:ns:meta/\'>\n<rdf:RDF xmlns:rdf=\'http://www.w3.org/1999/02/22-rdf-syntax-ns#\'>\n\n <rdf:Description xmlns:exif=\'http://ns.adobe.com/exif/1.0/\'>\n  <exif:Orientation>Top-left</exif:Orientation>\n  <exif:XResolution>96</exif:XResolution>\n  <exif:YResolution>96</exif:YResolution>\n  <exif:ResolutionUnit>Inch</exif:ResolutionUnit>\n  <exif:Software>ACD Systems Digital Imaging</exif:Software>\n  <exif:DateTime>2013:07:29 10:37:00</exif:DateTime>\n  <exif:YCbCrPositioning>Centered</exif:YCbCrPositioning>\n  <exif:ExifVersion>Exif Version 2.2</exif:ExifVersion>\n  <exif:SubsecTime>407</exif:SubsecTime>\n  <exif:FlashPixVersion>FlashPix Version 1.0</exif:FlashPixVersion>\n  <exif:ColorSpace>Uncalibrated</exif:ColorSpace>\n  

更仔细地观察,其实有一些字符看起来像

Looking closer, there are actually some characters look like the metadata like

...
<x:xmpmeta xmlns:x=\'adobe:ns:meta/\'>\n<rdf:RDF xmlns:rdf=\'http://www.w3.org/1999/02/22-rdf-syntax-ns#\'>\n\n 
<rdf:Description xmlns:exif=\'http://ns.adobe.com/exif/1.0/\'>\n  
<exif:Orientation>Top-left</exif:Orientation>\n  
<exif:XResolution>96</exif:XResolution>\n  
<exif:YResolution>96</exif:YResolution>\n  
...

我的previous经验是使用包装SciPy的和像imread相关的功能...并输入通常是一个文件名。现在,我真的迷路了什么呢那些单code均值和我能做些什么把它改造成我熟悉的格式。

My previous experience was using the package scipy and related functions like 'imread' ... and the input is usually a filename. Now I really got lost what does those unicode mean and what I can do to transform it into a format that I am familiar with.

可以跟我的人分享我如何在这些单code读入SciPy的图像(ndarray)?

Can anyone share with me how can I read in those unicode into a scipy image (ndarray)?

推荐答案

您的数据看起来像一个真正的图像文件的原始字节(JPG?)。与数据的问题是,它应该是字节,而不是单向code。你必须弄清楚如何从单向code转换为字节。有虫满编码陷阱,你必须处理的一个整体,但你可以用很幸运 img.en code('ISO-8859-1')。我不知道,我不会在我的回答处理。

Your data looks like the raw bytes from a real image file (JPG?). The problem with your data is that it should be bytes, not unicode. You have to figure out how to convert from unicode to bytes. There is a whole can of worms full of encoding traps you have to deal with, but you may be lucky using img.encode('iso-8859-1'). I don't know and I will not deal with that in my answer.

有关PNG图像的原始数据是这样的:

The raw data for a PNG image looks like this:

rawdata = '\x89PNG\r\n\x1a\n\x00\x00...\x00\x00IEND\xaeB`\x82'

一旦你以字节为单位拥有它,你可以从原始数据中创建一个PIL形象,读它作为nparray:

Once you have it in bytes, you can create a PIL image from the raw data, and read it as a nparray:

>>> from StringIO import StringIO
>>> from PIL import Image
>>> import numpy as np
>>> np.asarray(Image.open(StringIO(rawdata)))

array([[[255, 255, 255,   0],
    [255, 255, 255,   0],
    [255, 255, 255,   0],
    ...,
    [255, 255, 255,   0],
    [255, 255, 255,   0],
    [255, 255, 255,   0]]], dtype=uint8)

所有你需要使它在星火的工作是 SparkContext.binaryFiles

>>> images = sc.binaryFiles("path/to/images/")
>>> image_to_array = lambda rawdata: np.asarray(Image.open(StringIO(rawdata)))
>>> images.values().map(image_to_array)

这篇关于星火使用PySpark读取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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