Spaces:
Runtime error
Runtime error
Upload tfimage.py
Browse files- tools/tfimage.py +146 -0
tools/tfimage.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import absolute_import
|
| 2 |
+
from __future__ import division
|
| 3 |
+
from __future__ import print_function
|
| 4 |
+
|
| 5 |
+
import tensorflow.compat.v1 as tf
|
| 6 |
+
tf.disable_v2_behavior()
|
| 7 |
+
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def create_op(func, **placeholders):
|
| 12 |
+
op = func(**placeholders)
|
| 13 |
+
|
| 14 |
+
def f(**kwargs):
|
| 15 |
+
feed_dict = {}
|
| 16 |
+
for argname, argvalue in kwargs.items():
|
| 17 |
+
placeholder = placeholders[argname]
|
| 18 |
+
feed_dict[placeholder] = argvalue
|
| 19 |
+
return tf.get_default_session().run(op, feed_dict=feed_dict)
|
| 20 |
+
|
| 21 |
+
return f
|
| 22 |
+
|
| 23 |
+
downscale = create_op(
|
| 24 |
+
func=tf.image.resize,
|
| 25 |
+
images=tf.placeholder(tf.float32, [None, None, None]),
|
| 26 |
+
size=tf.placeholder(tf.int32, [2]),
|
| 27 |
+
method=tf.image.ResizeMethod.AREA,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
upscale = create_op(
|
| 31 |
+
func=tf.image.resize_images,
|
| 32 |
+
images=tf.placeholder(tf.float32, [None, None, None]),
|
| 33 |
+
size=tf.placeholder(tf.int32, [2]),
|
| 34 |
+
method=tf.image.ResizeMethod.BICUBIC,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
decode_jpeg = create_op(
|
| 38 |
+
func=tf.image.decode_jpeg,
|
| 39 |
+
contents=tf.placeholder(tf.string),
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
decode_png = create_op(
|
| 43 |
+
func=tf.image.decode_png,
|
| 44 |
+
contents=tf.placeholder(tf.string),
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
rgb_to_grayscale = create_op(
|
| 48 |
+
func=tf.image.rgb_to_grayscale,
|
| 49 |
+
images=tf.placeholder(tf.float32),
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
grayscale_to_rgb = create_op(
|
| 53 |
+
func=tf.image.grayscale_to_rgb,
|
| 54 |
+
images=tf.placeholder(tf.float32),
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
encode_jpeg = create_op(
|
| 58 |
+
func=tf.image.encode_jpeg,
|
| 59 |
+
image=tf.placeholder(tf.uint8),
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
encode_png = create_op(
|
| 63 |
+
func=tf.image.encode_png,
|
| 64 |
+
image=tf.placeholder(tf.uint8),
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
crop = create_op(
|
| 68 |
+
func=tf.image.crop_to_bounding_box,
|
| 69 |
+
image=tf.placeholder(tf.float32),
|
| 70 |
+
offset_height=tf.placeholder(tf.int32, []),
|
| 71 |
+
offset_width=tf.placeholder(tf.int32, []),
|
| 72 |
+
target_height=tf.placeholder(tf.int32, []),
|
| 73 |
+
target_width=tf.placeholder(tf.int32, []),
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
pad = create_op(
|
| 77 |
+
func=tf.image.pad_to_bounding_box,
|
| 78 |
+
image=tf.placeholder(tf.float32),
|
| 79 |
+
offset_height=tf.placeholder(tf.int32, []),
|
| 80 |
+
offset_width=tf.placeholder(tf.int32, []),
|
| 81 |
+
target_height=tf.placeholder(tf.int32, []),
|
| 82 |
+
target_width=tf.placeholder(tf.int32, []),
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
to_uint8 = create_op(
|
| 86 |
+
func=tf.image.convert_image_dtype,
|
| 87 |
+
image=tf.placeholder(tf.float32),
|
| 88 |
+
dtype=tf.uint8,
|
| 89 |
+
saturate=True,
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
to_float32 = create_op(
|
| 93 |
+
func=tf.image.convert_image_dtype,
|
| 94 |
+
image=tf.placeholder(tf.uint8),
|
| 95 |
+
dtype=tf.float32,
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def load(path):
|
| 100 |
+
with open(path, "rb") as f:
|
| 101 |
+
contents = f.read()
|
| 102 |
+
|
| 103 |
+
_, ext = os.path.splitext(path.lower())
|
| 104 |
+
|
| 105 |
+
if ext == ".jpg":
|
| 106 |
+
image = decode_jpeg(contents=contents)
|
| 107 |
+
elif ext == ".png":
|
| 108 |
+
image = decode_png(contents=contents)
|
| 109 |
+
else:
|
| 110 |
+
raise Exception("invalid image suffix")
|
| 111 |
+
|
| 112 |
+
return to_float32(image=image)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def find(d):
|
| 116 |
+
result = []
|
| 117 |
+
for filename in os.listdir(d):
|
| 118 |
+
_, ext = os.path.splitext(filename.lower())
|
| 119 |
+
if ext == ".jpg" or ext == ".png":
|
| 120 |
+
result.append(os.path.join(d, filename))
|
| 121 |
+
result.sort()
|
| 122 |
+
return result
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def save(image, path, replace=False):
|
| 126 |
+
_, ext = os.path.splitext(path.lower())
|
| 127 |
+
image = to_uint8(image=image)
|
| 128 |
+
if ext == ".jpg":
|
| 129 |
+
encoded = encode_jpeg(image=image)
|
| 130 |
+
elif ext == ".png":
|
| 131 |
+
encoded = encode_png(image=image)
|
| 132 |
+
else:
|
| 133 |
+
raise Exception("invalid image suffix")
|
| 134 |
+
|
| 135 |
+
dirname = os.path.dirname(path)
|
| 136 |
+
if dirname != "" and not os.path.exists(dirname):
|
| 137 |
+
os.makedirs(dirname)
|
| 138 |
+
|
| 139 |
+
if os.path.exists(path):
|
| 140 |
+
if replace:
|
| 141 |
+
os.remove(path)
|
| 142 |
+
else:
|
| 143 |
+
raise Exception("file already exists at " + path)
|
| 144 |
+
|
| 145 |
+
with open(path, "wb") as f:
|
| 146 |
+
f.write(encoded)
|