def test_draw_bounding_box(self): """Test case for draw_bounding_box.""" width = 560 height = 320 channels = 4 with open( os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_image", "small-00.png"), 'rb') as f: png_contents = f.read() with open( os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_image", "small-bb.png"), 'rb') as f: ex_png_contents = f.read() with self.cached_session(): ex_image_p = image.decode_png(ex_png_contents, channels=channels) ex_image_p = tf.expand_dims(ex_image_p, 0) # TODO: Travis seems to have issues with different rendering. Skip for now. # ex_image_v = ex_image_p.eval() _ = ex_image_p.eval() bb = [[[0.1, 0.2, 0.5, 0.9]]] with self.cached_session(): image_p = image.decode_png(png_contents, channels=channels) image_v = image_p.eval() self.assertEqual(image_v.shape, (height, width, channels)) image_p = image.convert_image_dtype(image_p, tf.float32) image_p = tf.expand_dims(image_p, 0) bb_image_p = image_io.draw_bounding_boxes(image_p, bb, ["hello world!"]) bb_image_p = image.convert_image_dtype(bb_image_p, tf.uint8) # TODO: Travis seems to have issues with different rendering. Skip for now. # bb_image_v = bb_image_p.eval() # self.assertAllEqual(bb_image_v, ex_image_v) _ = bb_image_p.eval()
def ssim(img1, img2, max_val): _, _, checks = _verify_compatible_image_shapes(img1, img2) with ops.control_dependencies(checks): img1 = array_ops.identity(img1) # Need to convert the images to float32. Scale max_val accordingly so that # SSIM is computed correctly. max_val = math_ops.cast(max_val, img1.dtype) max_val = convert_image_dtype(max_val, dtypes.float32) img1 = convert_image_dtype(img1, dtypes.float32) img2 = convert_image_dtype(img2, dtypes.float32) ssim_per_channel, cs, luminance = _ssim_per_channel(img1, img2, max_val) # Compute average over color channels. return math_ops.reduce_mean(ssim_per_channel, [-1]), math_ops.reduce_mean(cs, [-1]), math_ops.reduce_mean(luminance, [-1])
def decode_img_enhanced(leye_img, reye_img, region, label): precision_type = tf.float16 region = tf.cast(region, tf.int32) leye_im = tf.io.decode_jpeg(leye_img) reye_im = tf.io.decode_jpeg(reye_img) '''Convert to float16/32 in the [0,1] range''' leye_im = convert_image_dtype(leye_im, precision_type) reye_im = convert_image_dtype(reye_im, precision_type) '''Resize''' leye_im = resize(leye_im, [config.eyeIm_size, config.eyeIm_size]) reye_im = resize(reye_im, [config.eyeIm_size, config.eyeIm_size]) '''Normalize''' # leye_im = tf.image.per_image_standardization(leye_im) # reye_im = tf.image.per_image_standardization(reye_im) orientation = tf.cast(tf.one_hot(region[24], depth=3), precision_type) eyelandmark = tf.cast(tf.concat([region[8:11], region[13:16]], 0), tf.float32) / 640.0 '''Create heatmap label''' if (config.heatmap): hmFocus_size = 17 if (config.mobile) else 9 # tablet focus_size=9 HM_FOCUS_IM = np.zeros((5, hmFocus_size, hmFocus_size, 1)) stdv_list = [0.2, 0.25, 0.3, 0.35, 0.4] for level in range(5): # 5 levels of std to constuct heatmap stdv = stdv_list[level] # 3/(12-level) for i in range(hmFocus_size): for j in range(hmFocus_size): distanceFromCenter = 2 * \ np.linalg.norm(np.array([i-int(hmFocus_size/2), j-int(hmFocus_size/2)]))/((hmFocus_size)/2) gauss_prob = gauss(distanceFromCenter, stdv) HM_FOCUS_IM[level, i, j, 0] = gauss_prob HM_FOCUS_IM[level, :, :, 0] /= np.sum(HM_FOCUS_IM[level, :, :, 0]) heatmap_im = convert_image_dtype(HM_FOCUS_IM[0, :, :, :], tf.float32) heatmap_im = pad_to_bounding_box( heatmap_im, int(label[0] * config.scale + config.hm_size / 2 - hmFocus_size / 2), int(label[1] * config.scale + config.hm_size / 2 - hmFocus_size / 2), config.hm_size, config.hm_size) label = heatmap_im return (orientation, eyelandmark, leye_im, reye_im, label)
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 load_image(image_path): """ Convert all image into array of shape (1, width, height, 3) """ image = io.read_file(image_path) image = img.decode_image(image, channels=3) image = img.convert_image_dtype(image, float32) image = img.resize(image, (width, height)) image = image[newaxis, :] return image
def PSNRLoss(y_true, y_pred): y_true=K.clip(y_true, 0, 1) y_pred=K.clip(y_pred, 0, 1) im1 = image.convert_image_dtype(y_true, float32) im2 = image.convert_image_dtype(y_pred, float32) return -10. * K.log(K.mean(K.square(im2 - im1))) / K.log(10.)
def decode_img(img, region, label): precision_type = tf.float16 region = tf.cast(region, tf.int32) face_im = tf.io.decode_and_crop_jpeg( img, [region[5], region[4], region[3], region[2]], channels=config.channel) if (config.regions == 'default' ): # only for the default GazeCapture facial regions '''Crop eye regions from face region''' leye_im = face_im[region[10]:(region[10] + region[7]), region[9]:(region[9] + region[8]), :] reye_im = face_im[region[15]:(region[15] + region[12]), region[14]:(region[14] + region[13]), :] else: '''Decode and crop eye regions directly from raw image''' leye_im = tf.io.decode_and_crop_jpeg( img, [region[10], region[9], region[8], region[7]], channels=config.channel) reye_im = tf.io.decode_and_crop_jpeg( img, [region[15], region[14], region[13], region[12]], channels=config.channel) '''Convert to float16/32 in the [0,1] range''' leye_im = convert_image_dtype(leye_im, precision_type) reye_im = convert_image_dtype(reye_im, precision_type) '''Resize''' leye_im = resize(leye_im, [config.eyeIm_size, config.eyeIm_size]) reye_im = resize(reye_im, [config.eyeIm_size, config.eyeIm_size]) '''Normalize''' # leye_im = tf.image.per_image_standardization(leye_im) # reye_im = tf.image.per_image_standardization(reye_im) orientation = tf.cast(tf.one_hot(region[24], depth=3), precision_type) if (config.arc == 'iTracker'): face_im = convert_image_dtype(face_im, precision_type) face_im = resize(face_im, [config.faceIm_size, config.faceIm_size]) '''Create face grid''' face_grid_im = convert_image_dtype( tf.ones((region[19], region[19], 1)), precision_type) face_grid_im = pad_to_bounding_box(face_grid_im, region[18], region[17], config.faceGrid_size, config.faceGrid_size) elif (config.arc == 'SAGE'): eyelandmark = tf.cast(tf.concat([region[8:11], region[13:16]], 0), tf.float32) / 640.0 # SAGE mode leye_im = tf.image.flip_left_right(leye_im) '''Create heatmap label''' if (config.heatmap): hmFocus_size = 17 # if (config.mobile) else 9 # in pixel unit HM_FOCUS_IM = np.zeros((5, hmFocus_size, hmFocus_size, 1)) stdv_list = [0.2, 0.25, 0.3, 0.35, 0.4] for level in range(5): # 5 levels of std to constuct heatmap stdv = stdv_list[level] # 3/(12-level) for i in range(hmFocus_size): for j in range(hmFocus_size): distanceFromCenter = 2 * \ np.linalg.norm(np.array([i-int(hmFocus_size/2), j-int(hmFocus_size/2)]))/((hmFocus_size)/2) gauss_prob = gauss(distanceFromCenter, stdv) HM_FOCUS_IM[level, i, j, 0] = gauss_prob HM_FOCUS_IM[level, :, :, 0] /= np.sum(HM_FOCUS_IM[level, :, :, 0]) heatmap_im = convert_image_dtype(HM_FOCUS_IM[0, :, :, :], tf.float32) heatmap_im = pad_to_bounding_box( heatmap_im, int(label[0] * config.scale + config.hm_size / 2 - hmFocus_size / 2), int(label[1] * config.scale + config.hm_size / 2 - hmFocus_size / 2), config.hm_size, config.hm_size) label = heatmap_im if (config.arc == 'SAGE'): return (orientation, eyelandmark, leye_im, reye_im, label) else: return (orientation, face_grid_im, face_im, leye_im, reye_im, label)
def to_4D_float32(self) -> Tensor: img_4D = to_4D_image(self.image_tensor) img_4D_f32 = convert_image_dtype(img_4D, float32) return img_4D_f32
def from_4D_float32_tensor(cls, image_4D_f32: Tensor): image_unint8 = convert_image_dtype(image_4D_f32, uint8) image = from_4D_image(image_unint8, 3) return cls(image)
def preprocess(item): image, label = item['image'], item['label'] image = timg.convert_image_dtype(image, tf.float32) image = tf.reshape(image, (-1, )) return {'image-pixels': image}, label[..., tf.newaxis]