def test_multiple_lines(self): input_file = StringIO('%s 1:2\n%s 2:1' % ((self.image_name, ) * 2)) output_filename = os.path.join(self.temp_dir, 'empty_file.json') main(input_file, output_filename) self.assertTrue(os.path.exists(output_filename)) with open(output_filename, 'r') as output_file: obj = json.load(output_file) self.assertEqual(2, len(obj['requests'])) for i in range(2): self.assertIn('image', obj['requests'][i]) self.assertIn('features', obj['requests'][i]) self.assertEqual(1, len(obj['requests'][i]['features'])) self.assertIn('content', obj['requests'][i]['image'])
def test_multiple_lines(self): input_file = StringIO( '%s 1:2\n%s 2:1' % ((self.image_name,) * 2)) output_filename = os.path.join(self.temp_dir, 'empty_file.json') main(input_file, output_filename) self.assertTrue(os.path.exists(output_filename)) with open(output_filename, 'r') as output_file: obj = json.load(output_file) self.assertEqual(2, len(obj['requests'])) for i in range(2): self.assertIn('image', obj['requests'][i]) self.assertIn('features', obj['requests'][i]) self.assertEqual(1, len(obj['requests'][i]['features'])) self.assertIn('content', obj['requests'][i]['image'])
def test_one_detection(self): input_file = StringIO(self.image_name + ' 1:1') output_filename = os.path.join(self.temp_dir, 'empty_file.json') main(input_file, output_filename) self.assertTrue(os.path.exists(output_filename)) with open(output_filename, 'r') as output_file: obj = json.load(output_file) self.assertEqual(1, len(obj['requests'])) self.assertIn('image', obj['requests'][0]) self.assertIn('content', obj['requests'][0]['image']) self.assertIn('features', obj['requests'][0]) self.assertEqual(1, len(obj['requests'][0]['features'])) self.assertEqual('FACE_DETECTION', obj['requests'][0]['features'][0]['type']) self.assertEqual(1, obj['requests'][0]['features'][0]['maxResults'])
def test_one_detection(self): input_file = StringIO(self.image_name + ' 1:1') output_filename = os.path.join(self.temp_dir, 'empty_file.json') main(input_file, output_filename) self.assertTrue(os.path.exists(output_filename)) with open(output_filename, 'r') as output_file: obj = json.load(output_file) self.assertEqual(1, len(obj['requests'])) self.assertIn('image', obj['requests'][0]) self.assertIn('content', obj['requests'][0]['image']) self.assertIn('features', obj['requests'][0]) self.assertEqual(1, len(obj['requests'][0]['features'])) self.assertEqual( 'FACE_DETECTION', obj['requests'][0]['features'][0]['type']) self.assertEqual( 1, obj['requests'][0]['features'][0]['maxResults'])
import readoutput import showresults import sys import os def absoluteFilePaths(directory): for dirpath,_,filenames in os.walk(directory): for f in filenames: yield os.path.abspath(os.path.join(dirpath, f)) for image_filename in absoluteFilePaths('tst_images'): input_filename = image_filename.replace('images/', 'inputs/') input_filename = "%s.txt"%os.path.splitext(input_filename)[0] request_filename = image_filename.replace('images/', 'requests/') request_filename = "%s.json"%os.path.splitext(request_filename)[0] response_filename = image_filename.replace('images/', 'responses/') response_filename = "%s.json"%os.path.splitext(response_filename)[0] result_filename = image_filename.replace('images/', 'results/') result_filename = "%s.jpg"%os.path.splitext(result_filename)[0] print image_filename, ' to ', result_filename # generateinput.main(image_filename, input_filename, detection_type=5, max_results=10) #Logo detection generateinput.main(image_filename, input_filename, detection_type=1, max_results=10) #Label detection with open(input_filename, 'rb') as input_file: generatejson.main(input_file, request_filename) readoutput.main(request_filename, response_filename) # not exactly usefull for the final usage showresults.main(image_filename, response_filename, result_filename)
def test_no_max(self): input_file = StringIO(self.image_name + ' 1') output_filename = os.path.join(self.temp_dir, 'empty_file.json') with self.assertRaises(ValueError): main(input_file, output_filename) self.assertFalse(os.path.exists(output_filename))
def test_no_image(self): input_file = StringIO(u'doesnotexist.jpg 1:1') output_filename = os.path.join(self.temp_dir, 'empty_file.json') with self.assertRaises(IOError): main(input_file, output_filename) self.assertFalse(os.path.exists(output_filename))
import requests import generatejson import json import time with open("input.txt", 'r') as input_file: out = "" for i, line in enumerate(input_file): image_filename, features, id = line.lstrip().split(' ', 2) try: data = generatejson.main(image_filename, features) response = requests.post( url= 'https://vision.googleapis.com/v1/images:annotate?key=AIzaSyBBpVtsrJ2HPE9KKEwZyTrmwI-pPSBXtYA', data=data[0], headers={'Content-Type': 'application/json'}) if i % 10 == 0: print i #print json.dumps(json.loads(response.text)) out += json.dumps(json.loads( response.text)) + "\t" + str(id) + "\n" except: print str(id) + " can't find image'" out += "nan\t" + str(id) + "\n" ''' response = requests.post(url='https://vision.googleapis.com/v1/images:annotate?key=AIzaSyCcczu2Kj2NvCkiRsWTZO_qbsgolfx9gCk','''