def put_features(feature_str, hashes=None, replace=False): feature = eval(feature_str, {"imfeat": imfeat}) print ("Feature: %s (%s)" % (feature_str, feature)) # Compute feature on all available images by default if hashes is None: hashes = cass.get_image_hashes() # Optionally try not to replace existing features if replace: print "Replacing all existing features for %s" % feature_str else: old_hashes = cass.get_feature_hashes(feature_str) # Get an estimate of the number of images by counting # FIXME This requires cass to load the whole row, twice if 1: print ("Computing feature for %d images" % len(list(cass.get_feature_hashes(feature_str)))) success_count = 0 start_time = time.time() _hashes = hashes if replace else cass.sorted_iter_diff(hashes, old_hashes) for md5hash in _hashes: data = cass.get_imagedata(md5hash) import StringIO s = StringIO.StringIO(data) try: im = Image.open(s) im.load() # Guard for small images that break GIST if im.size[0] < 10 or im.size[1] < 10 or im.size[0] > 1000 or im.size[1] > 1000: print ( "Skipping small image (%d, %d) because of \ GIST segfault" % im.size ) continue except IOError: print "couldn't load image: %s" % md5hash continue # FIXME this seems to be necessary for many features # e.g. imfeat.Moments and imfeat.GIST() im = im.convert("RGB") # Only for catching segfaults print ("hash: ", md5hash) # Compute the feature value = imfeat.compute(feature, im) ret = cass.put_feature_value(feature_str, md5hash, value) print ("Put feature_value([%s], [%s]): %d" % (feature_str, md5hash, ret)) success_count += 1 stop_time = time.time() print ("Finished %d features in %.2f seconds" % (success_count, stop_time - start_time))
def GET(self, md5hash): cType = {"PNG": "images/png", "JPEG": "image/jpeg", "GIF": "image/gif", "ICO": "image/x-icon"} try: data = cass.get_imagedata(md5hash) except KeyboardInterrupt: raise web.notfound() try: s = StringIO.StringIO(data) im = Image.open(s) except IOError: raise web.internalerror("Couldn' open image") web.header("Content-Type", cType[im.format]) # Set the Header return data
def GET(self, md5hash): cType = {"PNG": "images/png", "JPEG": "image/jpeg", "GIF": "image/gif", "ICO": "image/x-icon"} try: data = cass.get_imagedata(md5hash) except KeyboardInterrupt: raise web.notfound() try: s = StringIO.StringIO(data) im = Image.open(s) except IOError: raise web.internalerror("Couldn't open image %s" % md5hash) web.header("Content-Type", cType["JPEG"]) # Set the Header print im im.thumbnail((100, 50)) fp = StringIO.StringIO() im.save(fp, "JPEG") fp.seek(0) return fp.read()