def _get_keyword(self): IQE_KEY = '0e640412a82a4896bfb40bb53429729b' IQE_SECRET = '310970e406d94f9db24b2c527d7ae6d9' cache = Memcached(["localhost:11211"], 60) #file_image = self.request.files[''] hash_value = calc_hash(self.request.body) keyword = cache.get(hash_value) if not cached_response: file_image = '/tmp/%s.jpg' % hash_value open(file_image, 'w').write(self.request.body) api = Api(IQE_KEY, IQE_SECRET) data = api.query(file_image) response = api.update() name = response['data']['results'][0]['qid_data']['labels'] results = [] for res in response['data']['results']: data = { 'product_name': res['qid_data']['labels'], } results.append(data) keyword = " ".join(results) cache.set(hash_value, keyword) return keyword
def _test_query_flow(): """ Tests whether querying works with the same interface. Warning, this will deplete your purchased query count. Also it requires that you set IQE_KEY and IQE_SECRET in your environment """ import time from pyiqe import Api iqe = Api(version="1.2") response, qid = iqe.query('testdata/dole1.jpeg') print("sending in query with qid %s" % qid) assert response == {'data': {'error': 0}}, "Invalid Response while querying: \n%s " % response print("\nwaiting for results ...") response = iqe.update() print(response) print("\nretrieving results manually") time.sleep(2) response = iqe.result(qid) print(response)
class TestApi(unittest.TestCase): def setUp(self): from pyiqe import Api self.api = Api(version="1.2") def test_QueryExtra(self): """ Make sure we pass back extra argument is returned""" import json device_id = "pyiqe.test.%s" % time.time() response, qid = self.api.query('testdata/dole1.jpeg', extra={'a':1}, device_id=device_id) # update method result = self.api.update(device_id=device_id) pprint(result) result = result['data']['results'] pprint(result) assert "extra" in result[0] and result[0]['extra'] == json.dumps({'a':1}), "Extra Argument mismatch %s" % result # result method response = self.api.result(qid) result = response['data'] pprint(result) assert "extra" in result and result['extra'] == json.dumps({'a':1}), "Extra Argument mismatch %s" % result
def match_image_to_turn(image, game_id): """ Match the image to the given image in the games last played image section returns True or False """ api = Api(settings.IQE_KEY, settings.IQE_SECRET) label = utils.create_unique_label() filename = "/tmp/%s" %label file = open(filename, "w") file.write(image.decode('base64')) file.close() response, qid = api.query(filename, device_id=label) print "Object created with response: %s and qid: %s" %(response, qid) game = query_db('select * from game where id=?', [game_id], one=True) expected_label = game['label'] try: # update method result = api.update(device_id=label) data = result['data'] if "results" in data: print data['results'] if isinstance(data['results'], list): actual_labels = [result['qid_data']['labels'] for result in data['results']] result = expected_label in actual_labels else: actual_labels = data['results']['qid_data']['labels'] result = expected_label == actual_labels print "Actual labels: %s" %actual_labels print "Expected labels: %s" %expected_label print "Result for the image match is: %s" %result if result: return result except Exception as ex: print "Match raised ane exception" import traceback traceback.print_exc() # result method response = api.result(qid) data = response['data'] print "Data response: %s" %response if "results" in data: if isinstance(data['results'], list): actual_labels = [result['labels'] for result in data['results']] result = expected_label in actual_labels else: actual_labels = data['results']['labels'] result = expected_label == actual_labels print "Actual labels: %s" %actual_labels print "Expected labels: %s" %expected_label print "Result for the image match is: %s" %result return result else: return False
def setUp(self): from pyiqe import Api self.api = Api(version="1.2") self.prev_upload = P.join(CWD, "uploaded.txt")
class TestTrainingAPI(unittest.TestCase): def setUp(self): from pyiqe import Api self.api = Api(version="1.2") self.prev_upload = P.join(CWD, "uploaded.txt") def tearDown(self): import os rs_delete = self.api.objects.delete(self.obj_id) print rs_delete self.assertTrue(rs_delete['error'] == 0) if P.exists(self.prev_upload): os.remove(self.prev_upload) def test_CreateObjectandDelete(self): import time import os.path as P # parameters imgpath = P.join(P.dirname(P.abspath(__file__)), "testdata/fox.jpeg") name = str(time.time()) # check if we didn't clean up from last test if P.exists(self.prev_upload): print "removing previous upload" prev_oid = open(self.prev_upload, "r").read().strip() self.api.objects.delete(prev_oid) # train the system rs_training = self.api.objects.create( name = name, images = [imgpath], custom_id = "123" ) print "Training API response =", rs_training self.assertTrue(rs_training['error'] == 0, "Training API request failed") # try retrieving it obj_id = rs_training['obj_id'] self.obj_id = obj_id rs_get = self.api.objects.get(obj_id) self.assertTrue(rs_get['object']['obj_id'] == obj_id, "Retrieval Failed") # write to previous upload file print "writing upload file" with open(self.prev_upload, "w") as fh: fh.write(obj_id) time.sleep(200) # try retrieving related_image img_id = rs_get['object']['related_images']['images'][0]['img_id'] print(rs_get) rs_img_get = self.api.images.get(img_id) print rs_img_get self.assertTrue(rs_img_get['image']['related_object']['obj_id'] == obj_id) # wait 10 seconds before querying time.sleep(20) # query the system for the image rs_query, qid = self.api.query(imgpath=imgpath) print "Query API response =", rs_query print "qid =", qid self.assertTrue(rs_query['data']['error'] == 0, "Query API request failed") # retrieve the results for i in range(20): rs_result = self.api.result(qid) print "Result API response =", rs_result assert rs_result['data']['error'] == 0, "Result API request failed" if rs_result['data'].has_key('results'): break time.sleep(2) self.assertTrue(rs_result['data'].has_key('results'), "Result API did not return any result after 20 tries") self.assertTrue(rs_result['data']['results']['labels'] == name, "Result API does not return correct labels") self.assertTrue(rs_result['data']['results']['obj_id'] == rs_training['obj_id'], "Result API does not return correct labels")
def setUp(self): from pyiqe import Api self.api = Api(version="1.2")