-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add PIL and complete rgb_histogram calculation... also, modularize fe…
…ature extraction so that individual features can be requested
- Loading branch information
Matt Caldwell
committed
Apr 14, 2012
1 parent
ce8240e
commit 46fa7cf
Showing
9 changed files
with
31 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.