def predict(img_bytes): # Decode x = image.decode_jpeg(img_bytes, channels=3) print('decoded') # Normalize x = image.convert_image_dtype(x, float32) print('normalized') # Resize np_x = image.resize(x, [224, 224]).numpy() x = np_x.tolist() print('resized') # Predict pred = model.predict(reshape(x, [-1, 224, 224, 3])) # Map top_k_values, top_k_indices = math.top_k(pred, k=3, sorted=True, name=None) top_k_values = top_k_values.numpy().tolist()[0] top_k_labels = [d[idx] for idx in top_k_indices.numpy()[0]] results = {'prob': top_k_values, 'prob_labels': top_k_labels} print(results) # Return return json.dumps(results)
def get_img(img_path): img = read_file(img_path) img = decode_jpeg(img, channels=3) img = resize(img, [image_size, image_size]) img = 1 - img/255. # We would rather have the whole white void area be full of zeros than ones img = tf.convert_to_tensor([img]) return img
def process_path(file_path): print(file_path) file = read_file(file_path) file = image.decode_jpeg(file, channels=3) file = cast(file, float32) file = preprocess_input(file) file = image.resize(file, [ROW, COL]) return file
def __call__(self, img): if self.is_encoded: img = tfimg.decode_jpeg(img) img = tf.image.convert_image_dtype(img, tf.float32) for cbr in self.comburant: img = cbr(img) if not tf.is_tensor(img): img = tf.convert_to_tensor(img) return img
def preprocess_image(self, image): image = tf_img.decode_jpeg(image, channels=3) image = tf_img.resize(image, [224, 224]) image /= 255.0 # normalize to [0,1] range return image
def handle_image_path(img_path): img = read_file(img_path) img = decode_jpeg(img, channels=3) img = resize_images(img, [image_size, image_size]) img = img / 255.0 return img
def load_image(image_path, img_width=299, img_height=299): img = io.read_file(image_path) img = image.decode_jpeg(img, channels=3) img = image.resize(img, (img_width, img_height)) return img
def from_raw(cls, image_bytes: bytes): image_tensor = decode_jpeg(image_bytes, channels=3) return cls(image_tensor)