|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2020 The TensorFlow Datasets Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +r"""Generate testing images for Imagewang. |
| 17 | +
|
| 18 | +Each image is a black 1 by 1 image. |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import absolute_import |
| 22 | +from __future__ import division |
| 23 | +from __future__ import print_function |
| 24 | + |
| 25 | +import os |
| 26 | + |
| 27 | +from absl import app |
| 28 | +from absl import flags |
| 29 | +import numpy as np |
| 30 | +import tensorflow as tf |
| 31 | + |
| 32 | +from tensorflow_datasets.core.utils import py_utils |
| 33 | +from tensorflow_datasets.testing import test_utils |
| 34 | + |
| 35 | +flags.DEFINE_string("tfds_dir", py_utils.tfds_dir(), |
| 36 | + "Path to tensorflow_datasets directory") |
| 37 | +FLAGS = flags.FLAGS |
| 38 | + |
| 39 | +_IMAGE_FILENAME = 'test_image' |
| 40 | +_LABEL_DIRNAME = 'label' |
| 41 | + |
| 42 | +_SIZES = ["full-size", "320px", "160px"] |
| 43 | +_SIZE_TO_DIRNAME = { |
| 44 | + "full-size": "imagewang", |
| 45 | + "320px": "imagewang-320", |
| 46 | + "160px": "imagewang-160" |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +def examples_dir(): |
| 51 | + return os.path.join(FLAGS.tfds_dir, "testing", "test_data", "fake_examples") |
| 52 | + |
| 53 | + |
| 54 | +def imagewang_dir(size): |
| 55 | + dir_name = _SIZE_TO_DIRNAME[size] |
| 56 | + return os.path.join(examples_dir(), 'imagewang', dir_name) |
| 57 | + |
| 58 | + |
| 59 | +def imagewang_label_dir(size): |
| 60 | + return os.path.join(imagewang_dir(size), _LABEL_DIRNAME) |
| 61 | + |
| 62 | + |
| 63 | +def make_image(): |
| 64 | + return np.random.randint(256, size=(1 * 1), dtype=np.uint8) |
| 65 | + |
| 66 | + |
| 67 | +def write_image_file(filename): |
| 68 | + with tf.io.gfile.GFile(filename, "wb") as f: |
| 69 | + f.write(b"1" * 16) # header |
| 70 | + f.write(make_image().tobytes()) |
| 71 | + |
| 72 | + |
| 73 | +def main(_): |
| 74 | + for size in _SIZES: |
| 75 | + output_dir = imagewang_label_dir(size) |
| 76 | + test_utils.remake_dir(output_dir) |
| 77 | + write_image_file(os.path.join(output_dir, _IMAGE_FILENAME)) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + app.run(main) |
0 commit comments