def test_search_image_url_and_geo(self): """ search over image url combined with geo """ # upload an image image_id = uuid.uuid4().hex geo = Geo(GeoPoint(40.7129, 74.0058)) img2 = self.app.inputs.create_image_from_url( image_id=image_id, url=sample_inputs.METRO_IMAGE_URL, geo=geo, allow_duplicate_url=True) term1 = clarifai.rest.InputSearchTerm( url=sample_inputs.METRO_IMAGE_URL) geo_box = GeoBox(GeoPoint(40.7028, 74.0009), GeoPoint(40.7328, 74.0359)) term2 = clarifai.rest.InputSearchTerm(geo=Geo(geo_box=geo_box)) query = clarifai.rest.SearchQueryBuilder() query.add_term(term1) query.add_term(term2) search_res = self.app.inputs.search(query) self.assertGreater(len(search_res), 0) match = False for img in search_res: if img.input_id == image_id or img.url == img2.url: match = True break self.assertTrue(match) # delete second test image self.app.inputs.delete(img2.input_id)
def test_search_geo_with_input_output_tag(self): """ search over input and output tag together with geo info """ # upload an image image_id = uuid.uuid4().hex geo = Geo(GeoPoint(40.7129, 74.0058)) img2 = self.app.inputs.create_image_from_url( image_id=image_id, url=sample_inputs.METRO_IMAGE_URL, geo=geo, concepts=["train"], allow_duplicate_url=True) geo_box = GeoBox(GeoPoint(40.7028, 74.0009), GeoPoint(40.7328, 74.0359)) term1 = clarifai.rest.InputSearchTerm(geo=Geo(geo_box=geo_box)) term2 = clarifai.rest.InputSearchTerm(concept="train") term3 = clarifai.rest.OutputSearchTerm(concept="railway") query = clarifai.rest.SearchQueryBuilder() query.add_term(term1) query.add_term(term2) query.add_term(term3) search_res = self.app.inputs.search(query, page=1, per_page=20) # disable this until citus migration fully finishes # self.assertGreater(len(search_res), 0) # match = False # for img in search_res: # if img.input_id == image_id or img.url == img2.url: # match = True # break # self.assertTrue(match) # delete second test image self.app.inputs.delete(img2.input_id)
def test_search_image_url_and_geo(self): """ search over image url combined with geo """ # upload an image image_id = uuid.uuid4().hex geo = Geo(GeoPoint(40.7129, 74.0058)) img2 = self.app.inputs.create_image_from_url(image_id=image_id, url=urls[0], geo=geo, allow_duplicate_url=True) term1 = clarifai.rest.InputSearchTerm(url=urls[0]) geo_box = GeoBox(GeoPoint(40.7028, 74.0009), GeoPoint(40.7328, 74.0359)) term2 = clarifai.rest.InputSearchTerm(geo=Geo(geo_box=geo_box)) query = clarifai.rest.SearchQueryBuilder() query.add_term(term1) query.add_term(term2) search_res = self.app.inputs.search(query) # disable this until citus migration fully finishes # self.assertGreater(len(search_res), 0) # match = False # for img in search_res: # if img.input_id == image_id or img.url == img2.url: # match = True # break # self.assertTrue(match) # delete second test image self.app.inputs.delete(img2.input_id)
def test_geo_box(self): """ test get one concept by id """ geo_p1 = GeoPoint(0, 0) geo_p2 = GeoPoint(10, 20) geo_p3 = GeoPoint(-30.3, 10.2234) for p1, p2 in permutations([geo_p1, geo_p2, geo_p3], 2): geo_b = GeoBox(p1, p2) with self.assertRaises(TypeError) as e: gl = GeoBox() self.assertIn(str(e), 'arguments')
def test_post_image_with_geo_and_metadata(self): """ add image with geo info, retrieve it and compare the meta data """ image_id = uuid.uuid4().hex geo = Geo(GeoPoint(-30, 40)) meta = {'myid': image_id, 'key_id': 'test_meta'} img = self.app.inputs.create_image_from_url( url=sample_inputs.METRO_IMAGE_URL, geo=geo, metadata=meta, image_id=image_id, allow_duplicate_url=True) res = self.app.inputs.get(image_id) self.assertTrue(isinstance(res, ClarifaiImage)) self.assertEqual(image_id, res.input_id) geo_res = res.geo self.assertTrue(isinstance(geo_res, Geo)) self.assertAlmostEqual(geo_res.geo_point.longitude, -30, delta=0.0001) self.assertAlmostEqual(geo_res.geo_point.latitude, 40, delta=0.0001) self.assertEqual(meta, res.metadata) self.app.inputs.delete(image_id)
def test_search_geo_point(self): """ search input geo point data """ # upload image image_id = uuid.uuid4().hex geo = Geo(GeoPoint(40.7128, 74.0059)) img1 = self.app.inputs.create_image_from_url( image_id=image_id, url=sample_inputs.METRO_IMAGE_URL, geo=geo, allow_duplicate_url=True) self.app.wait_for_specific_input_uploads_to_finish(ids=[image_id], max_wait=30) # simple geo search search_res = self.app.inputs.search_by_geo(GeoPoint(40.7128, 74.0059)) # disable this until citus migration fully finishes # self.assertGreater(len(search_res), 0) search_res = self.app.inputs.search_by_geo(GeoPoint(40.7128, 74.0059), GeoLimit("mile", 10)) # disable this until citus migration fully finishes # self.assertGreater(len(search_res), 0) # delete first test image self.app.inputs.delete(img1.input_id) # upload 2nd image image_id = uuid.uuid4().hex geo = Geo(GeoPoint(40.7129, 74.0058)) img2 = self.app.inputs.create_image_from_url( image_id=image_id, url=sample_inputs.METRO_IMAGE_URL, geo=geo, allow_duplicate_url=True) self.app.wait_for_specific_input_uploads_to_finish(ids=[image_id], max_wait=30) search_res = self.app.inputs.search_by_geo(GeoPoint(40.7128, 74.0059), GeoLimit("kilometer", 9)) # disable this until citus migration fully finishes # self.assertGreater(len(search_res), 0) # delete second test image self.app.inputs.delete(img2.input_id)
def test_search_geo_box(self): """ search input geo point data """ # upload an image image_id = uuid.uuid4().hex geo = Geo(GeoPoint(40.7129, 74.0058)) img2 = self.app.inputs.create_image_from_url( image_id=image_id, url=sample_inputs.METRO_IMAGE_URL, geo=geo, allow_duplicate_url=True) self.app.wait_for_specific_input_uploads_to_finish(ids=[image_id], max_wait=30) geo_box = GeoBox(GeoPoint(40.7028, 74.0009), GeoPoint(40.7328, 74.0359)) search_res = self.app.inputs.search_by_geo(geo_box=geo_box) # disable this until citus migration fully finishes # self.assertGreater(len(search_res), 0) # delete second test image self.app.inputs.delete(img2.input_id)
def test_search_geo_box(self): """ search input geo point data """ # upload an image image_id = uuid.uuid4().hex geo = Geo(GeoPoint(40.7129, 74.0058)) img2 = self.app.inputs.create_image_from_url(image_id=image_id, url=urls[0], geo=geo, allow_duplicate_url=True) geo_box = GeoBox(GeoPoint(40.7028, 74.0009), GeoPoint(40.7328, 74.0359)) search_res = self.app.inputs.search_by_geo(geo_box=geo_box) # disable this until citus migration fully finishes # self.assertGreater(len(search_res), 0) # delete second test image self.app.inputs.delete(img2.input_id)
def test_geo_points(self): """ test get one concept by id """ geo_p = GeoPoint(0, 0) geo_p = GeoPoint(10, 20) geo_p = GeoPoint(-30.3, 10.2234) geo_p = GeoPoint("-30.3", "10.2234") geo_p = GeoPoint("30.3", "10") geo_p = GeoPoint("0", "0") with self.assertRaises(TypeError) as e: gl = GeoPoint() self.assertIn(str(e), 'arguments') with self.assertRaises(ValueError) as e: gl = GeoPoint("b", "c") with self.assertRaises(ValueError) as e: gl = GeoPoint("", "0.33")
def test_geo(self): # make a geo point for input Geo(geo_point=GeoPoint(10, 20)) # make a geo point and geo limit for search Geo(geo_point=GeoPoint(10, 20), geo_limit=GeoLimit("mile", 33)) # make a geo box for search p1 = GeoPoint(0, 0) p2 = GeoPoint(30, -20.22) Geo(geo_box=GeoBox(p1, p2)) # make a geo point only Geo(geo_point=p1) # make an invalid Geo with self.assertRaises(Exception): Geo(geo_point=p1, geo_box=GeoBox(p1, p2), geo_limit=GeoLimit()) # make an invalid Geo with self.assertRaises(Exception): Geo()
def test_post_image_with_geo(self): """ add image with geo info, retrieve it and compare the meta data """ image_id = uuid.uuid4().hex geo = Geo(GeoPoint(-30, 40)) img = self.app.inputs.create_image_from_url( url=urls[0], geo=geo, image_id=image_id, allow_duplicate_url=True) res = self.app.inputs.get(image_id) self.assertTrue(isinstance(res, ClarifaiImage)) self.assertEqual(image_id, res.input_id) geo_res = res.geo self.assertAlmostEqual(geo_res.geo_point.longitude, -30, delta=0.0001) self.assertAlmostEqual(geo_res.geo_point.latitude, 40, delta=0.0001) self.app.inputs.delete(image_id)
def test_post_image_with_geo(self): """ add image with geo info, retrieve it and compare the meta data """ image_id = uuid.uuid4().hex geo = Geo(GeoPoint(-30, 40)) self.app.inputs.create_image_from_url( url=sample_inputs.METRO_IMAGE_URL, geo=geo, image_id=image_id, allow_duplicate_url=True) self.app.wait_for_specific_input_uploads_to_finish(ids=[image_id]) res = self.app.inputs.get(image_id) self.assertTrue(isinstance(res, ClarifaiImage)) self.assertEqual(image_id, res.input_id) geo_res = res.geo self.assertAlmostEqual(geo_res.geo_point.longitude, -30, delta=0.0001) self.assertAlmostEqual(geo_res.geo_point.latitude, 40, delta=0.0001) self.app.inputs.delete(image_id)
def test_search_inputs_by_geo_location( mock_http_client): # type: (mock.Mock) -> None mock_execute_request = mock_request(mock_http_client, json_responses=[ """ { "status": { "code": 10000, "description": "Ok" }, "hits": [ { "score": 0.5, "input": { "id": "@inputID", "created_at": "2016-11-22T17:06:02Z", "data": { "image": { "url": "@inputURL" } }, "status": { "code": 30000, "description": "Download complete" } } } ] } """, """ { "status": { "code": 10000, "description": "Ok" }, "hits": [] } """ ]) app = ClarifaiApp() qb = SearchQueryBuilder() qb.add_term( InputSearchTerm(geo=Geo(geo_point=GeoPoint(longitude=1.5, latitude=-1), geo_limit=GeoLimit(limit_type='kilometer', limit_range=1)))) inputs = app.inputs.search(qb) assert inputs[0].input_id == '@inputID' assert inputs[0].score == 0.5 assert_request( mock_execute_request, 'POST', '/v2/searches', """ { "query": { "ands": [ { "input": { "data": { "geo": { "geo_point": { "longitude": 1.5, "latitude": -1 }, "geo_limit": { "type": "withinKilometers", "value": 1 } } } } } ] }, "pagination": { "per_page": 20, "page": 1 } } """)
def test_bulk_create_image(mock_http_client): # type: (mock.Mock) -> None mock_execute_request = mock_request( mock_http_client, """ { "status": { "code": 10000, "description": "Ok" }, "inputs": [ { "id": "@inputID1", "data": { "image": { "url": "https://some.image.url1" }, "geo": { "geo_point": { "longitude": 55, "latitude": 66 } } }, "created_at": "2019-01-17T12:43:04.895006174Z", "modified_at": "2019-01-17T12:43:04.895006174Z", "status": { "code": 30001, "description": "Download pending" } }, { "id": "@inputID2", "data": { "image": { "url": "https://some.image.url2" } }, "created_at": "2019-01-17T12:43:04.895006174Z", "modified_at": "2019-01-17T12:43:04.895006174Z", "status": { "code": 30001, "description": "Download pending" } } ] } """) app = ClarifaiApp() images = app.inputs.bulk_create_images([ Image( url='https://some.image.url1', image_id='@inputID1', allow_dup_url=True, geo=Geo(geo_point=GeoPoint(55, 66))), Image(url='https://some.image.url2', image_id='@inputID2', allow_dup_url=True), ]) assert images[0].input_id == '@inputID1' assert images[0].url == 'https://some.image.url1' assert images[0].geo.geo_point.longitude == 55 assert images[0].geo.geo_point.latitude == 66 assert images[0].status.code == 30001 assert images[1].input_id == '@inputID2' assert images[1].url == 'https://some.image.url2' assert not images[1].geo assert images[1].status.code == 30001 assert_request( mock_execute_request, 'POST', '/v2/inputs', """ { "inputs": [ { "id": "@inputID1", "data": { "image": { "url": "https://some.image.url1", "allow_duplicate_url": true }, "geo": { "geo_point": { "longitude": 55, "latitude": 66 } } } }, { "id": "@inputID2", "data": { "image": { "url": "https://some.image.url2", "allow_duplicate_url": true } } } ] } """)