Python numpy.fromstring() Examples
The following are 30 code examples for showing how to use numpy.fromstring(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example 1
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: captcha_generator.py License: Apache License 2.0 | 8 votes |
def image(self, captcha_str): """ Generate a greyscale captcha image representing number string Parameters ---------- captcha_str: str string a characters for captcha image Returns ------- numpy.ndarray Generated greyscale image in np.ndarray float type with values normalized to [0, 1] """ img = self.captcha.generate(captcha_str) img = np.fromstring(img.getvalue(), dtype='uint8') img = cv2.imdecode(img, cv2.IMREAD_GRAYSCALE) img = cv2.resize(img, (self.h, self.w)) img = img.transpose(1, 0) img = np.multiply(img, 1 / 255.0) return img
Example 2
Project: kaldi-python-io Author: funcwj File: _io_kernel.py License: Apache License 2.0 | 6 votes |
def read_common_mat(fd): """ Read common matrix(for class Matrix in kaldi setup) see matrix/kaldi-matrix.cc:: void Matrix<Real>::Read(std::istream & is, bool binary, bool add) Return a numpy ndarray object """ mat_type = read_token(fd) print_info(f'\tType of the common matrix: {mat_type}') if mat_type not in ["FM", "DM"]: raise RuntimeError(f"Unknown matrix type in kaldi: {mat_type}") float_size = 4 if mat_type == 'FM' else 8 float_type = np.float32 if mat_type == 'FM' else np.float64 num_rows = read_int32(fd) num_cols = read_int32(fd) print_info(f'\tSize of the common matrix: {num_rows} x {num_cols}') mat_data = fd.read(float_size * num_cols * num_rows) mat = np.fromstring(mat_data, dtype=float_type) return mat.reshape(num_rows, num_cols)
Example 3
Project: kaldi-python-io Author: funcwj File: _io_kernel.py License: Apache License 2.0 | 6 votes |
def read_float_vec(fd, direct_access=False): """ Read float vector(for class Vector in kaldi setup) see matrix/kaldi-vector.cc """ if direct_access: expect_binary(fd) vec_type = read_token(fd) print_info(f'\tType of the common vector: {vec_type}') if vec_type not in ["FV", "DV"]: raise RuntimeError(f"Unknown matrix type in kaldi: {vec_type}") float_size = 4 if vec_type == 'FV' else 8 float_type = np.float32 if vec_type == 'FV' else np.float64 dim = read_int32(fd) print_info(f'\tDim of the common vector: {dim}') vec_data = fd.read(float_size * dim) return np.fromstring(vec_data, dtype=float_type)
Example 4
Project: dustmaps Author: gregreen File: json_serializers.py License: GNU General Public License v2.0 | 6 votes |
def deserialize_ndarray(d): """ Deserializes a JSONified :obj:`numpy.ndarray`. Can handle arrays serialized using any of the methods in this module: :obj:`"npy"`, :obj:`"b64"`, :obj:`"readable"`. Args: d (`dict`): A dictionary representation of an :obj:`ndarray` object. Returns: An :obj:`ndarray` object. """ if 'data' in d: x = np.fromstring( base64.b64decode(d['data']), dtype=d['dtype']) x.shape = d['shape'] return x elif 'value' in d: return np.array(d['value'], dtype=d['dtype']) elif 'npy' in d: return deserialize_ndarray_npy(d) else: raise ValueError('Malformed np.ndarray encoding.')
Example 5
Project: AdaptiveWingLoss Author: protossw512 File: utils.py License: Apache License 2.0 | 6 votes |
def fig2data(fig): """ @brief Convert a Matplotlib figure to a 4D numpy array with RGBA channels and return it @param fig a matplotlib figure @return a numpy 3D array of RGBA values """ # draw the renderer fig.canvas.draw ( ) # Get the RGB buffer from the figure w,h = fig.canvas.get_width_height() buf = np.fromstring (fig.canvas.tostring_rgb(), dtype=np.uint8) buf.shape = (w, h, 3) # canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA mode buf = np.roll (buf, 3, axis=2) return buf
Example 6
Project: ConvLab Author: ConvLab File: mdbt_util.py License: MIT License | 6 votes |
def load_word_vectors(url): ''' Load the word embeddings from the url :param url: to the word vectors :return: dict of word and vector values ''' word_vectors = {} # print("Loading the word embeddings....................") # print('abs path: ', os.path.abspath(url)) with open(url, mode='r', encoding='utf8') as f: for line in f: line = line.split(" ", 1) key = line[0] word_vectors[key] = np.fromstring(line[1], dtype="float32", sep=" ") # print("\tMDBT: The vocabulary contains about {} word embeddings".format(len(word_vectors))) return normalise_word_vectors(word_vectors)
Example 7
Project: me-ica Author: ME-ICA File: netcdf.py License: GNU Lesser General Public License v2.1 | 6 votes |
def _read_values(self): nc_type = self.fp.read(4) n = self._unpack_int() typecode, size = TYPEMAP[nc_type] count = n*size values = self.fp.read(int(count)) self.fp.read(-count % 4) # read padding if typecode is not 'c': values = fromstring(values, dtype='>%s' % typecode) if values.shape == (1,): values = values[0] else: values = values.rstrip(asbytes('\x00')) return values
Example 8
Project: object-detection Author: cristianpb File: camera_pi.py License: MIT License | 6 votes |
def frames(): with PiCamera() as camera: camera.rotation = int(str(os.environ['CAMERA_ROTATION'])) stream = io.BytesIO() for _ in camera.capture_continuous(stream, 'jpeg', use_video_port=True): # return current frame stream.seek(0) _stream = stream.getvalue() data = np.fromstring(_stream, dtype=np.uint8) img = cv2.imdecode(data, 1) yield img # reset stream for next frame stream.seek(0) stream.truncate()
Example 9
Project: Jamais-Vu Author: CwbhX File: decoder.py License: MIT License | 6 votes |
def read(filename, limit=None): """ Reads any file supported by pydub (ffmpeg) and returns the data contained within. If file reading fails due to input being a 24-bit wav file, wavio is used as a backup. Can be optionally limited to a certain amount of seconds from the start of the file by specifying the `limit` parameter. This is the amount of seconds from the start of the file. returns: (channels, samplerate) """ # pydub does not support 24-bit wav files, use wavio when this occurs try: audiofile = AudioSegment.from_file(filename) if limit: audiofile = audiofile[:limit * 1000] data = np.fromstring(audiofile._data, np.int16) channels = [] for chn in xrange(audiofile.channels): channels.append(data[chn::audiofile.channels]) fs = audiofile.frame_rate except audioop.error: fs, _, audiofile = wavio.readwav(filename) if limit: audiofile = audiofile[:limit * 1000] audiofile = audiofile.T audiofile = audiofile.astype(np.int16) channels = [] for chn in audiofile: channels.append(chn) return channels, audiofile.frame_rate, unique_hash(filename)
Example 10
Project: Jamais-Vu Author: CwbhX File: wavio.py License: MIT License | 6 votes |
def _wav2array(nchannels, sampwidth, data): """data must be the string containing the bytes from the wav file.""" num_samples, remainder = divmod(len(data), sampwidth * nchannels) if remainder > 0: raise ValueError('The length of data is not a multiple of ' 'sampwidth * num_channels.') if sampwidth > 4: raise ValueError("sampwidth must not be greater than 4.") if sampwidth == 3: a = _np.empty((num_samples, nchannels, 4), dtype=_np.uint8) raw_bytes = _np.fromstring(data, dtype=_np.uint8) a[:, :, :sampwidth] = raw_bytes.reshape(-1, nchannels, sampwidth) a[:, :, sampwidth:] = (a[:, :, sampwidth - 1:sampwidth] >> 7) * 255 result = a.view('<i4').reshape(a.shape[:-1]) else: # 8 bit samples are stored as unsigned ints; others as signed ints. dt_char = 'u' if sampwidth == 1 else 'i' a = _np.fromstring(data, dtype='<%s%d' % (dt_char, sampwidth)) result = a.reshape(-1, nchannels) return result
Example 11
Project: pykitti Author: utiasSTARS File: odometry.py License: MIT License | 6 votes |
def _load_poses(self): """Load ground truth poses (T_w_cam0) from file.""" pose_file = os.path.join(self.pose_path, self.sequence + '.txt') # Read and parse the poses poses = [] try: with open(pose_file, 'r') as f: lines = f.readlines() if self.frames is not None: lines = [lines[i] for i in self.frames] for line in lines: T_w_cam0 = np.fromstring(line, dtype=float, sep=' ') T_w_cam0 = T_w_cam0.reshape(3, 4) T_w_cam0 = np.vstack((T_w_cam0, [0, 0, 0, 1])) poses.append(T_w_cam0) except FileNotFoundError: print('Ground truth poses are not available for sequence ' + self.sequence + '.') self.poses = poses
Example 12
Project: Python-GUI-examples Author: swharden File: SWHear.py License: MIT License | 6 votes |
def stream_readchunk(self): """reads some audio and re-launches itself""" try: self.data = np.fromstring(self.stream.read(self.chunk),dtype=np.int16) self.fftx, self.fft = getFFT(self.data,self.rate) except Exception as E: print(" -- exception! terminating...") print(E,"\n"*5) self.keepRecording=False if self.keepRecording: self.stream_thread_new() else: self.stream.close() self.p.terminate() print(" -- stream STOPPED") self.chunksRead+=1
Example 13
Project: tf-lcnn Author: ildoonet File: data_feeder.py License: GNU General Public License v3.0 | 6 votes |
def get_data(self): idxs = np.arange(len(self.train_list)) if self.shuffle: self.rng.shuffle(idxs) caches = {} for i, k in enumerate(idxs): path = self.train_list[k] label = self.lb_list[k] if i % self.preload == 0: try: caches = ILSVRCTenth._read_tenth_batch(self.train_list[idxs[i:i+self.preload]]) except Exception as e: logging.warning('tenth local cache failed, err=%s' % str(e)) content = caches.get(path, '') if not content: content = ILSVRCTenth._read_tenth(path) img = cv2.imdecode(np.fromstring(content, dtype=np.uint8), cv2.IMREAD_COLOR) yield [img, label]
Example 14
Project: typhon Author: atmtools File: catalogues.py License: MIT License | 6 votes |
def from_xml(cls, xmlelement): """Loads a Sparse object from an existing file.""" binaryfp = xmlelement.binaryfp nelem = int(xmlelement[0].attrib['nelem']) nrows = int(xmlelement.attrib['nrows']) ncols = int(xmlelement.attrib['ncols']) if binaryfp is None: rowindex = np.fromstring(xmlelement[0].text, sep=' ').astype(int) colindex = np.fromstring(xmlelement[1].text, sep=' ').astype(int) sparsedata = np.fromstring(xmlelement[2].text, sep=' ') else: rowindex = np.fromfile(binaryfp, dtype='<i4', count=nelem) colindex = np.fromfile(binaryfp, dtype='<i4', count=nelem) sparsedata = np.fromfile(binaryfp, dtype='<d', count=nelem) return cls((sparsedata, (rowindex, colindex)), [nrows, ncols])
Example 15
Project: typhon Author: atmtools File: read.py License: MIT License | 6 votes |
def Vector(elem): nelem = int(elem.attrib['nelem']) if nelem == 0: arr = np.ndarray((0,)) else: # sep=' ' seems to work even when separated by newlines, see # http://stackoverflow.com/q/31882167/974555 if elem.binaryfp is not None: arr = np.fromfile(elem.binaryfp, dtype='<d', count=nelem) else: arr = np.fromstring(elem.text, sep=' ') if arr.size != nelem: raise RuntimeError( 'Expected {:s} elements in Vector, found {:d}' ' elements!'.format(elem.attrib['nelem'], arr.size)) return arr
Example 16
Project: typhon Author: atmtools File: read.py License: MIT License | 6 votes |
def ComplexVector(elem): nelem = int(elem.attrib['nelem']) if nelem == 0: arr = np.ndarray((0,), dtype=np.complex128) else: # sep=' ' seems to work even when separated by newlines, see # http://stackoverflow.com/q/31882167/974555 if elem.binaryfp is not None: arr = np.fromfile(elem.binaryfp, dtype=np.complex128, count=nelem) else: arr = np.fromstring(elem.text, sep=' ', dtype=np.float64) arr.dtype = np.complex128 if arr.size != nelem: raise RuntimeError( 'Expected {:s} elements in Vector, found {:d}' ' elements!'.format(elem.attrib['nelem'], arr.size)) return arr
Example 17
Project: typhon Author: atmtools File: read.py License: MIT License | 6 votes |
def ComplexMatrix(elem): # turn dims around: in ARTS, [10 x 1 x 1] means 10 pages, 1 row, 1 col dimnames = [dim for dim in dimension_names if dim in elem.attrib.keys()][::-1] dims = [int(elem.attrib[dim]) for dim in dimnames] if np.prod(dims) == 0: flatarr = np.ndarray(dims, dtype=np.complex128) elif elem.binaryfp is not None: flatarr = np.fromfile(elem.binaryfp, dtype=np.complex128, count=np.prod(np.array(dims)).item()) flatarr = flatarr.reshape(dims) else: flatarr = np.fromstring(elem.text, sep=' ', dtype=np.float64) flatarr.dtype = np.complex128 flatarr = flatarr.reshape(dims) return flatarr
Example 18
Project: Caffe-Python-Data-Layer Author: liuxianming File: bcfstore.py License: BSD 2-Clause "Simplified" License | 5 votes |
def __init__(self, filename): self._filename = filename print 'Loading BCF file to memory ... '+filename file = open(filename, 'rb') size = numpy.fromstring(file.read(8), dtype=numpy.uint64) file_sizes = numpy.fromstring(file.read(8*size), dtype=numpy.uint64) self._offsets = numpy.append(numpy.uint64(0), numpy.add.accumulate(file_sizes)) self._memory = file.read() file.close()
Example 19
Project: Caffe-Python-Data-Layer Author: liuxianming File: bcfstore.py License: BSD 2-Clause "Simplified" License | 5 votes |
def __init__(self, filename): self._filename = filename print 'Opening BCF file ... '+filename self._file = open(filename, 'rb') size = numpy.fromstring(self._file.read(8), dtype=numpy.uint64) file_sizes = numpy.fromstring(self._file.read(8*size), dtype=numpy.uint64) self._offsets = numpy.append(numpy.uint64(0), numpy.add.accumulate(file_sizes))
Example 20
Project: BiblioPixelAnimations Author: ManiacalLabs File: system_eq.py License: MIT License | 5 votes |
def new_frame(self, data, frame_count, time_info, status): data = np.fromstring(data, 'int16') with self.lock: self.frames.append(data) if self.stop: return None, pyaudio.paComplete return None, pyaudio.paContinue
Example 21
Project: BiblioPixelAnimations Author: ManiacalLabs File: system_eq.py License: MIT License | 5 votes |
def new_frame(self, data, frame_count, time_info, status): data = np.fromstring(data, 'int16') with self.lock: self.frames.append(data) if self.stop: return None, pyaudio.paComplete return None, pyaudio.paContinue
Example 22
Project: disentangling_conditional_gans Author: zalandoresearch File: dataset_tool.py License: MIT License | 5 votes |
def create_lsun(tfrecord_dir, lmdb_dir, resolution=256, max_images=None): print('Loading LSUN dataset from "%s"' % lmdb_dir) import lmdb # pip install lmdb import cv2 # pip install opencv-python import io with lmdb.open(lmdb_dir, readonly=True).begin(write=False) as txn: total_images = txn.stat()['entries'] if max_images is None: max_images = total_images with TFRecordExporter(tfrecord_dir, max_images) as tfr: for idx, (key, value) in enumerate(txn.cursor()): try: try: img = cv2.imdecode(np.fromstring(value, dtype=np.uint8), 1) if img is None: raise IOError('cv2.imdecode failed') img = img[:, :, ::-1] # BGR => RGB except IOError: img = np.asarray(PIL.Image.open(io.BytesIO(value))) crop = np.min(img.shape[:2]) img = img[(img.shape[0] - crop) // 2 : (img.shape[0] + crop) // 2, (img.shape[1] - crop) // 2 : (img.shape[1] + crop) // 2] img = PIL.Image.fromarray(img, 'RGB') img = img.resize((resolution, resolution), PIL.Image.ANTIALIAS) img = np.asarray(img) img = img.transpose(2, 0, 1) # HWC => CHW tfr.add_image(img) except: print(sys.exc_info()[1]) if tfr.cur_images == max_images: break #----------------------------------------------------------------------------
Example 23
Project: disentangling_conditional_gans Author: zalandoresearch File: dataset.py License: MIT License | 5 votes |
def parse_tfrecord_np(record): ex = tf.train.Example() ex.ParseFromString(record) shape = ex.features.feature['shape'].int64_list.value data = ex.features.feature['data'].bytes_list.value[0] return np.fromstring(data, np.uint8).reshape(shape) #---------------------------------------------------------------------------- # Dataset class that loads data from tfrecords files.
Example 24
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: capsulenet.py License: Apache License 2.0 | 5 votes |
def read_data(label_url, image_url): with gzip.open(download_data(label_url)) as flbl: magic, num = struct.unpack(">II", flbl.read(8)) label = np.fromstring(flbl.read(), dtype=np.int8) with gzip.open(download_data(image_url), 'rb') as fimg: magic, num, rows, cols = struct.unpack(">IIII", fimg.read(16)) image = np.fromstring(fimg.read(), dtype=np.uint8).reshape(len(label), rows, cols) return label, image
Example 25
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: train_mnist.py License: Apache License 2.0 | 5 votes |
def read_data(label, image): """ download and read data into numpy """ base_url = 'http://yann.lecun.com/exdb/mnist/' with gzip.open(download_file(base_url+label, os.path.join('data',label))) as flbl: magic, num = struct.unpack(">II", flbl.read(8)) label = np.fromstring(flbl.read(), dtype=np.int8) with gzip.open(download_file(base_url+image, os.path.join('data',image)), 'rb') as fimg: magic, num, rows, cols = struct.unpack(">IIII", fimg.read(16)) image = np.fromstring(fimg.read(), dtype=np.uint8).reshape(len(label), rows, cols) return (label, image)
Example 26
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: captcha_generator.py License: Apache License 2.0 | 5 votes |
def main(): parser = argparse.ArgumentParser() parser.add_argument("font_path", help="Path to ttf font file") parser.add_argument("output", help="Output filename including extension (e.g. 'sample.jpg')") parser.add_argument("--num", help="Up to 4 digit number [Default: random]") args = parser.parse_args() captcha = ImageCaptcha(fonts=[args.font_path]) captcha_str = args.num if args.num else DigitCaptcha.get_rand(3, 4) img = captcha.generate(captcha_str) img = np.fromstring(img.getvalue(), dtype='uint8') img = cv2.imdecode(img, cv2.IMREAD_GRAYSCALE) cv2.imwrite(args.output, img) print("Captcha image with digits {} written to {}".format([int(c) for c in captcha_str], args.output))
Example 27
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: proposal_target.py License: Apache License 2.0 | 5 votes |
def __init__(self, num_classes='21', batch_images='1', batch_rois='128', fg_fraction='0.25', fg_overlap='0.5', box_stds='(0.1, 0.1, 0.2, 0.2)'): super(ProposalTargetProp, self).__init__(need_top_grad=False) self._num_classes = int(num_classes) self._batch_images = int(batch_images) self._batch_rois = int(batch_rois) self._fg_fraction = float(fg_fraction) self._fg_overlap = float(fg_overlap) self._box_stds = tuple(np.fromstring(box_stds[1:-1], dtype=float, sep=','))
Example 28
Project: GST-Tacotron Author: KinglittleQ File: cutoff.py License: MIT License | 5 votes |
def cutoff(input_wav, output_wav): ''' input_wav --- input wav file path output_wav --- output wav file path ''' # read input wave file and get parameters. with wave.open(input_wav, 'r') as fw: params = fw.getparams() # print(params) nchannels, sampwidth, framerate, nframes = params[:4] strData = fw.readframes(nframes) waveData = np.fromstring(strData, dtype=np.int16) max_v = np.max(abs(waveData)) for i in range(waveData.shape[0]): if abs(waveData[i]) > 0.08 * max_v: break for j in range(waveData.shape[0] - 1, 0, -1): if abs(waveData[j]) > 0.08 * max_v: break # write new wav file with wave.open(output_wav, 'w') as fw: params = list(params) params[3] = nframes - i - (waveData.shape[0] - 1 - j) fw.setparams(params) fw.writeframes(strData[2 * i:2 * (j + 1)])
Example 29
Project: DOTA_models Author: ringringyi File: plot_lfads.py License: Apache License 2.0 | 5 votes |
def plot_lfads(train_bxtxd, train_model_vals, train_ext_input_bxtxi=None, train_truth_bxtxd=None, valid_bxtxd=None, valid_model_vals=None, valid_ext_input_bxtxi=None, valid_truth_bxtxd=None, bidx=None, cf=1.0, output_dist='poisson'): # Plotting f = plt.figure(figsize=(18,20), tight_layout=True) plot_lfads_timeseries(train_bxtxd, train_model_vals, train_ext_input_bxtxi, truth_bxtxn=train_truth_bxtxd, conversion_factor=cf, bidx=bidx, output_dist=output_dist, col_title='Train') plot_lfads_timeseries(valid_bxtxd, valid_model_vals, valid_ext_input_bxtxi, truth_bxtxn=valid_truth_bxtxd, conversion_factor=cf, bidx=bidx, output_dist=output_dist, subplot_cidx=1, col_title='Valid') # Convert from figure to an numpy array width x height x 3 (last for RGB) f.canvas.draw() data = np.fromstring(f.canvas.tostring_rgb(), dtype=np.uint8, sep='') data_wxhx3 = data.reshape(f.canvas.get_width_height()[::-1] + (3,)) plt.close() return data_wxhx3
Example 30
Project: deep-models Author: LaurentMazare File: rhn-text8.py License: Apache License 2.0 | 5 votes |
def load(filename): with open(filename, 'r') as f: data = f.read() data = np.fromstring(data, dtype=np.uint8) unique, data = np.unique(data, return_inverse=True) return data, len(unique)