def get_pbar(num, prefix=""):
assert isinstance(prefix, str)
pbar = pb.ProgressBar(widgets=[prefix, pb.Percentage(), pb.Bar(), pb.ETA()], maxval=num)
return pbar
python类Bar()的实例源码
def __init__(self, nbytes, nfiles):
self._total_bytes = nbytes
self._pending_files = nfiles
self._transferring_files = 0
self._complete_files = 0
self._lock = Lock()
self._data = {}
widgets = ['Progress: ', Percentage(), ' ', Bar(left='[',right=']'),
' ', Timer(format='Time: %s'), ' ', FileTransferSpeed()]
if self._total_bytes > 0:
self.pbar = ProgressBar(widgets=widgets, maxval=self._total_bytes).start()
else:
self.pbar = ProgressBar(widgets=widgets, maxval=nfiles).start()
def collect_mailinfos(server, folder_contents, outpath_format):
#construct progressbar
progressbar_widgets = [
'[Choosing mails for download ] ',
progressbar.Percentage(),
progressbar.Bar(marker=progressbar.RotatingMarker()), ' ', progressbar.ETA()]
total_count = 0
for folder, mailids in folder_contents.items():
total_count += len(mailids)
progressbar_instance = progressbar.ProgressBar(widgets=progressbar_widgets, maxval=total_count).start()
#collect all mailinfos
mailinfos = {}
mailinfo_count = 0
for folder, mailids in folder_contents.items():
mailinfos[folder] = []
#get mailinfo bit by bit
server.select_folder(folder, readonly=True)
for mailid in mailids:
#fetch mail information
mailinfo = server.fetch([mailid], ['ENVELOPE', 'INTERNALDATE', 'RFC822.SIZE'])[mailid]
mailsize = bitmath.Byte(mailinfo[b'RFC822.SIZE'])
mailfilename = construct_mailfilename(outpath_format, mailinfo, args.outdir, folder, mailid)
#only add if mailfilename can be constructed
if mailfilename:
mailinfos[folder].append((mailid, mailfilename, mailsize))
mailinfo_count += 1
progressbar_instance.update(mailinfo_count)
progressbar_instance.finish()
return mailinfos
def cleanup(stored_files, stored_dirs, download_list, outdir):
#create list of files to keep
keep_list = []
for folder, mails in download_list.items():
for mailid, mailfilename, mailsize in mails:
keep_list.append(mailfilename)
progressbar_widgets = [
'[Cleaning up outdir ] ',
progressbar.Percentage(),
progressbar.Bar(marker=progressbar.RotatingMarker()), ' ']
progressbar_instance = progressbar.ProgressBar(widgets=progressbar_widgets, maxval=len(stored_files)).start()
file_count = 0
#delete all files we don't need to keep
for file in stored_files:
#delete if not on server
if not file in keep_list:
os.remove(file)
#progressbar
file_count += 1
progressbar_instance.update(file_count)
progressbar_instance.finish()
#remove empty folders
possible_empty_folders = True
while possible_empty_folders:
#find all subfolders
stored_dirs = []
for root, dirs, files in os.walk(outdir):
for name in dirs:
stored_dirs.append(os.path.join(root, name))
#delete empty folders indicate next run if one folder was deleted
possible_empty_folders = False
for folder in stored_dirs:
if not os.listdir(folder):
shutil.rmtree(folder)
possible_empty_folders = True
def create_app(load_db=True, populate_qr_cache=True, progressbar=False):
# Set up logging
log_level = os.environ.get('AF_LOGGING_LEVEL', None)
if log_level is not None:
log_levels = ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL')
if log_level.upper() in log_levels:
log_level = getattr(log, log_level)
log.basicConfig(level=log_level)
else:
log.warning('Invalid log level: {}'.format(log_level.upper()))
else:
log.warning('No log level set, using default level.')
log.info('Creating Flask application')
app = Flask(__name__)
app.register_blueprint(root)
# Now load the database if requested
if load_db:
from . import database_handler as dh
log.info('Loading database.')
dh.get_database() # This loads the database into memory.
log.info('Database loaded.')
if populate_qr_cache:
if progressbar:
from progressbar import ProgressBar, Bar, Timer, ETA
pbar = ProgressBar(widgets=['Populating QR cache: ', Bar(),
' ', Timer(), ' ', ETA()])
kwargs = {'pbar': pbar}
else:
log.info('Populating QR cache.')
kwargs = {}
from .cache_utils import populate_qr_cache
populate_qr_cache(**kwargs)
return app
def __init__(self, msg, maxval, widgets=None, extrapos=-1):
self.msg = msg
self.extrapos = extrapos
if not widgets:
widgets = [progressbar.Percentage(), ' ', progressbar.Bar(), ' ',
progressbar.ETA()]
self.extrapos = 4
try:
self._resize_default = signal.getsignal(signal.SIGWINCH)
except:
self._resize_default = None
progressbar.ProgressBar.__init__(self, maxval, [self.msg + ": "] + widgets, fd=sys.stdout)
def getCurrentStatus(self, filename_detections):
pbar = pb.ProgressBar(maxval=len(self._seq.data), widgets=['Loading last status', pb.Percentage(), pb.Bar()])
pbar.start()
cache_str = ''
with open(filename_detections, "r") as inputfile:
cache_str = inputfile.readlines()
for i in xrange(len(self._seq.data)):
pbar.update(i)
if len(self.subset_idxs) > 0:
if i not in self.subset_idxs:
break
hd = HandDetector(numpy.zeros((1, 1)), 0., 0.) # dummy object
com = numpy.asarray(hd.detectFromCache(filename_detections, self._seq.data[i].fileName, cache_str))
if numpy.allclose(com[2], 0.):
self.curFrame = i
break
else:
self._seq.data[i] = self._seq.data[i]._replace(com=self.importer.jointImgTo3D(com.reshape((3,))))
# redo last pose, it might be set to default and saved
if self.curFrame > 0:
if len(self.subset_idxs) > 0:
if self.subset_idxs.index(self.curFrame) - 1 >= 0:
self.curFrame = self.subset_idxs[self.subset_idxs.index(self.curFrame) - 1]
else:
self.curFrame -= 1
def saveVideo3D(self, filename, sequence, showPC=True, showGT=False, niceColors=True, plotFrameNumbers=False,
height=400, width=400):
"""
Create a video with 3D annotations
:param filename: name of file to save
:param sequence: sequence data
:return: None
"""
txt = 'Saving {}'.format(filename)
pbar = pb.ProgressBar(maxval=self.joints.shape[0], widgets=[txt, pb.Percentage(), pb.Bar()])
pbar.start()
# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC(*'DIVX')
video = cv2.VideoWriter('{}/depth_{}.avi'.format(self.subfolder, filename), fourcc, self.fps, (height, width))
if not video:
raise EnvironmentError("Error in creating video writer")
for i in range(self.joints.shape[0]):
jt = self.joints[i]
img = self.plotResult3D_OS(sequence.data[i].dpt, sequence.data[i].T, sequence.data[i].gt3Dorig, jt,
showPC=showPC, showGT=showGT, niceColors=niceColors, width=width, height=height)
img = numpy.flipud(img)
img = img[:, :, [2, 1, 0]] # change color channels for OpenCV
img = cv2.resize(img, (height, width))
if plotFrameNumbers:
if sequence.data[i].subSeqName == 'ref':
cv2.putText(img, "Reference Frame {}".format(i), (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255))
# plot frame number
cv2.putText(img, "{}".format(i), (height-50, width-10), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255))
# write frame
video.write(img)
pbar.update(i)
video.release()
del video
cv2.destroyAllWindows()
pbar.finish()
def saveVideoFrames(self, filename, images):
"""
Create a video with synthesized images
:param filename: name of file to save
:param images: video data
:return: None
"""
txt = 'Saving {}'.format(filename)
pbar = pb.ProgressBar(maxval=images.shape[0], widgets=[txt, pb.Percentage(), pb.Bar()])
pbar.start()
height = width = 128
# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC(*'DIVX')
video = cv2.VideoWriter('{}/synth_{}.avi'.format(self.subfolder, filename), fourcc, self.fps, (height, width))
if not video:
raise EnvironmentError("Error in creating video writer")
for i in range(images.shape[0]):
img = images[i]
img = cv2.normalize(img, alpha=0, beta=255, norm_type=cv2.cv.CV_MINMAX, dtype=cv2.cv.CV_8UC1)
img = cv2.cvtColor(img, cv2.cv.CV_GRAY2BGR)
img = cv2.resize(img, (height, width))
# write frame
video.write(img)
pbar.update(i)
video.release()
del video
cv2.destroyAllWindows()
pbar.finish()
def print_status_stream(title, stream):
widgets = [title, FormatLabel(''), ' ', Percentage(), ' ', Bar(), ' ', RotatingMarker()]
bar = None
if sys.stderr.isatty():
bar = progressbar.ProgressBar(widgets=widgets, max_value=255)
def print_error(status):
print(status['error'])
def print_status(status):
progress = status.get('progressDetail')
if progress:
widgets[1] = FormatLabel("%12s" % (status['status']))
prog = int(round(255 * ((progress['current'] / progress['total']))))
if bar is not None:
bar.update(prog)
def print_unknown(status):
print(status)
for line in stream:
status = json.loads(line.decode('utf8'))
if 'error' in status:
print_error(status)
elif 'status' in status:
print_status(status)
else:
print_unknown(status)
download.py 文件源码
项目:Activation-Visualization-Histogram
作者: shaohua0116
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def prepare_h5py(train_image, train_label, test_image,
test_label, data_dir, shape=None):
image = np.concatenate((train_image, test_image), axis=0).astype(np.uint8)
label = np.concatenate((train_label, test_label), axis=0).astype(np.uint8)
print('Preprocessing data...')
import progressbar
bar = progressbar.ProgressBar(
maxval=100, widgets=[progressbar.Bar('=', '[', ']'),
' ', progressbar.Percentage()]
)
bar.start()
f = h5py.File(os.path.join(data_dir, 'data.hy'), 'w')
with open(os.path.join(data_dir, 'id.txt'), 'w') as data_id:
for i in range(image.shape[0]):
if i % (image.shape[0] / 100) == 0:
bar.update(i / (image.shape[0] / 100))
grp = f.create_group(str(i))
data_id.write('{}\n'.format(i))
if shape:
grp['image'] = np.reshape(image[i], shape, order='F')
else:
grp['image'] = image[i]
label_vec = np.zeros(10)
label_vec[label[i] % 10] = 1
grp['label'] = label_vec.astype(np.bool)
bar.finish()
f.close()
return
def __init__(self):
super().__init__()
self.label = Label()
# Got messes with sys.stdout and sys.stderr in ways that confuse progressbar and cause it to output on the wrong one
# This can be worked around by passing in a new stream, but that stream can't be the same instance as sys.stdout or sys.stderr, so I make a new one here that forwards everything
class StreamWrapper:
def __getattr__(self, k):
return getattr(sys.stdout, k)
self.bar = progressbar.ProgressBar(fd = StreamWrapper(), widgets = [self.label, ' ', progressbar.Bar(), ' ', progressbar.Percentage(), ' '])
style_transfer.py 文件源码
项目:deepdream-neural-style-transfer
作者: rdcolema
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def _create_pbar(self, max_iter):
"""
Creates a progress bar.
"""
self.grad_iter = 0
self.pbar = pb.ProgressBar()
self.pbar.widgets = ["Optimizing: ", pb.Percentage(),
" ", pb.Bar(marker=pb.AnimatedMarker()),
" ", pb.ETA()]
self.pbar.maxval = max_iter
def convert_dataset(indices, name):
# Open a TFRRecordWriter
filename = os.path.join(FLAGS.out, name + '.tfrecords')
writeOpts = tf.python_io.TFRecordOptions(tf.python_io.TFRecordCompressionType.ZLIB)
writer = tf.python_io.TFRecordWriter(filename, options=writeOpts)
# Load each data sample (image_a, image_b, flow) and write it to the TFRecord
count = 0
pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval=len(indices)).start()
for i in indices:
image_a_path = os.path.join(FLAGS.data_dir, '%05d_img1.ppm' % (i + 1))
image_b_path = os.path.join(FLAGS.data_dir, '%05d_img2.ppm' % (i + 1))
flow_path = os.path.join(FLAGS.data_dir, '%05d_flow.flo' % (i + 1))
image_a = imread(image_a_path)
image_b = imread(image_b_path)
# Convert from RGB -> BGR
image_a = image_a[..., [2, 1, 0]]
image_b = image_b[..., [2, 1, 0]]
# Scale from [0, 255] -> [0.0, 1.0]
image_a = image_a / 255.0
image_b = image_b / 255.0
image_a_raw = image_a.tostring()
image_b_raw = image_b.tostring()
flow_raw = open_flo_file(flow_path).tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'image_a': _bytes_feature(image_a_raw),
'image_b': _bytes_feature(image_b_raw),
'flow': _bytes_feature(flow_raw)}))
writer.write(example.SerializeToString())
pbar.update(count + 1)
count += 1
writer.close()
def setup_progressbar(self):
from progressbar import ProgressBar, Bar, Percentage
return ProgressBar(widgets=[Bar(), ' ', Percentage()])
def setup_progressbar(self):
from progressbar import ProgressBar, FileTransferSpeed, Bar, Percentage, ETA
return ProgressBar(widgets=[FileTransferSpeed(), ' <<<', Bar(), '>>> ', Percentage(), ' ', ETA()])
def __init__(self, widgets=None, **kwargs):
import progressbar as pb
logging.Handler.__init__(self)
if widgets is None:
class CommaProgress(pb.widgets.WidgetBase):
def __call__(self, progress, data):
return '{value:,} of {max_value:,}'.format(**data)
widgets = [' ', CommaProgress(), ' (', pb.Percentage(), ') ',
pb.Bar(), ' ', pb.ETA()]
self.pbar_args = {'widgets': widgets}
self.pbar_args.update(kwargs)
def create_progress_bar(message):
widgets = [
message,
progressbar.Counter(),
' ',
progressbar.Percentage(),
' ',
progressbar.Bar(),
progressbar.AdaptiveETA()
]
pbar = progressbar.ProgressBar(widgets=widgets)
return pbar
def _get_progress_widgets(self):
"""
Returns the progress widgets for a file download.
"""
format_custom_text = progressbar.FormatCustomText(
'Fetching [ %(file)s ] :: ', dict(file=self.remote_file_name),
)
widgets = [
format_custom_text,
progressbar.ETA(),
progressbar.Percentage(),
progressbar.Bar(),
]
return widgets
def transfer(self):
image_reshape = np.ndarray(shape=(self.pre_images.shape[0], self.output_rows, self.output_cols, 3),
dtype=np.float16)
widgets = ['Transfer: ', pbar.Percentage(), ' ', pbar.Bar('>'), ' ', pbar.ETA()]
image_bar = pbar.ProgressBar(widgets=widgets, maxval=self.pre_images.shape[0]).start()
for i in range(0, self.pre_images.shape[0]):
image = self.pre_images[i].reshape(self.pre_img_rows, self.pre_img_cols)
image = image.astype('uint8')
im = Image.fromarray(image) # monochromatic image
imrgb = im.convert('RGB')
imrgb = imrgb.resize((self.output_rows, self.output_cols), Image.ANTIALIAS)
im = np.array(imrgb, dtype=np.float16)
im[:, :, 0] -= imagenet_mean['R']
im[:, :, 1] -= imagenet_mean['G']
im[:, :, 2] -= imagenet_mean['B']
# 'RGB'->'BGR', historical reasons in OpenCV
im = im[:, :, ::-1]
image_reshape[i] = im
# test for correct convert!
# if i < 3:
# img = Image.fromarray(np.uint8(im))
# img.save(str(i) + '.jpeg', 'jpeg')
image_bar.update(i + 1)
image_bar.finish()
print('image_reshape:', image_reshape.shape)
return image_reshape