def deepdream_case1(image_bytes): end = 'inception_4b/output' # input image img = np.float32(imread.imread_from_blob(image_bytes)) if img.shape[2] == 1: img = skimage.color.gray2rgb(img[:,:,0]) # best match guide = find_guide(img) h, w = guide.shape[:2] src, dst = net.blobs['data'], net.blobs[end] src.reshape(1,3,h,w) src.data[0] = preprocess(net, guide) net.forward(end=end) guide_features = dst.data[0].copy() def objective_guide(dst): x = dst.data[0].copy() y = guide_features ch = x.shape[0] x = x.reshape(ch,-1) y = y.reshape(ch,-1) A = x.T.dot(y) # compute the matrix of dot-products with guide features dst.diff[0].reshape(ch,-1)[:] = y[:,A.argmax(1)] # select ones that match best result = deepdream(net, img, end=end, objective=objective_guide) dst_image = PIL.Image.fromarray(result.astype(np.uint8)) output_stream = StringIO.StringIO() dst_image.save(output_stream, format="jpeg") contents = output_stream.getvalue() output_stream.close() base_coded = base64.b64encode(contents) return base_coded
def plot_chunk(chunk, mode='spectrogram', output_folder=None, size=227, nfft=None, **kwargs): """ Plot spectrograms for a chunk of a wav-file using the described parameters. :param chunk: audio chunk to be plotted. :param mode: type of audio plot to create. :param nfft: number of samples for the fast fourier transformation (Default: 256) :param size: size of the spectrogram plot in pixels. Height and width are always identical (Default: 227) :param output_folder: if given, the plot is saved to this path in .png format (Default: None) :param kwargs: keyword args for plotting functions :return: blob of the spectrogram plot """ filename, sr, ts, audio = chunk write_index = ts is not None if not nfft: nfft = _next_power_of_two(int(sr * 0.025)) fig = plt.figure(frameon=False) fig.set_size_inches(1, 1) ax = plt.Axes( fig, [0., 0., 1., 1.], ) ax.set_axis_off() fig.add_axes(ax) with warnings.catch_warnings(): warnings.simplefilter('ignore') spectrogram_axes = PLOTTING_FUNCTIONS[mode](audio, sr, nfft, **kwargs) del audio fig.add_axes(spectrogram_axes, id='spectrogram') if output_folder: file_name = basename(filename)[:-4] outfile = join( output_folder, '{}_{:.4f}'.format(file_name, ts).rstrip('0').rstrip('.') + '.png') if write_index else join(output_folder, file_name + '.png') fig.savefig(outfile, format='png', dpi=size) buf = io.BytesIO() fig.savefig(buf, format='png', dpi=size) buf.seek(0) fig.clf() plt.close(fig) img_blob = buf.read() buf.close() try: img = imread_from_blob(img_blob, 'png') img = img[:, :, :-1] except IOError: print('Error while reading the spectrogram blob.') return None return PlotTuple(name=filename, timestamp=ts, plot=img)
def imread(record, dtype=None): """Load an image from a WARC record. Parameters ---------- record : WARC Record """ im = _imread.imread_from_blob(record.payload.read()) if dtype is not None: im = convert(im, dtype) return im
def download_and_resize(url, filename, output_dir="", w_h_shape=None): try: req = urlopen(url) file_type = req.headers.get('Content-Type').split('/')[1] image = imread_from_blob(req.read(), file_type) if w_h_shape: image = cv2.resize(image, w_h_shape) if output_dir and not os.path.exists(output_dir): os.mkdir(output_dir) file_path = os.path.join(output_dir, filename+"."+file_type) imsave(file_path, image, formatstr=file_type) height, width, _ = image.shape return filename, file_path, None, width, height except Exception as ex: return filename, None, ex, -1, -1
def feedforward_bytes(image_bytes): src_float = skimage.img_as_float(imread.imread_from_blob(image_bytes)).astype(np.float32) if src_float.shape[2] == 1: src_float = skimage.color.gray2rgb(src_float[:,:,0]) prediction = net.predict([src_float]) return prediction
def find_guide(input_bytes): #Dummy guide img = np.float32(imread.imread_from_blob(open('/home/denis/workspace/DataScienceHack/pynet/deepdream/new.jpg','rb').read())) if img.shape[2] == 1: img = skimage.color.gray2rgb(img[:,:,0]) return img
def plot_chunk(chunk, mode='spectrogram', output_folder=None, base_path=None, size=227, nfft=None, file_type='png', labelling=False, **kwargs): """ Plot spectrograms for a chunk of a wav-file using the described parameters. :param chunk: audio chunk to be plotted. :param mode: type of audio plot to create. :param nfft: number of samples for the fast fourier transformation \ (Default: 256) :param size: size of the spectrogram plot in pixels. Height and width are \ always identical (Default: 227) :param output_folder: if given, the plot is saved to this path in .png \ format (Default: None) :param kwargs: keyword args for plotting functions :return: blob of the spectrogram plot """ matplotlib.use('Agg') import matplotlib.pyplot as plt filename, sr, ts, audio = chunk write_index = ts is not None if not nfft: nfft = _next_power_of_two(int(sr * 0.025)) log.debug(f'Using nfft={nfft} for the FFT.') fig = plt.figure(frameon=False, tight_layout=False) if labelling: pass else: fig.set_size_inches(1, 1) ax = plt.Axes(fig, [0., 0., 1., 1.]) ax.set_axis_off() fig.add_axes(ax) with warnings.catch_warnings(): warnings.simplefilter('ignore') spectrogram_axes = PLOTTING_FUNCTIONS[mode](audio, sr, nfft, **kwargs) if labelling: original_xlim = spectrogram_axes.get_xlim() if mode != 'chroma': kHz_ticks = np.apply_along_axis(lambda x: x / 1000, 0, spectrogram_axes.get_yticks()) spectrogram_axes.set_yticklabels(kHz_ticks) spectrogram_axes.set_ylabel('Frequency [kHz]', fontdict=label_font) else: spectrogram_axes.set_ylabel('Pitch Classes', fontdict=label_font) if labelling: spectrogram_axes.set_xticks(spectrogram_axes.get_xticks()[::2]) spectrogram_axes.set_xlabel('Time [s]', fontdict=label_font) spectrogram_axes.set_xlim(original_xlim) del audio fig.add_axes(spectrogram_axes, id='spectrogram') if labelling: plt.colorbar(format='%+2.1f dB') plt.tight_layout() if output_folder: relative_file_name = f'{splitext(get_relative_path(filename, base_path))[0]}_{ts:g}.{file_type}' if write_index else f'{splitext(get_relative_path(filename, base_path))[0]}.{file_type}' if base_path is None: outfile = join(output_folder, basename(relative_file_name)) else: outfile = join(output_folder, relative_file_name) log.debug(f'Saving spectrogram plot to {outfile}.') makedirs(dirname(outfile), exist_ok=True) fig.savefig(outfile, format=file_type, dpi=size) buf = io.BytesIO() fig.savefig(buf, format='png', dpi=size) buf.seek(0) fig.clf() plt.close(fig) img_blob = buf.read() buf.close() try: img = imread_from_blob(img_blob, 'png') img = img[:, :, :-1] log.debug(f'Read spectrogram plot with shape {img.shape}.') except IOError: log.error('Error while reading the spectrogram blob.') return None return PlotTuple(name=get_relative_path(filename, base_path), timestamp=ts, plot=img)