def __read_spike_fixed(self, numpts=40):
"""
Read a spike with a fixed waveform length (40 time bins)
-------------------------------------------
Returns the time, waveform and trig2 value.
The returned objects must be converted to a SpikeTrain then
added to the Block.
ID: 29079
"""
# float32 -- spike time stamp in ms since start of SpikeTrain
time = np.fromfile(self._fsrc, dtype=np.float32, count=1)
# int8 * 40 -- spike shape -- use numpts for spike_var
waveform = np.fromfile(self._fsrc, dtype=np.int8,
count=numpts).reshape(1, 1, numpts)
# uint8 -- point of return to noise
trig2 = np.fromfile(self._fsrc, dtype=np.uint8, count=1)
return time, waveform, trig2
python类uint8()的实例源码
def __read_spike_var(self):
"""
Read a spike with a variable waveform length
-------------------------------------------
Returns the time, waveform and trig2 value.
The returned objects must be converted to a SpikeTrain then
added to the Block.
ID: 29115
"""
# uint8 -- number of points in spike shape
numpts = np.fromfile(self._fsrc, dtype=np.uint8, count=1)[0]
# spike_fixed is the same as spike_var if you don't read the numpts
# byte and set numpts = 40
return self.__read_spike_fixed(numpts)
def __read_spike_fixed(self, numpts=40):
"""
Read a spike with a fixed waveform length (40 time bins)
-------------------------------------------
Returns the time, waveform and trig2 value.
The returned objects must be converted to a SpikeTrain then
added to the Block.
ID: 29079
"""
# float32 -- spike time stamp in ms since start of SpikeTrain
time = np.fromfile(self._fsrc, dtype=np.float32, count=1)
# int8 * 40 -- spike shape -- use numpts for spike_var
waveform = np.fromfile(self._fsrc, dtype=np.int8,
count=numpts).reshape(1, 1, numpts)
# uint8 -- point of return to noise
trig2 = np.fromfile(self._fsrc, dtype=np.uint8, count=1)
return time, waveform, trig2
def __read_spike_var(self):
"""
Read a spike with a variable waveform length
-------------------------------------------
Returns the time, waveform and trig2 value.
The returned objects must be converted to a SpikeTrain then
added to the Block.
ID: 29115
"""
# uint8 -- number of points in spike shape
numpts = np.fromfile(self._fsrc, dtype=np.uint8, count=1)[0]
# spike_fixed is the same as spike_var if you don't read the numpts
# byte and set numpts = 40
return self.__read_spike_fixed(numpts)
def __init__(self, *args, **kwds):
import numpy
self.dst_types = [numpy.uint8, numpy.uint16, numpy.uint32]
try:
self.dst_types.append(numpy.uint64)
except AttributeError:
pass
pygame.display.init()
try:
unittest.TestCase.__init__(self, *args, **kwds)
self.sources = [self._make_src_surface(8),
self._make_src_surface(16),
self._make_src_surface(16, srcalpha=True),
self._make_src_surface(24),
self._make_src_surface(32),
self._make_src_surface(32, srcalpha=True)]
finally:
pygame.display.quit()
def array2d(surface):
"""pygame.numpyarray.array2d(Surface): return array
copy pixels into a 2d array
Copy the pixels from a Surface into a 2D array. The bit depth of the
surface will control the size of the integer values, and will work
for any type of pixel format.
This function will temporarily lock the Surface as pixels are copied
(see the Surface.lock - lock the Surface memory for pixel access
method).
"""
bpp = surface.get_bytesize()
try:
dtype = (numpy.uint8, numpy.uint16, numpy.int32, numpy.int32)[bpp - 1]
except IndexError:
raise ValueError("unsupported bit depth %i for 2D array" % (bpp * 8,))
size = surface.get_size()
array = numpy.empty(size, dtype)
surface_to_array(array, surface)
return array
def array3d(surface):
"""pygame.numpyarray.array3d(Surface): return array
copy pixels into a 3d array
Copy the pixels from a Surface into a 3D array. The bit depth of the
surface will control the size of the integer values, and will work
for any type of pixel format.
This function will temporarily lock the Surface as pixels are copied
(see the Surface.lock - lock the Surface memory for pixel access
method).
"""
w, h = surface.get_size()
array = numpy.empty((w, h, 3), numpy.uint8)
surface_to_array(array, surface)
return array
def array_red(surface):
"""pygame.numpyarray.array_red(Surface): return array
copy pixel red into a 2d array
Copy the pixel red values from a Surface into a 2D array. This will work
for any type of Surface format.
This function will temporarily lock the Surface as pixels are copied
(see the Surface.lock - lock the Surface memory for pixel access
method).
"""
size = surface.get_size()
array = numpy.empty(size, numpy.uint8)
surface_to_array(array, surface, 'R')
return array
def array_green(surface):
"""pygame.numpyarray.array_green(Surface): return array
copy pixel green into a 2d array
Copy the pixel green values from a Surface into a 2D array. This will work
for any type of Surface format.
This function will temporarily lock the Surface as pixels are copied
(see the Surface.lock - lock the Surface memory for pixel access
method).
"""
size = surface.get_size()
array = numpy.empty(size, numpy.uint8)
surface_to_array(array, surface, 'G')
return array
def array_blue(surface):
"""pygame.numpyarray.array_blue(Surface): return array
copy pixel blue into a 2d array
Copy the pixel blue values from a Surface into a 2D array. This will work
for any type of Surface format.
This function will temporarily lock the Surface as pixels are copied
(see the Surface.lock - lock the Surface memory for pixel access
method).
"""
size = surface.get_size()
array = numpy.empty(size, numpy.uint8)
surface_to_array(array, surface, 'B')
return array
def array_colorkey(surface):
"""pygame.numpyarray.array_colorkey(Surface): return array
copy the colorkey values into a 2d array
Create a new array with the colorkey transparency value from each
pixel. If the pixel matches the colorkey it will be fully
tranparent; otherwise it will be fully opaque.
This will work on any type of Surface format. If the image has no
colorkey a solid opaque array will be returned.
This function will temporarily lock the Surface as pixels are
copied.
"""
size = surface.get_size()
array = numpy.empty(size, numpy.uint8)
surface_to_array(array, surface, 'C')
return array
def write_tfrecord(self, img_list, label_list, record_path):
# write a single tfrecord
if os.path.exists(record_path):
print ("%s exists!"%record_path)
return
self._check_list()
print ("write %s"%record_path)
self._write_info()
writer = tf.python_io.TFRecordWriter(record_path)
c = 0
for imgname,label in zip(img_list,label_list):
img = Image.open(imgname).resize((self.flags.width, self.flags.height))
data = np.array(img).astype(np.uint8)
img,data = self._check_color(img,data)
example = self._get_example(data,label)
writer.write(example.SerializeToString())
c+=1
if c%LOG_EVERY == 0:
print ("%d images written to tfrecord"%c)
writer.close()
print("writing %s done"%record_path)
def array2PIL(arr, size):
mode = 'RGBA'
arr = arr.reshape(arr.shape[0]*arr.shape[1], arr.shape[2])
if len(arr[0]) == 3:
arr = numpy.c_[arr, 255*numpy.ones((len(arr),1), numpy.uint8)]
return Image.frombuffer(mode, size, arr.tostring(), 'raw', mode, 0, 1)
def ONES(n):
return np.ones((n, n), np.uint8)
def conv2d(x,W,strides=[1,1,1,1],name=None):
# return an op that convolves x with W
strides = np.array(strides)
if strides.size == 1:
strides = np.array([1,strides,strides,1])
elif strides.size == 2:
strides = np.array([1,strides[0],strides[1],1])
if np.any(strides < 1):
strides = np.around(1./strides).astype(np.uint8)
return tf.nn.conv2d_transpose(x,W,strides=strides.tolist(),padding='SAME',name=name)
else:
return tf.nn.conv2d(x,W,strides=strides.tolist(),padding='SAME',name=name)
def conv3d(x,W,strides=1,name=None):
# return an op that convolves x with W
strides = np.array(strides)
if strides.size == 1:
strides = np.array([1,strides,strides,strides[0],1])
elif strides.size == 3:
strides = np.array([1,strides[0],strides[1],strides[2],1])
if np.any(strides < 1):
strides = np.around(1./strides).astype(np.uint8)
return tf.nn.conv3d_transpose(x,W,strides=strides.tolist(),padding='SAME',name=name)
else:
return tf.nn.conv3d(x,W,strides=strides.tolist(),padding='SAME',name=name)
def extract_labels(filename, one_hot=False):
"""Extract the labels into a 1D uint8 numpy array [index]."""
print('Extracting', filename)
with gzip.open(filename) as bytestream:
magic = _read32(bytestream)
if magic != 2049:
raise ValueError(
'Invalid magic number %d in MNIST label file: %s' %
(magic, filename))
num_items = _read32(bytestream)
buf = bytestream.read(num_items)
labels = numpy.frombuffer(buf, dtype=numpy.uint8)
if one_hot:
return dense_to_one_hot(labels)
return labels
def get_contour_mask(dshape, img_fl):
mask = np.zeros(dshape)
hull = cv2.convexHull(img_fl)
cv2.drawContours(mask, [hull], 0, (1, 1, 1) , -1)
return np.uint8(mask)
# Orients input_ mask onto tmpl_ face
def orient_faces_wrap(frame, args):
input = args[0]
out_ = orient_faces(frame, input)
if out_ is None:
return None
out = np.uint8(out_)
return out
# A wrapper of mask_on_face for videoize method
def mask_on_face_wrap(frame, args):
input = args[0]
mask_shape = args[1]
out_ = mask_on_face(frame, input, mask_shape)
if out_ is None:
return None
out = np.uint8(out_)
return out
# A routine to extend single-image proc methods to
# successive frames read from Camera