class GoogleImageSearchTask(object): def __init__(self, query): self.folder = "imageStore" self.query = query self.uber = UberAPIWrapper() self.task = self.uber.create(self.query) self.trainCount = 4 def start(self): BASE_URL = 'https://ajax.googleapis.com/ajax/services/search/images?'\ 'v=1.0&q=' + self.query + '&start=%d' BASE_PATH = os.path.join(self.folder, self.query) if not os.path.exists(BASE_PATH): os.makedirs(BASE_PATH) start = 0 # Google's start query string parameter for pagination. incr = 0 trained = 0 while start < 60: # Google will only return a max of 56 results. r = requests.get(BASE_URL % start) for image_info in json.loads(r.text)['responseData']['results']: url = image_info['unescapedUrl'] try: image_r = requests.get(url) except ConnectionError, e: print 'could not download %s' % url continue # Remove file-system path characters from name. title = image_info['titleNoFormatting'].replace('/', '').replace('\\', '') filePath = os.path.join(BASE_PATH, '%d.jpg') % incr file = open(filePath, 'w') try: incr +=1 im = Image.open(StringIO(image_r.content)) im.save(file, 'JPEG') if trained < self.trainCount: self.uber.train(self.task["id"], filePath, self.query) trained += 1 else: self.uber.match(self.task["id"], filePath) # buf = StringIO() # im.save(buf, format='JPEG') # jpeg = buf.getvalue() # self.addFrame(jpeg) except IOError, e: # Throw away some gifs...blegh. print 'could not save %s' % url continue finally:
import os import sys path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../python')) if not path in sys.path: sys.path.insert(1, path) del path from UberAPIWrapper import UberAPIWrapper import pycurl import StringIO import json if __name__ == '__main__': uber = UberAPIWrapper() #Creating a task task = uber.create("Coca cola") #Training the api uber.train(task["id"], "train1.jpg", "Coca cola 1") #Matching uber.match(task["id"], "match1.jpg")