Skip to content

Commit

Permalink
add PIL and complete rgb_histogram calculation... also, modularize fe…
Browse files Browse the repository at this point in the history
…ature extraction so that individual features can be requested
  • Loading branch information
Matt Caldwell committed Apr 14, 2012
1 parent ce8240e commit 46fa7cf
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 17 deletions.
45 changes: 29 additions & 16 deletions react/features.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
import numpy as np, urllib2
from django.conf import settings
#from scipy import ndimage
import logging, numpy as np, sys, urllib2
from django.core.cache import cache
from PIL import Image

try:
from scipy import ndimage
except:
from scipy_local import ndimage

try:
from cStringIO import StringIO
except:
from StringIO import StringIO

# from http://stackoverflow.com/questions/6464353/np-array-from-cstringio-object-and-avoiding-copies
def chariter(filelike):
while True:
octet = filelike.read(1)
if octet:
yield ord(octet)
else:
return
logger = logging.getLogger('react')

def extract_features(cache_key, url):

def rgb_histogram(im):
r = np.asarray(im.convert('RGB', (1,0,0,0, 1,0,0,0, 1,0,0,0) ))
g = np.asarray(im.convert('RGB', (0,1,0,0, 0,1,0,0, 0,1,0,0) ))
b = np.asarray(im.convert('RGB', (0,0,1,0, 0,0,1,0, 0,0,1,0) ))
hr, hr_bins = np.histogram(r, bins=256, density=True)
hg, hg_bins = np.histogram(g, bins=256, density=True)
hb, hb_bins = np.histogram(b, bins=256, density=True)
h_rgb = np.array([hr, hg, hb]).ravel()
return h_rgb

def extract_features(cache_key, url, feature_list=['rgb_histogram']):
if url:
print url
logger.debug('extracting features for document at: %s' % url)
response = urllib2.urlopen(url)
bits = np.fromstring(StringIO(response.read()).getvalue(), dtype=np.uint8)
im = Image.open(StringIO(response.read()))
feature_dict = cache.get(cache_key, { })

for feature in feature_list:
feature_dict[feature] = getattr(sys.modules[__name__], feature)(im)
cache.set(cache_key, feature_dict)
print feature_dict

if settings.DEBUG:
pass
2 changes: 1 addition & 1 deletion react/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
div#searchBox input {
height: 40px;
margin: 28px 0;
font-size: 2em;
font-size: 1.5em;
}

.docDisplay {
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ django-tastypie==0.9.11
flickrapi==1.4.2
matplotlib==1.1.0
numpy==1.6.1
PIL==1.1.7
psycopg2==2.4.5
Binary file removed scipy/misc/__init__.pyc
Binary file not shown.
Binary file removed scipy/misc/common.pyc
Binary file not shown.
Binary file removed scipy/misc/doccer.pyc
Binary file not shown.
Binary file removed scipy/misc/pilutil.pyc
Binary file not shown.
Binary file removed scipy/misc/setup.pyc
Binary file not shown.
Binary file removed scipy/misc/setupscons.pyc
Binary file not shown.

0 comments on commit 46fa7cf

Please sign in to comment.