def train(self, x_train, y_train, x_test, y_test): """ Train the network with the parameters provided. The training sets must be nparrays of grayscale images, intensity 0..255 """ log.info('Preprocessing the training set') x_train = preprocess(x_train, rows=MNIST_ROWS, cols=MNIST_COLS, padding=PADDING) log.info('Preprocessing the validation set') x_test = preprocess(x_test, rows=MNIST_ROWS, cols=MNIST_COLS, padding=PADDING) self._model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test), batch_size=64) score = self._model.evaluate(x_test, y_test, verbose=0) return score
def run(self): global vision_results global vidion_isRun while self.cap.isOpened(): image_data = [] ret, img = self.cap.read() if not ret or not vidion_isRun: continue out_img = image.preprocess(img) image_data.append(out_img) x = np.array(image_data) test = x.reshape(-1, self.img_width * self.img_height).astype( np.float32) # knn预测,k值更具训练的图片数量可以修改,越多可以越大 ret, result, neighbours, dist = self.knn.findNearest(test, k=5) vision_results = ret # cv2.imshow("img",img) # 不能再线程中显示 # cv2.waitKey(10) print("退出线程")
def preprocess(): '''Runs the preprocessing cycle''' # Import here to prevent slowdown in different modes from image import preprocess from os import path, listdir files = [ path.join(args.load_path[0], f) for f in listdir(args.load_path[0]) if path.isfile(path.join(args.load_path[0], f)) ] for file in files: if file.split('.')[-1] == 'tif': preprocess(file, args.save_path[0], args.delete, not args.no_overwrite) getLogger('preprocess').info('Preprocessed %s files to "%s"', len(files), args.save_path[0] or args.load_path[0])
def recognise(self, img): img = from_binary(img) log.debug('Converting to a grayscale image') img = convert_colour(img, transparency_colour=255, colour=cv2.COLOR_BGRA2GRAY) log.debug('Adjusting dimensions for Keras') img = adjust_dimensions(img) log.debug('Preprocessing images before feeding them to the network') img = preprocess(img, rows=MNIST_ROWS, cols=MNIST_COLS, padding=PADDING, verbose=_VERBOSE) log.debug('Image preprocessing complete') verify(img) log.debug('Recognising the image') response = self.client.recognise(img) return next(iter(classify(response)))
def predict(clf, boundaries, img=None, align_template=None): if isinstance(img, basestring): raw_img = io.imread(img) elif isinstance(img, np.ndarray): raw_img = img else: raise TypeError() if align_template is None: aligned_img = raw_img else: aligned_img = align_fft(raw_img, align_template) cropped_img = crop(aligned_img, boundaries) binarized_img = preprocess(cropped_img) chars = split(binarized_img) # this gets an array of ints predictions = clf.predict(np.array([c.reshape(-1) for c in chars])) # make into a string prediction = ''.join([str(p) for p in predictions]) return prediction, binarized_img, chars
def create(self, source: str, meta: int, to_pickle: bool = False): source_ext = source.split('.')[-1] meta_reshaped = tf.repeat(tf.repeat(np.moveaxis(tf.expand_dims(tf.expand_dims(normalize_meta(meta), 1, 0), 1, 0), 0, -1), 64, axis=0), 64, axis=1)[None] if not path.exists(source) or not source_ext in ['pickle', 'tif']: self.log.error('Cannot find TIF image or pickle at "%s"', source) return if source_ext == 'tif': image = preprocess(source, crop_image = False, return_value = True) image = np.nan_to_num(image) else: with open(source, 'rb') as f: image = pickle.load(f) if image.shape[0] < 64 or image.shape[1] < 64: self.log.error('Image shape needs to be at least 64 by 64 pixels.') return elif image.shape[0] > 64 or image.shape[1] > 64: self.log.info("Original image size %d by %d", image.shape[1], image.shape[0]) tiles = self.tile(image) self.log.info("Cut into %d tiles", len(tiles)) else: tiles = [image[None]] results = [] for tile in tiles: if to_pickle: results.append(np.squeeze(self.generator([tile, meta_reshaped]).numpy())) else: results.append(denormalize(np.squeeze(self.generator([tile, meta_reshaped]).numpy()))) if len(results) > 1: stitched = self.stitch(results, image.shape) self.log.info('Stiched image dimensions %d by %d', stitched.shape[1], stitched.shape[0]) return stitched else: return results[0]
help='Gradient ascent step size', required=False) parser.add_argument('-oct', '--octave', type=int, help='Number of scales at which to run gradient ascent', required=False) parser.add_argument('-ocs', '--octavescale', type=float, help='Size ratio between scales', required=False) parser.add_argument('-mxl', '--maxloss', type=float, help='Maximum gradient ascent loss', required=False) # These are the names of the InceptionV3 50 layers # for which we try to maximize activation, # as well as their weight in the loss # we try to maximize. # You can tweak these setting to obtain new visual effects. config = { 'mixed2': 0.2, 'mixed3': 0.5, 'mixed4': 2., 'mixed5': 1.5, } args = parser.parse_args() dream = CreepDream(args.model, config) dream = dream.compile() # preprocess image img = preprocess(args.input, args.model) # start creep dreaming img = dream.run(img, args.iterations, args.step, args.octave, args.octavescale, args.maxloss) # save resulting image to hard drive save(img, fname=args.output)