def _parse_fn(example_serialized, is_training): """Helper function for parse_fn_train() and parse_fn_valid() Each Example proto (TFRecord) contains the following fields: image/height: 462 image/width: 581 image/colorspace: 'RGB' image/channels: 3 image/class/label: 615 image/class/synset: 'n03623198' image/class/text: 'knee pad' image/format: 'JPEG' image/filename: 'ILSVRC2012_val_00041207.JPEG' image/encoded: <JPEG encoded string> Args: example_serialized: scalar Tensor tf.string containing a serialized Example protocol buffer. is_training: training (True) or validation (False). Returns: image_buffer: Tensor tf.string containing the contents of a JPEG file. label: Tensor tf.int32 containing the label. text: Tensor tf.string containing the human-readable label. """ feature_map = { 'image/encoded': tf.FixedLenFeature([], dtype=tf.string, default_value=''), 'image/class/label': tf.FixedLenFeature([], dtype=tf.int64, default_value=-1), 'image/class/text': tf.FixedLenFeature([], dtype=tf.string, default_value=''), } parsed = tf.parse_single_example(example_serialized, feature_map) image = decode_jpeg(parsed['image/encoded']) if config.DATA_AUGMENTATION: image = preprocess_image(image, 299, 299, is_training=is_training) else: image = resize_and_rescale_image(image, 299, 299) # image = 1 - image ##tf.print(image) #arr = tf.keras.preprocessing.image.img_to_array(image) #print(arr.shape) #with tf.Session() as sess: # tf.print(sess.run(image)) #exit() # The label in the tfrecords is 1~1000 (0 not used). # So I think the minus 1 (of class label) is needed below. label = tf.one_hot(parsed['image/class/label'] - 1, 1000, dtype=tf.float32) return (image, label)
def run(): try: frame1_url = request.args.get('img1') frame2_url = request.args.get('img2') frame1_path = get_image_from_url(https_url=frame1_url, file_id="1") frame2_path = get_image_from_url(https_url=frame2_url, file_id="2") processed_frame1_path = preprocess_image(frame_path=frame1_path, file_id="1") processed_frame2_path = preprocess_image(frame_path=frame2_path, file_id="2") _, similarity = compare_two_images(frame1_path=processed_frame1_path, frame2_path=processed_frame2_path) ss = 1 - math.exp(EXP_CONST * similarity) data = {'score': ss} response = json.dumps(data) for path in glob.glob(os.path.join(CUR_DIR, 'utils', '*.jpg')): os.remove(path) return response except Exception as e: log_print(info_str=e) data = {'score': 0} response = json.dumps(data) return response
def _parse_fn(example_serialized, is_training): """Helper function for parse_fn_train() and parse_fn_valid() Each Example proto (TFRecord) contains the following fields: image/height: 462 image/width: 581 image/colorspace: 'RGB' image/channels: 3 image/class/label: 615 image/class/synset: 'n03623198' image/class/text: 'knee pad' image/format: 'JPEG' image/filename: 'ILSVRC2012_val_00041207.JPEG' image/encoded: <JPEG encoded string> Args: example_serialized: scalar Tensor tf.string containing a serialized Example protocol buffer. Returns: image_buffer: Tensor tf.string containing the contents of a JPEG file. label: Tensor tf.int32 containing the label. text: Tensor tf.string containing the human-readable label. """ feature_map = { 'image/encoded': tf.FixedLenFeature([], dtype=tf.string, default_value=''), 'image/class/label': tf.FixedLenFeature([], dtype=tf.int64, default_value=-1), 'image/class/text': tf.FixedLenFeature([], dtype=tf.string, default_value=''), } parsed = tf.parse_single_example(example_serialized, feature_map) image = decode_jpeg(parsed['image/encoded']) if config.DATA_AUGMENTATION: image = preprocess_image(image, 224, 224, is_training=is_training) else: image = resize_and_rescale_image(image, 224, 224) label = tf.one_hot(parsed['image/class/label'], 1000, dtype=tf.float32) return (image, label)