forked from scottpurdy/nupic.vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_readers.py
60 lines (54 loc) · 2.13 KB
/
dataset_readers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2015, Numenta, Inc. Unless you have purchased from
# Numenta, Inc. a separate commercial license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------
"""
This module provides routines that read in image recognition data sets
consisting of images and their associated ground truth. Image recognition data
sets come in different formats so there are routines for reading each format
that has been used with nupic.vision.
"""
from PIL import Image
from xml.dom import minidom
DEBUG = 0
def getImagesAndTags(filename):
"""
This routine reads the XML files that contain the paths to the images and the
tags which indicate what is in the image (i.e. "ground truth").
"""
print
print "Reading data set: ", filename
print
xmldoc = minidom.parse(filename)
# Find the path to the XML file so it can be used to find the image files.
directoryPath = filename.replace(filename.split("/")[-1],"")
# Read the image list from the XML file and populate images and tags.
imageList = xmldoc.getElementsByTagName('image')
images = []
tags = []
for image in imageList:
tags.append(image.attributes['tag'].value)
filename = image.attributes['file'].value
fp = open(directoryPath + filename, 'rb')
im = Image.open(fp)
im.load()
images.append(im)
fp.close()
#imagePatches[-1].show()
return images, tags