def get_mnist_def(model): """ Follows https://github.com/Lasagne/Lasagne/blob/master/examples/mnist.py python -m ibeis_cnn MNISTModel.init_arch --verbcnn --name=mnist --show python -m ibeis_cnn.models.mnist MNISTModel.fit:0 --name=mnist --vd --monitor """ import ibeis_cnn.__LASAGNE__ as lasagne from ibeis_cnn import custom_layers batch_norm = False bundles = custom_layers.make_bundles( nonlinearity=lasagne.nonlinearities.rectify, batch_norm=batch_norm, W=lasagne.init.Orthogonal('relu'), ) b = ut.DynStruct(copy_dict=bundles) network_layers_def = [ b.InputBundle(shape=model.input_shape), b.ConvBundle(num_filters=32, filter_size=(5, 5), pool=True), b.ConvBundle(num_filters=32, filter_size=(5, 5), pool=True), # A fully-connected layer and 50% dropout of its inputs b.DenseBundle(num_units=256, dropout=.5), # And, finally, the 10-unit output layer with 50% dropout on its inputs b.SoftmaxBundle(num_units=model.output_dims, dropout=.5), ] return network_layers_def
def initialize_job_manager(ibs): """ Starts a background zmq job engine. Initializes a zmq object in this thread that can talk to the background processes. Run from the webserver CommandLine: python -m ibeis.web.job_engine --exec-initialize_job_manager:0 Example: >>> # DISABLE_DOCTEST >>> from ibeis.web.job_engine import * # NOQA >>> import ibeis >>> ibs = ibeis.opendb(defaultdb='testdb1') >>> from ibeis.web import apis_engine >>> from ibeis.web import job_engine >>> ibs.load_plugin_module(job_engine) >>> ibs.load_plugin_module(apis_engine) >>> ibs.initialize_job_manager() >>> print('Initializqation success. Now closing') >>> ibs.close_job_manager() >>> print('Closing success.') Example: >>> # WEB_DOCTEST >>> from ibeis.web.job_engine import * # NOQA >>> import ibeis >>> import requests >>> web_instance = ibeis.opendb_bg_web(db='testdb1') >>> baseurl = 'http://127.0.1.1:5000' >>> _payload = {'image_attrs_list': [], 'annot_attrs_list': []} >>> payload = ut.map_dict_vals(ut.to_json, _payload) >>> resp1 = requests.post(baseurl + '/api/test/helloworld/?f=b', data=payload) >>> #resp2 = requests.post(baseurl + '/api/image/json/', data=payload) >>> #print(resp2) >>> web_instance.terminate() >>> #json_dict = resp2.json() >>> #text = json_dict['response'] >>> #print(text) """ ibs.job_manager = ut.DynStruct() ibs.job_manager.jobiface = JobInterface(id_=0) if not ut.get_argflag('--fg'): ibs.job_manager.reciever = JobBackend() ibs.job_manager.reciever.initialize_background_processes( dbdir=ibs.get_dbdir()) ibs.job_manager.jobiface.initialize_client_thread() # Wait until the collector becomes live while 0 and True: result = ibs.get_job_status(-1) print('result = %r' % (result, )) if result['status'] == 'ok': break
def dummy_import_vim(fpath=None): if fpath is not None: fpath = abspath(expanduser(fpath)) try: import vim dohack = False except ImportError: dohack = True vim = None if vim is not None: if getattr(vim, '__ishack__', False): if fpath != vim.current.buffer.name: dohack = True if dohack: import sys import utool as ut vim = ut.DynStruct() vim.__ishack__ = True vim.current = ut.DynStruct() vim.current.window = ut.DynStruct() vim.current.window.cursor = (0, 0) if fpath is None: lines = [ 'line1', 'line2', 'line3', ] else: lines = ut.readfrom(fpath).splitlines() vim.current.buffer = DummyVimBuffer(lines) vim.current.buffer.name = fpath # VERY HACKY sys.modules['vim'] = vim return vim
def get_inception_def(model): """ python -m ibeis_cnn MNISTModel.init_arch --verbcnn --name=resnet --show python -m ibeis_cnn.models.mnist MNISTModel.fit:0 --name=resnet --vd --monitor """ import ibeis_cnn.__LASAGNE__ as lasagne from ibeis_cnn import custom_layers batch_norm = model.batch_norm if model.dropout is None: dropout = 0 if batch_norm else .5 else: dropout = model.dropout bundles = custom_layers.make_bundles( nonlinearity=lasagne.nonlinearities.rectify, batch_norm=batch_norm, ) b = ut.DynStruct(copy_dict=bundles) N = 64 network_layers_def = [ b.InputBundle(shape=model.input_shape, noise=False), b.ConvBundle(num_filters=N, filter_size=(3, 3), pool=False), b.ConvBundle(num_filters=N, filter_size=(3, 3), pool=True), b.InceptionBundle(branches=[ dict(t='c', s=(1, 1), r=00, n=N), dict(t='c', s=(3, 3), r=N // 2, n=N), dict(t='c', s=(3, 3), r=N // 4, n=N // 2, d=2), dict(t='p', s=(3, 3), n=N // 2) ], ), b.InceptionBundle(branches=[ dict(t='c', s=(1, 1), r=00, n=N), dict(t='c', s=(3, 3), r=N // 2, n=N), dict(t='c', s=(3, 3), r=N // 4, n=N // 2, d=2), dict(t='p', s=(3, 3), n=N // 2) ], dropout=dropout, pool=True), # --- b.DenseBundle(num_units=N, dropout=dropout), b.DenseBundle(num_units=N, dropout=dropout), # And, finally, the 10-unit output layer with 50% dropout on its inputs b.SoftmaxBundle(num_units=model.output_dims, dropout=dropout), #b.GlobalPool #b.NonlinearitySoftmax(), ] return network_layers_def
def make_cacher(name, cfgstr=None): if cfgstr is None: cfgstr = ut.hashstr27(qreq_.get_cfgstr()) if False and ut.is_developer(): return ut.Cacher(fname=name + '_' + qreq_.ibs.get_dbname(), cfgstr=cfgstr, cache_dir=ut.ensuredir( ut.truepath('~/Desktop/smkcache'))) else: wrp = ut.DynStruct() def ensure(func): return func() wrp.ensure = ensure return wrp
def static_new(cls, depc, parent_rowids, cfgdict=None, tablename=None): """ hack for autoreload """ request = cls() if tablename is None: try: if hasattr(cls, '_tablename'): tablename = cls._tablename else: tablename = ut.invert_dict(depc.requestclass_dict)[cls] except Exception as ex: ut.printex(ex, 'tablename must be given') raise request.tablename = tablename request.parent_rowids = parent_rowids request.depc = depc if cfgdict is None: cfgdict = {} configclass = depc.configclass_dict[tablename] config = configclass(**cfgdict) request.config = config # HACK FOR IBEIS request.params = dict(config.parse_items()) # HACK-ier FOR BACKWARDS COMPATABILITY if True: # params.featweight_cfgstr = query_cfg._featweight_cfg.get_cfgstr() # TODO: if this hack is fully completed need a way of getting the # full config belonging to both chip + feat try: request.params['chip_cfgstr'] = config.chip_cfg.get_cfgstr() request.params['chip_cfg_dict'] = config.chip_cfg.asdict() request.params['feat_cfgstr'] = config.feat_cfg.get_cfgstr() request.params[ 'hesaff_params'] = config.feat_cfg.get_hesaff_params() request.params[ 'featweight_cfgstr'] = config.feat_weight_cfg.get_cfgstr() except AttributeError: pass request.qparams = ut.DynStruct() for key, val in request.params.items(): setattr(request.qparams, key, val) return request
def ensure_simple_server(port=5832): r""" CommandLine: python -m wbia.web.apis_engine --exec-ensure_simple_server python -m utool.util_web --exec-start_simple_webserver Example: >>> # DISABLE_DOCTEST >>> from wbia.web.apis_engine import * # NOQA >>> result = ensure_simple_server() >>> print(result) """ if ut.is_local_port_open(port): bgserver = ut.spawn_background_process(ut.start_simple_webserver, port=port) return bgserver else: bgserver = ut.DynStruct() bgserver.terminate2 = lambda: None logger.info('server is running elsewhere') return bgserver
def init_arch(model, verbose=True): """ CommandLine: python -m ibeis_cnn DummyModel.init_arch --verbcnn --show Example: >>> # ENABLE_DOCTEST >>> from ibeis_cnn.models.dummy import * # NOQA >>> model = DummyModel(autoinit=True) >>> model.print_model_info_str() >>> print(model) >>> ut.quit_if_noshow() >>> model.show_arch() >>> ut.show_if_requested() """ import ibeis_cnn.__LASAGNE__ as lasange from ibeis_cnn import custom_layers if verbose: print('init arch') bundles = custom_layers.make_bundles( nonlinearity=lasange.nonlinearities.rectify, batch_norm=False, ) b = ut.DynStruct(copy_dict=bundles) network_layers_def = [ b.InputBundle(shape=model.input_shape), b.ConvBundle(num_filters=5, filter_size=(3, 3)), b.DenseBundle(num_units=8), b.SoftmaxBundle(num_units=model.output_dims), ] from ibeis_cnn import custom_layers network_layers = custom_layers.evaluate_layer_list(network_layers_def) #model.network_layers = network_layers model.output_layer = network_layers[-1] if ut.VERBOSE: model.print_arch_str() model.print_layer_info() return model.output_layer
def get_resnet_def(model): """ A residual network with pre-activations python -m ibeis_cnn MNISTModel.init_arch --verbcnn --name=resnet --show python -m ibeis_cnn.models.mnist MNISTModel.fit:0 --name=resnet --vd --monitor """ import ibeis_cnn.__LASAGNE__ as lasagne from ibeis_cnn import custom_layers batch_norm = model.batch_norm W = lasagne.init.HeNormal(gain='relu') bundles = custom_layers.make_bundles( filter_size=(3, 3), nonlinearity=lasagne.nonlinearities.rectify, batch_norm=batch_norm, W=W, ) b = ut.DynStruct(copy_dict=bundles) N = 16 network_layers_def = [ b.InputBundle(shape=model.input_shape, noise=False), b.ConvBundle(num_filters=N, pool=False), b.ResidualBundle(num_filters=N, stride=(2, 2), preactivate=False), b.ResidualBundle(num_filters=N), b.ResidualBundle(num_filters=N, stride=(2, 2)), #b.ResidualBundle(num_filters=N), b.ResidualBundle(num_filters=N, stride=(2, 2), dropout=.2), b.ResidualBundle(num_filters=N, stride=(2, 2), dropout=.5, postactivate=True), b.AveragePool(), b.SoftmaxBundle(num_units=model.output_dims, dropout=.5), ] return network_layers_def
def get_lenet_def(model): """ python -m ibeis_cnn MNISTModel.init_arch --verbcnn --name=lenet --show python -m ibeis_cnn.models.mnist MNISTModel.fit:0 --name=lenet --vd --monitor """ import ibeis_cnn.__LASAGNE__ as lasagne from ibeis_cnn import custom_layers batch_norm = model.batch_norm dropout = model.dropout W = lasagne.init.Orthogonal('relu') bundles = custom_layers.make_bundles( nonlinearity=lasagne.nonlinearities.rectify, batch_norm=batch_norm, W=W, ) b = ut.DynStruct(copy_dict=bundles) N = 128 network_layers_def = [ b.InputBundle(shape=model.input_shape, noise=False), b.ConvBundle(num_filters=N, filter_size=(3, 3), pool=True), b.ConvBundle(num_filters=N, filter_size=(3, 3), pool=False), b.ConvBundle(num_filters=N, filter_size=(3, 3), pool=True), b.ConvBundle(num_filters=N, filter_size=(3, 3), pool=False), b.ConvBundle(num_filters=N, filter_size=(2, 2), pool=False), # A fully-connected layer and 50% dropout of its inputs b.DenseBundle(num_units=N * 2, dropout=dropout), # A fully-connected layer and 50% dropout of its inputs b.DenseBundle(num_units=N * 2, dropout=dropout), # And, finally, the 10-unit output layer with 50% dropout on its inputs b.SoftmaxBundle(num_units=model.output_dims, dropout=dropout), ] return network_layers_def
def ishow_image(ibs, gid, sel_aids=[], fnum=None, select_callback=None, **kwargs): if ut.VERBOSE: print(ut.get_caller_name(range(9))) print('[interact_image] gid=%r fnum=%r' % (gid, fnum)) if fnum is None: fnum = df2.next_fnum() # TODO: change to class based structure self = ut.DynStruct() self.fnum = fnum fig = ih.begin_interaction('image', fnum) # printDBG(utool.func_str(interact_image, [], locals())) kwargs['draw_lbls'] = kwargs.get('draw_lbls', True) def _image_view(sel_aids=sel_aids, **_kwargs): try: viz.show_image(ibs, gid, sel_aids=sel_aids, fnum=self.fnum, **_kwargs) df2.set_figtitle('Image View') except TypeError as ex: ut.printex(ex, ut.repr2(_kwargs)) raise # Create callback wrapper def _on_image_click(event): print('[inter] clicked image') if ih.clicked_outside_axis(event): # Toggle draw lbls kwargs['draw_lbls'] = not kwargs.get('draw_lbls', True) _image_view(**kwargs) else: ax = event.inaxes viztype = vh.get_ibsdat(ax, 'viztype') annotation_centers = vh.get_ibsdat(ax, 'annotation_centers', default=[]) print(' annotation_centers=%r' % annotation_centers) print(' viztype=%r' % viztype) if len(annotation_centers) == 0: print(' ...no chips exist to click') return x, y = event.xdata, event.ydata # Find ANNOTATION center nearest to the clicked point aid_list = vh.get_ibsdat(ax, 'aid_list', default=[]) import vtool as vt centx, _dist = vt.nearest_point(x, y, annotation_centers) aid = aid_list[centx] print(' ...clicked aid=%r' % aid) if select_callback is not None: # HACK, should just implement this correctly here select_callback(gid, sel_aids=[aid], fnum=self.fnum) else: _image_view(sel_aids=[aid]) viz.draw() _image_view(**kwargs) viz.draw() ih.connect_callback(fig, 'button_press_event', _on_image_click)
def ishow_keypoints(chip, kpts, desc, fnum=0, figtitle=None, nodraw=False, **kwargs): """ TODO: Depricate in favor of the class CommandLine: python -m plottool.interact_keypoints --test-ishow_keypoints --show python -m plottool.interact_keypoints --test-ishow_keypoints --show --fname zebra.png Example: >>> # DISABLE_DOCTEST >>> from plottool.interact_keypoints import * # NOQA >>> import numpy as np >>> import plottool as pt >>> import utool as ut >>> import pyhesaff >>> import vtool as vt >>> kpts, vecs, imgBGR = pt.viz_keypoints.testdata_kpts() >>> ut.quit_if_noshow() >>> #pt.interact_keypoints.ishow_keypoints(imgBGR, kpts, vecs, ori=True, ell_alpha=.4, color='distinct') >>> pt.interact_keypoints.ishow_keypoints(imgBGR, kpts, vecs, ori=True, ell_alpha=.4) >>> pt.show_if_requested() """ if isinstance(chip, six.string_types): import vtool as vt chip = vt.imread(chip) fig = ih.begin_interaction('keypoint', fnum) annote_ptr = [1] self = ut.DynStruct() # MOVE TO A CLASS INTERACTION self.kpts = kpts vecs = desc self.vecs = vecs def _select_ith_kpt(fx): print('[interact] viewing ith=%r keypoint' % fx) # Get the fx-th keypiont kp, sift = kpts[fx], vecs[fx] # Draw the image with keypoint fx highlighted _viz_keypoints(fnum, (2, 1, 1), sel_fx=fx, **kwargs) # MAYBE: remove kwargs # Draw the selected feature nRows, nCols, px = (2, 3, 3) draw_feat_row(chip, fx, kp, sift, fnum, nRows, nCols, px, None) def _viz_keypoints(fnum, pnum=(1, 1, 1), **kwargs): df2.figure(fnum=fnum, docla=True, doclf=True) show_keypoints(chip, kpts, fnum=fnum, pnum=pnum, **kwargs) if figtitle is not None: df2.set_figtitle(figtitle) def _on_keypoints_click(event): print('[viz] clicked keypoint view') if event is None or event.xdata is None or event.inaxes is None: annote_ptr[0] = (annote_ptr[0] + 1) % 3 mode = annote_ptr[0] ell = mode == 1 pts = mode == 2 print('... default kpts view mode=%r' % mode) _viz_keypoints(fnum, ell=ell, pts=pts, **kwargs) # MAYBE: remove kwargs else: ax = event.inaxes viztype = ph.get_plotdat(ax, 'viztype', None) print('[ik] viztype=%r' % viztype) if viztype == 'keypoints': kpts = ph.get_plotdat(ax, 'kpts', []) if len(kpts) == 0: print('...nokpts') else: print('...nearest') x, y = event.xdata, event.ydata fx = ut.nearest_point(x, y, kpts)[0] _select_ith_kpt(fx) elif viztype == 'warped': hs_fx = ph.get_plotdat(ax, 'fx', None) #kpts = ph.get_plotdat(ax, 'kpts', []) if hs_fx is not None: # Ugly. Interactions should be changed to classes. kp = self.kpts[hs_fx] # FIXME sift = self.vecs[hs_fx] df2.draw_keypoint_gradient_orientations( chip, kp, sift=sift, mode='vec', fnum=df2.next_fnum()) elif viztype.startswith('colorbar'): pass # Hack to get a specific scoring feature #sortx = self.fs.argsort() #idx = np.clip(int(np.round(y * len(sortx))), 0, len(sortx) - 1) #mx = sortx[idx] #(fx1, fx2) = self.fm[mx] #(fx1, fx2) = self.fm[mx] #print('... selected score at rank idx=%r' % (idx,)) #print('... selected score with fs=%r' % (self.fs[mx],)) #print('... resolved to mx=%r' % mx) #print('... fx1, fx2 = %r, %r' % (fx1, fx2,)) #self.select_ith_match(mx) else: print('...unhandled') ph.draw() # Draw without keypoints the first time _viz_keypoints(fnum, **kwargs) # MAYBE: remove kwargs ih.connect_callback(fig, 'button_press_event', _on_keypoints_click) if not nodraw: ph.draw()
def opendb_bg_web(*args, managed=False, **kwargs): """ Wrapper around opendb_in_background, returns a nice web_ibs object to execute web calls using normal python-like syntax Args: *args: passed to opendb_in_background **kwargs: port (int): domain (str): if specified assumes server is already running somewhere otherwise kwargs is passed to opendb_in_background start_job_queue (bool) managed (bool): if True, return a context manager that terminates the server upon completion of the block Returns: web_ibs - this is a KillableProcess object with special functions CommandLine: python -m wbia.entry_points opendb_bg_web Example: >>> # DISABLE_DOCTEST >>> from wbia.entry_points import * # NOQA >>> args = tuple() >>> kwargs = {} >>> print('Opening a web_ibs') >>> web_ibs = opendb_bg_web() >>> print('SUCESS Opened a web_ibs!') >>> print(web_ibs) >>> print('Now kill the web_ibs') >>> web_ibs.terminate2() """ import utool as ut from wbia.web import appfuncs domain = kwargs.pop('domain', ut.get_argval('--domain', type_=str, default=None)) port = kwargs.pop('port', appfuncs.DEFAULT_WEB_API_PORT) if 'wait' in kwargs: logger.info('NOTE: No need to specify wait param anymore. ' 'This is automatically taken care of.') if domain is None: # Requesting a local test server _kw = dict(web=True, browser=False) _kw.update(kwargs) web_ibs = opendb_in_background(*args, **_kw) else: # Using a remote controller, no need to spin up anything web_ibs = ut.DynStruct() web_ibs.terminate2 = lambda: None # Augment web instance with usefull test functions if domain is None: domain = 'http://127.0.1.1' if not domain.startswith('http://'): domain = 'http://' + domain baseurl = domain + ':' + str(port) web_ibs.domain = domain web_ibs.port = port web_ibs.baseurl = baseurl def get(suffix, **kwargs): import requests return requests.get(baseurl + suffix) def post(suffix, **kwargs): import requests return requests.post(baseurl + suffix) def send_wbia_request(suffix, type_='post', json=True, **kwargs): """ Posts a request to a url suffix """ import requests import utool as ut if not suffix.endswith('/'): raise Exception('YOU PROBABLY WANT A / AT THE END OF YOUR URL') payload = ut.map_dict_vals(ut.to_json, kwargs) if type_ == 'post': resp = requests.post(baseurl + suffix, data=payload) content = resp._content elif type_ == 'get': resp = requests.get(baseurl + suffix, data=payload) content = resp.content if json: try: content = ut.from_json(content) except ValueError: raise Exception('Expected JSON string but got content=%r' % (content, )) else: # logger.info('content = %r' % (content,)) if content['status']['code'] != 200: logger.info(content['status']['message']) raise Exception(content['status']['message']) content = content['response'] return content def wait_for_results(jobid, timeout=None, delays=[1, 3, 10]): """ Waits for results from an engine """ for _ in ut.delayed_retry_gen(delays): logger.info('Waiting for jobid = %s' % (jobid, )) status_response = web_ibs.send_wbia_request( '/api/engine/job/status/', jobid=jobid) if status_response['jobstatus'] in ('completed', 'exception'): break return status_response def read_engine_results(jobid): result_response = web_ibs.send_wbia_request('/api/engine/job/result/', jobid=jobid) return result_response def send_request_and_wait(suffix, type_='post', timeout=None, **kwargs): jobid = web_ibs.send_wbia_request(suffix, type_=type_, **kwargs) status_response = web_ibs.wait_for_results(jobid, timeout) # NOQA result_response = web_ibs.read_engine_results(jobid) # >>> cmdict = ut.from_json(result_response['json_result'])[0] return result_response web_ibs.send_wbia_request = send_wbia_request web_ibs.wait_for_results = wait_for_results web_ibs.read_engine_results = read_engine_results web_ibs.send_request_and_wait = send_request_and_wait web_ibs.get = get web_ibs.post = post def wait_until_started(): """ waits until the web server responds to a request """ import requests for count in ut.delayed_retry_gen([1], timeout=15): if True or ut.VERBOSE: logger.info('Waiting for server to be up. count=%r' % (count, )) try: web_ibs.send_wbia_request('/api/test/heartbeat/', type_='get') break except requests.ConnectionError: pass wait_until_started() @contextmanager def managed_server(): try: yield web_ibs finally: web_ibs.terminate2() if managed: return managed_server() return web_ibs
This module should handle all things elliptical """ from __future__ import absolute_import, division, print_function from six.moves import zip, range from numpy.core.umath_tests import matrix_multiply import scipy.signal as spsignal import numpy as np from vtool import keypoint as ktool from vtool import image as gtool import utool as ut try: import cv2 except ImportError as ex: print('ERROR: import cv2 is failing!') cv2 = ut.DynStruct() (print, rrr, profile) = ut.inject2(__name__, '[ellipse]', DEBUG=False) @profile def adaptive_scale(img_fpath, kpts, nScales=4, low=-.5, high=.5, nSamples=16): #imgBGR = cv2.imread(img_fpath, flags=cv2.CV_LOAD_IMAGE_COLOR) imgBGR = gtool.imread(img_fpath) nKp = len(kpts) dtype_ = kpts.dtype # Work with float65 kpts_ = np.array(kpts, dtype=np.float64) # Expand each keypoint into a number of different scales
def opendb_bg_web(*args, **kwargs): """ Wrapper around opendb_in_background, returns a nice web_ibs object to execute web calls using normal python-like syntax Accespts domain and port as kwargs Kwargs: port, domain CommandLine: python -m ibeis.main_module opendb_bg_web Example: >>> # DISABLE_DOCTEST >>> from ibeis.main_module import * # NOQA >>> print('Opening a web_ibs') >>> web_ibs = opendb_bg_web() >>> print('SUCESS Opened a web_ibs!') >>> print(web_ibs) >>> print('Now kill the web_ibs') >>> web_ibs.terminate2() """ import utool as ut kwargs = kwargs.copy() domain = kwargs.pop('domain', ut.get_argval('--domain', type_=str, default=None)) port = kwargs.pop('port', 5000) if 'wait' in kwargs: print('NOTE: No need to specify wait param anymore. ' 'This is automatically taken care of.') if domain is None: # Requesting a local test server _kw = dict(web=True, browser=False) _kw.update(kwargs) web_ibs = opendb_in_background(*args, **_kw) else: # Using a remote controller, no need to spin up anything web_ibs = ut.DynStruct() web_ibs.terminate2 = lambda: None # Augment web instance with usefull test functions if domain is None: domain = 'http://127.0.1.1' if not domain.startswith('http://'): domain = 'http://' + domain baseurl = domain + ':' + str(port) web_ibs.domain = domain web_ibs.port = port web_ibs.baseurl = baseurl def send_ibeis_request(suffix, type_='post', **kwargs): """ Posts a request to a url suffix """ import requests import utool as ut if not suffix.endswith('/'): raise Exception('YOU PROBABLY WANT A / AT THE END OF YOUR URL') payload = ut.map_dict_vals(ut.to_json, kwargs) if type_ == 'post': resp = requests.post(baseurl + suffix, data=payload) json_content = resp._content elif type_ == 'get': resp = requests.get(baseurl + suffix, data=payload) json_content = resp.content try: content = ut.from_json(json_content) except ValueError: raise Exception('Expected JSON string but got json_content=%r' % (json_content, )) else: # print('content = %r' % (content,)) if content['status']['code'] != 200: print(content['status']['message']) raise Exception(content['status']['message']) request_response = content['response'] return request_response def wait_for_results(jobid, timeout=None, delays=[1, 3, 10]): """ Waits for results from an engine """ for _ in ut.delayed_retry_gen(delays): print('Waiting for jobid = %s' % (jobid, )) status_response = web_ibs.send_ibeis_request( '/api/engine/job/status/', jobid=jobid) if status_response['jobstatus'] == 'completed': break return status_response def read_engine_results(jobid): result_response = web_ibs.send_ibeis_request('/api/engine/job/result/', jobid=jobid) return result_response def send_request_and_wait(suffix, type_='post', timeout=None, **kwargs): jobid = web_ibs.send_ibeis_request(suffix, type_=type_, **kwargs) status_response = web_ibs.wait_for_results(jobid, timeout) # NOQA result_response = web_ibs.read_engine_results(jobid) #>>> cmdict = ut.from_json(result_response['json_result'])[0] return result_response web_ibs.send_ibeis_request = send_ibeis_request web_ibs.wait_for_results = wait_for_results web_ibs.read_engine_results = read_engine_results web_ibs.send_request_and_wait = send_request_and_wait def wait_until_started(): """ waits until the web server responds to a request """ import requests for count in ut.delayed_retry_gen([1], timeout=15): if ut.VERBOSE: print('Waiting for server to be up. count=%r' % (count, )) try: web_ibs.send_ibeis_request('/api/test/heartbeat/', type_='get') break except requests.ConnectionError: pass wait_until_started() return web_ibs
Kwargs: fname=u'', name=u'', style=u'normal', variant=u'normal', weight=u'normal', stretch=u'normal', size=u'medium' """ kwargs['family'] = 'monospace' font_prop = mpl.font_manager.FontProperties(*args, **kwargs) return font_prop FONTS = ut.DynStruct() FONTS.smallest = FontProp(weight='light', size=SMALLEST) FONTS.small = FontProp(weight='light', size=SMALL) FONTS.smaller = FontProp(weight='light', size=SMALLER) FONTS.med = FontProp(weight='light', size=MED) FONTS.large = FontProp(weight='light', size=LARGE) FONTS.medbold = FontProp(weight='bold', size=MED) FONTS.largebold = FontProp(weight='bold', size=LARGE) # SPECIFIC FONTS if False: # personal FONTS.legend = FONTS.small FONTS.figtitle = FONTS.med FONTS.axtitle = FONTS.small