def test_post_bulk_images(self): """ post bulk post images """ img1 = ClarifaiImage( url=sample_inputs.METRO_IMAGE_URL, concepts=['train_custom_images', 'railway_custom_images'], allow_dup_url=True) img2 = ClarifaiImage( url=sample_inputs.WEDDING_IMAGE_URL, concepts=['wedding_custon_images'], not_concepts=['food_custom_images'], allow_dup_url=True) ret_imgs = self.app.inputs.bulk_create_images([img1, img2]) self.app.wait_for_specific_input_uploads_to_finish( ids=[ret_imgs[0].input_id, ret_imgs[1].input_id]) self.assertEqual(len(list(ret_imgs)), 2) # sleep here to ensure inputs are all properly added before deleting them time.sleep(2) for img in ret_imgs: self.assertTrue(isinstance(img, ClarifaiImage)) try: self.app.inputs.delete(img.input_id) except ApiError: pass
def test_partial_errors(self): """ upload a few failed urls and fetch by pages make sure the partial error is coming and well handled """ img1 = ClarifaiImage(url='https://samples.clarifai.com/dog2.jpeg', allow_dup_url=True) img2 = ClarifaiImage(url='https://samples.clarifai.com/dog2_bad.jpeg', allow_dup_url=True) imgs = self.app.inputs.bulk_create_images([img1] * 5 + [img2] * 2 + [img1] * 5) bad_ids = [] for img in imgs: if img.url == 'https://samples.clarifai.com/dog2_bad.jpeg': bad_ids.append(img.input_id) self.assertEqual(len(bad_ids), 2) self.app.wait_until_inputs_upload_finish(max_wait=30) # Mixed status exception will be raised found_error = False found_error_id = False for img_one in self.app.inputs.get_all(): if img_one.status.code != 30000: found_error = True if img_one.input_id in bad_ids: found_error_id = True self.assertTrue(found_error) self.assertTrue(found_error_id) # no exception will be raised by default found_error = False for img_one in self.app.inputs.get_all(ignore_error=True): if img_one.status.code != 30000: found_error = True self.assertFalse(found_error)
def test_raises_on_crop_argument(self): with self.assertRaises(UserError): ClarifaiImage(url=sample_inputs.FACEBOOK_IMAGE_URL, crop=[0.1, 0.3, 0.5, 0.7]) with self.assertRaises(UserError): self.app.inputs.create_image_from_url( url=sample_inputs.METRO_IMAGE_URL, crop=[0.2, 0.4, 0.3, 0.6], allow_duplicate_url=True)
def test_post_bulk_images(self): """ post bulk post images """ img1 = ClarifaiImage(url=urls[0], concepts=['train', 'railway'], allow_dup_url=True) img2 = ClarifaiImage( url=urls[1], concepts=['wedding'], not_concepts=['food'], allow_dup_url=True) ret_imgs = self.app.inputs.bulk_create_images([img1, img2]) self.assertEqual(len(list(ret_imgs)), 2) # sleep here to ensure inputs are all properly added before deleting them time.sleep(2) for img in ret_imgs: self.assertTrue(isinstance(img, ClarifaiImage)) try: self.app.inputs.delete(img.input_id) except ApiError: pass
def test_base64_from_fileobj(self): # send it from a remote url data = self.app.api.session.get(sample_inputs.METRO_IMAGE_URL).content image = self.app.inputs.create_image(ClarifaiImage(file_obj=BytesIO(data))) self.assertEqual(len(image.input_id) in [22, 32], True) self.assertTrue(image.url.startswith("https://s3.amazonaws.com/clarifai-api/img")) res2 = self.app.inputs.get(input_id=image.input_id) self.assertEqual(image.input_id, res2.input_id)
def test_init_images(self): """ initialize Image object in different ways """ img1 = ClarifaiImage(url=sample_inputs.FACEBOOK_IMAGE_URL) with open(sample_inputs.TODDLER_FLOWERS_IMAGE_FILE_PATH, 'rb') as f: img2 = ClarifaiImage(file_obj=f) img3 = ClarifaiImage( filename=sample_inputs.TODDLER_FLOWERS_IMAGE_FILE_PATH) with open(sample_inputs.TODDLER_FLOWERS_IMAGE_FILE_PATH, 'rb') as f: toddler_flowers_file_bytes = f.read() toddler_flowers_base64_bytes = base64.b64encode( toddler_flowers_file_bytes) img4 = ClarifaiImage(base64=toddler_flowers_base64_bytes) # init with crops img1 = ClarifaiImage(url=sample_inputs.FACEBOOK_IMAGE_URL, crop=[0.1, 0.3, 0.5, 0.7]) f = open(sample_inputs.TODDLER_FLOWERS_IMAGE_FILE_PATH, 'rb') img2 = ClarifaiImage(file_obj=f, crop=[0.1, 0.3, 0.5, 0.7]) f.close() img3 = ClarifaiImage( filename=sample_inputs.TODDLER_FLOWERS_IMAGE_FILE_PATH, crop=[0.1, 0.3, 0.5, 0.7]) img4 = ClarifaiImage(base64=toddler_flowers_base64_bytes, crop=[0.1, 0.3, 0.5, 0.7]) self.assertListEqual(img1.crop, [0.1, 0.3, 0.5, 0.7]) self.assertListEqual(img2.crop, [0.1, 0.3, 0.5, 0.7]) self.assertListEqual(img3.crop, [0.1, 0.3, 0.5, 0.7]) self.assertListEqual(img4.crop, [0.1, 0.3, 0.5, 0.7]) # init with urls with spaces img5 = ClarifaiImage(url=' ' + sample_inputs.FACEBOOK_IMAGE_URL) self.assertEqual(img5.url, sample_inputs.FACEBOOK_IMAGE_URL) img6 = ClarifaiImage(url=' ' + sample_inputs.FACEBOOK_IMAGE_URL + ' ') self.assertEqual(img6.url, sample_inputs.FACEBOOK_IMAGE_URL)
def test_init_images(self): """ initialize Image object in different ways """ toddler_flowers_file_path = _data_filename('toddler-flowers.jpeg') img1 = ClarifaiImage(url=urls[2]) with open(toddler_flowers_file_path, 'rb') as f: img2 = ClarifaiImage(file_obj=f) img3 = ClarifaiImage(filename=toddler_flowers_file_path) with open(toddler_flowers_file_path, 'rb') as f: toddler_flowers_file_bytes = f.read() toddler_flowers_base64_bytes = base64.b64encode( toddler_flowers_file_bytes) img4 = ClarifaiImage(base64=toddler_flowers_base64_bytes) # init with crops img1 = ClarifaiImage(url=urls[2], crop=[0.1, 0.3, 0.5, 0.7]) f = open(toddler_flowers_file_path, 'rb') img2 = ClarifaiImage(file_obj=f, crop=[0.1, 0.3, 0.5, 0.7]) f.close() img3 = ClarifaiImage(filename=toddler_flowers_file_path, crop=[0.1, 0.3, 0.5, 0.7]) img4 = ClarifaiImage(base64=toddler_flowers_base64_bytes, crop=[0.1, 0.3, 0.5, 0.7]) self.assertListEqual(img1.crop, [0.1, 0.3, 0.5, 0.7]) self.assertListEqual(img2.crop, [0.1, 0.3, 0.5, 0.7]) self.assertListEqual(img3.crop, [0.1, 0.3, 0.5, 0.7]) self.assertListEqual(img4.crop, [0.1, 0.3, 0.5, 0.7]) # init with urls with spaces img5 = ClarifaiImage(url=' ' + urls[2]) self.assertEqual(img5.url, urls[2]) img6 = ClarifaiImage(url=' ' + urls[2] + ' ') self.assertEqual(img6.url, urls[2])
def make_image(url): return ClarifaiImage(image_id=uuid.uuid4().hex, url=url, allow_dup_url=True)
def test_user_errors(self): with self.assertRaises(UserError): ClarifaiImage(url="blah", file_obj="hey") with self.assertRaises(UserError): # not using open('rb') ClarifaiImage(file_obj=open( sample_inputs.TODDLER_FLOWERS_IMAGE_FILE_PATH, mode='r'))
def test_user_errors(self): with self.assertRaises(UserError): ClarifaiImage(url="blah", file_obj="hey") with self.assertRaises(UserError): # not using open('rb') ClarifaiImage(file_obj=open(_data_filename('toddler-flowers.jpeg'), mode='r'))