def akari_cron_override(text): utils.logger.info('Overriding cron with "%s"!', text) params = dict(type='animation', shuffle_results=False) if cfg('twitter:cron_override_image_url'): params['image_url'] = cfg('twitter:cron_override_image_url') akari_publish(text, **params) return True
def score(status): favs = status.favorite_count rts = status.retweet_count followers = status.user.followers_count if (followers == 0 or # avoid division by zero later on status.user.protected): # don't post from protected users return -1 # decay coefficient. promotes newer tweets to compensate for the # lower amount of favs they have received (fewer people have seen # them, in theory) diff = (datetime.utcnow() - status.created_at).total_seconds() score = utils.decay(diff, cfg('twitter:cron_interval:int') * 60, 1.5) score *= (favs + rts * 2) / followers # apply penalties. some tweets carry a penalty but are not removed # right away, in case there isn't anything better. # at least 80% of letters in the status must be /a-zA-Z/ clean_text = utils.clean(status.text, urls=True, replies=True, rts=True) meat = sum(c in string.ascii_letters for c in clean_text) or -1 # also, get the author's lang and filter him if it's not in the wl lang_wl = cfg('twitter:cron_lang_whitelist:list') # filter quoted tweets filter_quotes = cfg('twitter:cron_filter_quotes:bool') if ((lang_wl and status.user.lang not in lang_wl) or (filter_quotes and status.is_quote_status) or meat / len(clean_text) < 0.8 or followers < (followers_median * 1.5) or favs < favs_median): score /= 10 return score
def compute_anchors(angle): """ compute angle offset and which bin the angle lies in input: fixed local orientation [0, 2pi] output: [bin number, angle offset] For two bins: if angle < pi, l = 0, r = 1 if angle < 1.65, return [0, angle] elif pi - angle < 1.65, return [1, angle - pi] if angle > pi, l = 1, r = 2 if angle - pi < 1.65, return [1, angle - pi] elif 2pi - angle < 1.65, return [0, angle - 2pi] """ anchors = [] wedge = 2. * np.pi / cfg().bin # 2pi / bin = pi l_index = int(angle / wedge) # angle/pi r_index = l_index + 1 # (angle - l_index*pi) < pi/2 * 1.05 = 1.65 if (angle - l_index * wedge) < wedge / 2 * (1 + cfg().overlap / 2): anchors.append([l_index, angle - l_index * wedge]) # (r*pi + pi - angle) < pi/2 * 1.05 = 1.65 if (r_index * wedge - angle) < wedge / 2 * (1 + cfg().overlap / 2): anchors.append([r_index % cfg().bin, angle - r_index * wedge]) return anchors
def show_gallery(path): global g user = g.user if user is None and not cfg()["public_access"]: return render("gallery.html", authn_error="only logged in users may view this page") path = path.encode("utf-8") check_jailed_path(path, "data") groups = get_access_groups(path) if not cfg()["public_access"]: if not is_admin_mode(user) and not access_permitted(groups, user["groups"]): return render("gallery.html", authn_error=True) gallery = Gallery(path, follow_freedesktop_standard = cfg()["follow_freedesktop_standard"]) gallery.populate() if cfg()["public_access"] or is_admin_mode(user): galleries = gallery.get_galleries() else: galleries = [gal for gal in gallery.get_galleries() if access_permitted(get_access_groups(gal["key"]), user["groups"])] groups_error = None if request.method == 'POST': action = request.form["action"] if action == "save": if not is_admin_mode(user): return render("gallery.html", authn_error=True) groups = request.form["groups_string"].split() try: set_access_groups(path, groups) except IOError, ioe: groups_error = "%s" %ioe else: raise Exception("Unknown gallery editing action: \"%s\"" %action)
def __init__(self, text, type='still', shuffle_results=False, caption=None, image_url=None): self.text = text # make hashtags searchable if '#' in self.text: self.text = ' %s ' % self.text self.caption = caption if caption else self.text if type not in ('still', 'animation'): raise ValueError('Incorrect Akari type "%s"' % type) self.type = type self.override = cfg('image_search:override:list') self.limit_results = cfg('akari:limit_results:int') self.caption_type = cfg('akari:caption_type') if image_url: result = ImageSearchResult(image_url, 'overridden', 'overridden') results = [result] else: if self.override: results = image_search(random.choice(self.override)) random.shuffle(results) else: results = image_search(self.text) if self.limit_results: results = results[:self.limit_results] if shuffle_results: random.shuffle(results) else: # results are always shuffled if # of results is uncapped random.shuffle(results) for result in results: # first, download the result. if it fails, continue for the next try: result.download() except ImageSearchResultError as exc: utils.logger.info('Error downloading this result: %s', exc) continue # then, compose it. 3 tries, in case Wand acts funny. for _ in range(3): try: self.compose(result) return except AkariTooBigError: utils.logger.info('Composed an animation that is too big.') # a retry would generate the same gif with the same # problem, so don't do that. go for the next result. break except AkariWandIsRetardedError: # this, we want to retry it utils.logger.info('Wand failed to save the animation.') raise AkariComposingError('Could not generate an image.')
def test_singleton(): borg = cfg().load_config_file() another_borg = cfg().load_config_file() print borg is not another_borg print borg print another_borg assert borg, another_borg print 'Singleton test pass!' cfg()._drop()
def send_email(subject, text): """sends an email using the mailgun http api""" url = ('https://api.mailgun.net/v3/%s/messages' % cfg('mail:mailgun_domain')) auth = ('api', cfg('mail:mailgun_key')) data = { 'from': 'Akari Bot <%s>' % cfg('mail:from'), 'to': [cfg('mail:to')], 'subject': subject, 'text': text } return requests.post(url, auth=auth, data=data)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._answerer = telepot.aio.helper.Answerer(self) self.username = cfg('telegram:username') realname = cfg('telegram:realname') self.help_msg_priv = self.HELP_MSG_PRIV % realname tw_url = cfg('telegram:twitter_url') if tw_url: self.help_msg_priv += self.HELP_MSG_TW + tw_url self.help_msg_group = self.HELP_MSG_GROUP % realname self.rate_limit_exemptions = cfg('telegram:rate_limit_exemptions:' 'int_list')
def is_eligible(user): """checks if a user is eligible to be followed.""" if (cfg('tasks:follow_max_friends:int') and user.friends_count > cfg('tasks:follow_max_friends:int')): utils.logger.info('@%s has too many friends: %d', user.screen_name, user.friends_count) return False if (cfg('tasks:follow_min_followers:int') and user.followers_count < cfg('tasks:follow_min_followers:int')): utils.logger.info('@%s has too few followers: %d', user.screen_name, user.followers_count) return False if (cfg('tasks:follow_only_lang:list') and user.lang.lower() not in cfg('tasks:follow_only_lang:list')): utils.logger.info('@%s uses a language not in the whitelist: %s', user.screen_name, user.lang.lower()) return False if cfg('tasks:follow_last_post_days:int') and hasattr(user, 'status'): delta_min = (datetime.now() - timedelta(days=cfg('tasks:follow_last_post_days:int'))) if user.status.created_at < delta_min: utils.logger.info('@%s has not posted anything for too long', user.screen_name) return False return True
def prepare_input_and_output_tf(train_inst, image_dir, depth_dir): ''' prepare image patch for training input: train_inst -- input image for training output: img -- cropped bbox train_inst['dims'] -- object dimensions train_inst['orient'] -- object orientation (or flipped orientation) train_inst['conf_flipped'] -- orientation confidence ''' xmin = train_inst['xmin'] # + np.random.randint(-cfg().jit, cfg().jit+1) ymin = train_inst['ymin'] # + np.random.randint(-cfg().jit, cfg().jit+1) xmax = train_inst['xmax'] # + np.random.randint(-cfg().jit, cfg().jit+1) ymax = train_inst['ymax'] # + np.random.randint(-cfg().jit, cfg().jit+1) #print("image_dir = ", image_dir) img = cv2.imread(image_dir) dpth = cv2.imread(depth_dir) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) dpth = cv2.cvtColor(dpth, cv2.COLOR_BGR2RGB) img = copy.deepcopy(img[ymin:ymax + 1, xmin:xmax + 1]).astype(np.float32) dpth = copy.deepcopy(dpth[ymin:ymax + 1, xmin:xmax + 1]).astype(np.float32) ######################################################################### # flip the image # 50% percent choose 1, 50% percent choose 0 flip = np.random.binomial(1, .5) if flip > 0.5: img = cv2.flip(img, 1) dpth = cv2.flip(dpth, 1) # resize the image to standard size img = cv2.resize(img, (cfg().norm_h, cfg().norm_w)) dpth = cv2.resize(dpth, (cfg().norm_h, cfg().norm_w)) # minus the mean value in each channel img = img - np.array([[[103.939, 116.779, 123.68]]]) dpth = dpth - np.array([[[103.939, 116.779, 123.68]]]) ### Fix orientation and confidence if flip > 0.5: return img, train_inst['dims'], train_inst[ 'orient_flipped'], train_inst['conf_flipped'], dpth else: return img, train_inst['dims'], train_inst['orient'], train_inst[ 'conf'], dpth
def _is_token_valid(self, token): try: jwt.decode(bytes(token, encoding='UTF-8'), cfg('app', 'secret'), algorithm='HS256', verify=True) except (jwt.InvalidTokenError, jwt.DecodeError) as e: print(">>> TOKEN ERROR", e) return False return True
def __init__(self, consumer_key, consumer_secret, access_token, access_token_secret): # all of this is encased in a try block to handle the case where this # account is either locked or suspended try: self.auth = tweepy.OAuthHandler(consumer_key, consumer_secret) self.auth.set_access_token(access_token, access_token_secret) self.api = tweepy.API(self.auth, compression=True) self.me = self.api.me() self.bio_ok = cfg('twitter:bio_ok') self.bio_ratelimit = cfg('twitter:bio_ratelimit') except tweepy.error.TweepError as exc: Twitter.handle_exception(exc) raise utils.logger.info('Twitter API initialised.')
def retweet_promo_tweet(): promo_tweet_id = cfg('twitter:promo_tweet_id:int') try: tweet = twitter.api.get_status(promo_tweet_id, include_my_retweet=True) except tweepy.error.TweepError: utils.logger.error('Error retrieving info about the promo tweet.') raise try: current_rt_id = tweet.current_user_retweet['id'] utils.logger.info('Undoing previous retweet of the promo tweet.') twitter.api.destroy_status(current_rt_id) except AttributeError: # this happens when there was no retweet to undo pass except tweepy.error.TweepError: utils.logger.error('Error undoing the retweet.') raise # if there was an error undoing the retweet, give up try: twitter.api.retweet(promo_tweet_id) except tweepy.error.TweepError: utils.logger.error('Error retweeting the promo tweet.') raise utils.logger.info('Promo tweet retweeted.')
def test_config(): def dumy(config): assert_that(config.defaults).contains_entry({ 'db': 'service1' }).contains_entry({'db_port': '232'}) dumy(cfg(os.path.join(test_root(), 'get_data_file', 'db.cfg')))
def edit_config(): user = g.user if not is_admin_mode(user): return render('edit_config.html', authn_error=True) def get_rows(textarea): return len(textarea.split("\n")) * 1.2 def format_json(json_string): return json.dumps(json_string, indent=4, sort_keys=True, separators=(",", " : ")) config = format_json(cfg()) if request.method == 'POST': action = request.form["action"] if action == "save": config = request.form["config"] try: tmp = json.loads(config) config = format_json(tmp) except ValueError, ve: return render('edit_config.html', config=config, rows=get_rows(config), error="configuration syntax error.") with open("config.json", "w") as f: f.write(config) f.flush() elif action == "revert": pass else: raise Exception("Unknown config editing action: \"%s\"" %action)
def edit_config(): user = g.user if not is_admin_mode(user): return render('edit_config.html', authn_error=True) def get_rows(textarea): return len(textarea.split("\n")) * 1.2 def format_json(json_string): return json.dumps(json_string, indent=4, sort_keys=True, separators=(",", " : ")) config = format_json(cfg()) if request.method == 'POST': action = request.form["action"] if action == "save": config = request.form["config"] try: tmp = json.loads(config) config = format_json(tmp) except ValueError, ve: return render('edit_config.html', config=config, rows=get_rows(config), error="configuration syntax error.") with open("config.json", "w") as f: f.write(config) f.flush() elif action == "revert": pass else: raise Exception("Unknown config editing action: \"%s\"" % action)
def start_sequence(): files = Popen("find | grep ngrok", stdout=PIPE, shell=True).communicate()[0].decode() files = files.split("\n") nfiles = [] for i in files: parts = i.split("/") for i in parts: nfiles.append(i) if not 'ngrok' in nfiles: # cuz ngrok.py if mch in arm: ver = "arm" elif mch in x86: ver = "32" elif mch in x64: ver = "64" else: ver = config.cfg().ver if ver == "64": ngrok.download_64bit() elif ver == "32": ngrok.download_32bit() elif ver == "arm": ngrok.download_arm() else: print(fail("Incorrect OS version in config.py, using 64bit")) ngrok.download_64bit() print_banner()
def data_gen(all_objs): ''' generate data for training input: all_objs -- all objects used for training batch_size -- number of images used for training at once yield: x_batch -- (batch_size, 224, 224, 3), input images to training process at each batch d_batch -- (batch_size, 3), object dimensions o_batch -- (batch_size, 2, 2), object orientation c_batch -- (batch_size, 2), angle confidence ''' num_obj = len(all_objs) keys = list(range(num_obj)) np.random.shuffle(keys) l_bound = 0 r_bound = cfg().batch_size if cfg().batch_size < num_obj else num_obj while True: if l_bound == r_bound: l_bound = 0 r_bound = cfg( ).batch_size if cfg().batch_size < num_obj else num_obj np.random.shuffle(keys) currt_inst = 0 x_batch = np.zeros((r_bound - l_bound, cfg().norm_h, cfg().norm_w, 3)) d_batch = np.zeros((r_bound - l_bound, 3)) o_batch = np.zeros((r_bound - l_bound, cfg().bin, 2)) c_batch = np.zeros((r_bound - l_bound, cfg().bin)) for key in keys[l_bound:r_bound]: # augment input image and fix object's orientation and confidence image, dimension, orientation, confidence = prepare_input_and_output( all_objs[key], all_objs[key]['image'], ) x_batch[currt_inst, :] = image d_batch[currt_inst, :] = dimension o_batch[currt_inst, :] = orientation c_batch[currt_inst, :] = confidence currt_inst += 1 yield x_batch, [d_batch, o_batch, c_batch] l_bound = r_bound r_bound = r_bound + cfg().batch_size if r_bound > num_obj: r_bound = num_obj
def process_timeline(): """retrieves all tweets in the home timeline and then stores them in pending.txt""" params = dict(count=200) sources_whitelist = cfg('twitter:sources_whitelist:list') try: with open('state_home_timeline.txt') as fp: since_id = int(fp.read()) utils.logging.info('State: since_id=%d', since_id) params['since_id'] = since_id except Exception as exc: utils.logging.exception("Couldn't figure what the last tweet was, " "so I'm starting from the beginning.") filtered_statuses = [] statuses = [ status for page in tweepy.Cursor(twitter.api.home_timeline, **params).pages() for status in page ] # they are in reverse chronological order, so put them straight statuses = statuses[::-1] for status in statuses: # ignore yourself if status.author.screen_name == twitter.me.screen_name: continue # ignore mentions if status.text.startswith('@'): continue # ignore retweets if hasattr(status, 'retweeted_status'): continue # if the sources whitelist is enabled, ignore those who aren't on it if (sources_whitelist and status.source not in sources_whitelist): continue text = utils.clean(status.text, urls=True, replies=True, rts=True) if not text: continue # store this status to score it later in akari_cron filtered_statuses.append(status) if filtered_statuses: with open('pending.txt', 'a') as p_file: for status in filtered_statuses: text_no_nl = utils.clean(status.text) print('%d %s' % (status.id, text_no_nl), file=p_file) utils.logging.info('Retrieved %d new statuses (from %d to %d).', len(filtered_statuses), filtered_statuses[0].id, filtered_statuses[-1].id) with open('state_home_timeline.txt', 'wt') as fp: fp.write(str(filtered_statuses[-1].id)) else: utils.logging.info('Retrieved no new statuses.')
def render(*args, **kwargs): global g user = g.user return render_template(*args, debug=cfg()["debug_mode_enabled"], admin_mode=is_admin_mode(user), user=user, **kwargs)
def akari_publish(text, **kwargs): from twitter import twitter akari = Akari(text, **kwargs) if cfg('twitter:text_in_status:bool'): twitter.post(status=akari.caption, media=akari.filename) else: twitter.post(media=akari.filename)
def show_video(path): global g user = g.user if user is None and not cfg()["public_access"]: return render("video.html", authn_error="only logged in users may view this page") video_path = path.encode("utf-8") path = os.path.dirname(video_path) check_jailed_path(path, "data") return render("video.html", video_path = video_path, route=get_route(video_path)[1:-1], video_basename = os.path.basename(video_path))
def __init__(self): self.engine = create_engine(cfg('database', 'connection_string'), pool_recycle=3600, echo=True, pool_size=10, max_overflow=20, pool_timeout=30) self.DBSession = scoping.scoped_session( sessionmaker(bind=self.engine, autocommit=False))
def __init__(self, subset='training'): super(KITTILoader, self).__init__() self.base_dir = cfg().base_dir self.KITTI_cat = cfg().KITTI_cat label_dir = os.path.join(self.base_dir, subset, 'label_2') image_dir = os.path.join(self.base_dir, subset, 'image_2') self.image_data = [] self.images = [] for i, fn in enumerate(os.listdir(label_dir)): label_full_path = os.path.join(label_dir, fn) image_full_path = os.path.join(image_dir, fn.replace('.txt', '.png')) self.images.append(image_full_path) fieldnames = ['type', 'truncated', 'occluded', 'alpha', 'xmin', 'ymin', 'xmax', 'ymax', 'dh', 'dw', 'dl', 'lx', 'ly', 'lz', 'ry'] with open(label_full_path, 'r') as csv_file: reader = csv.DictReader(csv_file, delimiter=' ', fieldnames=fieldnames) for line, row in enumerate(reader): if row['type'] in self.KITTI_cat: if subset == 'training': new_alpha = get_new_alpha(row['alpha']) dimensions = np.array([float(row['dh']), float(row['dw']), float(row['dl'])]) annotation = {'name': row['type'], 'image': image_full_path, 'xmin': int(float(row['xmin'])), 'ymin': int(float(row['ymin'])), 'xmax': int(float(row['xmax'])), 'ymax': int(float(row['ymax'])), 'dims': dimensions, 'new_alpha': new_alpha} elif subset == 'eval': dimensions = np.array([float(row['dh']), float(row['dw']), float(row['dl'])]) translations = np.array([float(row['lx']), float(row['ly']), float(row['lz'])]) annotation = {'name': row['type'], 'image': image_full_path, 'alpha': float(row['alpha']), 'xmin': int(float(row['xmin'])), 'ymin': int(float(row['ymin'])), 'xmax': int(float(row['xmax'])), 'ymax': int(float(row['ymax'])), 'dims': dimensions, 'trans': translations, 'rot_y': float(row['ry'])} self.image_data.append(annotation)
def network(): vgg16_model = VGG16(include_top=False, weights='imagenet', input_shape=(224, 224, 3)) for layer in vgg16_model.layers: layer.trainable = False for layer in vgg16_model.layers: print(layer, layer.trainable) x = Flatten()(vgg16_model.output) # Dimensions branch dimensions = Dense(512)(x) dimensions = LeakyReLU(alpha=0.1)(dimensions) dimensions = Dropout(0.5)(dimensions) dimensions = Dense(3, name='dimensions')(dimensions) # # Dimensions branch # dimensions = Dense(512)(x) # dimensions = LeakyReLU(alpha=0.1)(dimensions) # dimensions = Dropout(0.5)(dimensions) # dimensions = Dense(3)(dimensions) # dimensions = LeakyReLU(alpha=0.1, name='dimensions')(dimensions) orientation = Dense(256)(x) orientation = LeakyReLU(alpha=0.1)(orientation) orientation = Dropout(0.5)(orientation) orientation = Dense(cfg().bin * 2)(orientation) #orientation = LeakyReLU(alpha=0.1)(orientation) orientation = Reshape((cfg().bin, -1))(orientation) orientation = Lambda(l2_normalize, name='orientation')(orientation) confidence = Dense(256)(x) confidence = LeakyReLU(alpha=0.1)(confidence) confidence = Dropout(0.5)(confidence) confidence = Dense(cfg().bin, activation='softmax', name='confidence')(confidence) model = Model(vgg16_model.input, [dimensions, orientation, confidence]) model.summary() return model
def show_data(path): global g user = g.user if user is None and not cfg()["public_access"]: abort(401) path = path.encode("utf-8") if os.path.basename(path) == ".access": abort(401) check_jailed_path(path, "data") return send_file(path)
def determine_paths(self): tv_path = Path(cfg().path("tv")) matches = [] for sub_path in tv_path.iterdir(): if not sub_path.is_dir(): continue path_parts = sub_path.name.lower().split(" ") if all([x.lower() in path_parts for x in self.args]): matches.append(sub_path) return matches
def on_post(self, req, resp): username = req.get_json('username') password = req.get_json('password') dbsession = self.db.session user = dbsession.query(User).filter(User.username == username).first() if user is None or not bcrypt.checkpw( bytes(password, encoding='UTF-8'), bytes(user.password, encoding='UTF-8') ): raise falcon.HTTPUnauthorized(title='Unauthorized', description='Authentication failed!!!') token = jwt.encode(dict( user_id=user.id, exp=datetime.utcnow() + timedelta(seconds=int(cfg('app', 'expired_after'))) ), cfg('app', 'secret'), algorithm='HS256') resp.json = dict( id=user.id, username=user.username, token=token.decode('UTF-8') )
def download(self): self.filename = self.get_path('original') if os.path.isfile(self.filename): utils.logger.info('Returning cached file "%s".', self.image_url) return try: utils.logger.info('Downloading image "%s" from "%s"', self.image_url, self.source_url) headers = { 'Accept': '*/*', 'User-Agent': cfg('image_search:user_agent'), 'Referer': self.source_url } response = requests.get(self.image_url, headers=headers, timeout=5) except (requests.exceptions.RequestException, socket.timeout): # if the download times out, try with the next result raise ImageSearchResultError('Timed out') # if the download fails (404, ...), try with the next result if response.status_code != requests.codes.ok: raise ImageSearchResultError('Download of image failed') # store the image utils.logger.info('Saving image to "%s"', self.filename) with open(self.filename, 'wb') as handle: for block in response.iter_content(1024 * 1024): if not block: break handle.write(block) # and a metadata file to know where it came from metafile = self.get_path('meta') with open(metafile, 'w') as fp: print('url: %s' % self.image_url, file=fp) print('source: %s' % self.source_url, file=fp) print('query: %s' % self.text, file=fp) # check the size of the image before loading it into memory if os.stat(self.filename).st_size > 25 * 1024 * 1024: raise ImageSearchResultError('Image too big') # try to get Wand to load it as an image. if that doesn't work, raise # an exception so that we try with the next result try: image = Image(filename=self.filename) except Exception: raise ImageSearchResultError('Not an image') else: image.destroy() utils.logger.info('Complete')
def show_video(path): global g user = g.user if user is None and not cfg()["public_access"]: return render("video.html", authn_error="only logged in users may view this page") video_path = path.encode("utf-8") path = os.path.dirname(video_path) check_jailed_path(path, "data") return render("video.html", video_path=video_path, route=get_route(video_path)[1:-1], video_basename=os.path.basename(video_path))
def __init__(self): self._config = cfg() self.script_path = os.path.dirname(os.path.realpath(__file__)) self._db_file = self._config.get_setting("path", "tvdb") self._loaded_db = None self._load_db() self._show_list = [] self._key_list = [] if self._loaded_db is not None and self._loaded_db: for show in self._loaded_db.keys(): self._show_list.append(show) for key in self._loaded_db[self._show_list[0]].keys(): self._key_list.append(key)
def orientation_confidence_flip(image_data, dims_avg): for data in image_data: # minus the average dimensions data['dims'] = data['dims'] - dims_avg[data['name']] # fix orientation and confidence for no flip orientation = np.zeros((cfg().bin, 2)) confidence = np.zeros(cfg().bin) anchors = compute_anchors(data['new_alpha']) for anchor in anchors: # each angle is represented in sin and cos orientation[anchor[0]] = np.array([np.cos(anchor[1]), np.sin(anchor[1])]) confidence[anchor[0]] = 1 confidence = confidence / np.sum(confidence) data['orient'] = orientation data['conf'] = confidence # Fix orientation and confidence for random flip orientation = np.zeros((cfg().bin, 2)) confidence = np.zeros(cfg().bin) anchors = compute_anchors(2. * np.pi - data['new_alpha']) # compute orientation and bin # for flipped images for anchor in anchors: orientation[anchor[0]] = np.array([np.cos(anchor[1]), np.sin(anchor[1])]) confidence[anchor[0]] = 1 confidence = confidence / np.sum(confidence) data['orient_flipped'] = orientation data['conf_flipped'] = confidence return image_data
def network(): inputs = layers.Input(shape=(cfg().norm_h, cfg().norm_w, 3)) x = _conv_block(inputs, 32, (3, 3), strides=(2, 2)) x = _inverted_residual_block(x, 16, (3, 3), t=1, strides=1, n=1) x = _inverted_residual_block(x, 24, (3, 3), t=6, strides=2, n=2) x = _inverted_residual_block(x, 32, (3, 3), t=6, strides=2, n=3) x = _inverted_residual_block(x, 64, (3, 3), t=6, strides=2, n=4) x = _inverted_residual_block(x, 96, (3, 3), t=6, strides=1, n=3) x = _inverted_residual_block(x, 160, (3, 3), t=6, strides=2, n=3) x = _inverted_residual_block(x, 320, (3, 3), t=6, strides=1, n=1) x = _conv_block(x, 1280, (1, 1), strides=(1, 1)) x = layers.GlobalAveragePooling2D()(x) x = layers.Reshape((1, 1, 1280))(x) x = layers.Dropout(0.3, name='Dropout')(x) # Dimensions branch dimensions = layers.Conv2D(3, (1, 1), padding='same', name='d_conv')(x) dimensions = layers.Reshape((3, ), name='dimensions')(dimensions) # Orientation branch orientation = layers.Conv2D(4, (1, 1), padding='same', name='o_conv')(x) orientation = layers.Reshape((cfg().bin, -1))(orientation) orientation = layers.Lambda(l2_normalize, name='orientation')(orientation) # Confidence branch confidence = layers.Conv2D(cfg().bin, (1, 1), padding='same', name='c_conv')(x) confidence = layers.Activation('softmax', name='softmax')(confidence) confidence = layers.Reshape((2, ), name='confidence')(confidence) # Build model model = tf.keras.Model(inputs, [dimensions, orientation, confidence]) model.summary() return model
def create_profile(): """If this is the user's first login, the create_or_login function will redirect here so that the user can set up his profile. """ if g.user is not None or 'openid' not in session: return redirect(url_for('index')) if not cfg()["profile_creation_enabled"]: return render('create_profile.html', authn_error="profile creation has been disabled by the administrator.") if request.method == 'POST': name = request.form['name'] email = request.form['email'] if not name: flash(u'Error: you have to provide a name') elif '@' not in email: flash(u'Error: you have to enter a valid email address') else: flash(u'Profile successfully created') groups = [] if user_list.total() == 0: groups.append("administrators") user_list.add_new(name, email, session['openid'], groups) return redirect(oid.get_next_url()) return render('create_profile.html', next_url=oid.get_next_url())
path = os.path.dirname(video_path) check_jailed_path(path, "data") return render("video.html", video_path = video_path, route=get_route(video_path)[1:-1], video_basename = os.path.basename(video_path)) def check_jailed_path(path, jail_path): if os.path.normpath(path) == jail_path: return if not os.path.normpath(path).startswith(jail_path + os.sep): raise Exception("Permission denied, '%s' is outside '%s'" %(path, jail_path)) @app.route('/data/<path:path>') def show_data(path): global g user = g.user if user is None and not cfg()["public_access"]: abort(401) path = path.encode("utf-8") if os.path.basename(path) == ".access": abort(401) check_jailed_path(path, "data") return send_file(path) @app.route('/static/<path:path>') def show_static(path): path = os.path.join("static", path.encode("utf-8")) check_jailed_path(path, "static") return send_file(path) if __name__ == '__main__': app.run(debug=cfg()["debug_mode_enabled"], host="0.0.0.0")
from stats import Stats class UnsupportedFormatError(Exception): pass def recursive_populate(path, log_freq, follow_freedektop_standard): try: gallery = Gallery(path, log_freq, follow_freedektop_standard) gallery.populate() stats = gallery.stats.clone() for g in gallery.gallery_paths: subgallery, substats = recursive_populate(os.path.join(gallery.path, g), log_freq, follow_freedektop_standard) if substats is None: break else: stats.increase(substats) except KeyboardInterrupt: print "\n%sProcess interrupted by user!%s" %(Color.RED, Color.RESET) return None, None return gallery, stats log_freq = 5 g, stats = recursive_populate(path="data", log_freq=log_freq, follow_freedektop_standard = cfg()["follow_freedesktop_standard"]) if log_freq > 0: sys.stdout.write("\n") print "Total stats: %s" %stats if len(stats.failed_thumbs) > 0: print "There were errors when generating thumbnails for the following files:" for fail in stats.failed_thumbs: print " %s* %s: %s%s" %(Color.RED, fail[0], fail[1], Color.RESET)
#!/usr/bin/env python import sys sys.path.insert(0, '/etc/vmc') from config import vmcConfig as cfg import os import re import subprocess import sys from xml.dom.minidom import parse cfg_path = cfg().libvirt_domain_dir def getvm_cfg_files(cfg_path): files = os.listdir(cfg_path) vms = [] for f in files: if re.match(r'.*\.xml$', f): vms.append(f) return vms def getvm_name_from_cfg_file(file): return parse(file).getElementsByTagName('name')[0].firstChild.nodeValue def getvm_disk_from_cfg_file(file): disks = [] for d in parse(file).getElementsByTagName('disk'):
def __init__(self): self.config = cfg() self.module = mod()