-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathimage_coder.py
139 lines (107 loc) · 3.71 KB
/
image_coder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
# coding=utf8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
# use eager execution
import tensorflow as tf
tf.enable_eager_execution()
# decorator
def with_tf_cpu(fn):
def wrapper_fn(*args, **kwargs):
with tf.device('CPU:0'):
return fn(*args, **kwargs)
return wrapper_fn
@with_tf_cpu
def decode_png(contents):
image = tf.image.decode_png(contents, channels=3)
return image.numpy()
@with_tf_cpu
def decode_jpg(contents):
# 정확한 성능을 내기 위해서는 dct_method='INTEGER_ACCURATE' 를 사용해야 한다.
image = tf.image.decode_jpeg(contents, channels=3, dct_method='INTEGER_ACCURATE')
return image.numpy()
@with_tf_cpu
def decode_bmp(contents):
image = tf.image.decode_bmp(contents, channels=3)
return image.numpy()
@with_tf_cpu
def decode_gif(contents):
image = tf.image.decode_gif(contents).numpy()
if len(image.shape) == 4:
return image[0,]
return image
@with_tf_cpu
def decode(contents, image_type=None):
if image_type == 'jpg':
return decode_jpg(contents)
elif image_type == 'png':
return decode_png(contents)
elif image_type == 'bmp':
return decode_bmp(contents)
elif image_type == 'gif':
return decode_gif(contents)
image = tf.image.decode_image(contents).numpy()
if len(image.shape) == 4:
return image[0,]
return image
@with_tf_cpu
def encode_png(image):
image_data = tf.image.encode_png(image)
return image_data.numpy()
@with_tf_cpu
def encode_jpg(image):
image_data = tf.image.encode_jpeg(image, format='rgb', quality=100)
return image_data.numpy()
def crop(image, offset_height, offset_width, crop_height, crop_width):
assert len(image.shape) == 3
assert offset_height >= 0
assert offset_width >= 0
assert crop_height > 0
assert crop_width > 0
assert (offset_height + crop_height) <= image.shape[0]
assert (offset_width + crop_width) <= image.shape[1]
return image[offset_height:(offset_height + crop_height),
offset_width:(offset_width + crop_width)]
def crop_bbox(image, bbox=None):
if bbox is None:
return image
return crop(image, bbox[0], bbox[1], bbox[2], bbox[3])
@with_tf_cpu
def central_crop(image, central_fraction):
cropped_image = tf.image.central_crop(image, central_fraction)
return cropped_image.numpy()
def _smallest_size_at_least(height, width, smallest_side):
"""
Computes new shape with the smallest side equal to `smallest_side`.
:param height: A python integer indicating the current height.
:param width: A python integer indicating the current width.
:param smallest_side: A python integer indicating the size of the smallest side after resize.
:return: tuple (new_height, new_width) of int type.
"""
assert height > 0
assert width > 0
assert smallest_side > 0
scale = float(smallest_side) / min(height, width)
new_height = int(np.rint(height * scale))
new_width = int(np.rint(width * scale))
return (new_height, new_width)
@with_tf_cpu
def resize(image, height, width):
image = tf.image.resize_images(
image, [height, width],
method=tf.image.ResizeMethod.BILINEAR,
align_corners=False)
return image.numpy().astype(np.uint8)
@with_tf_cpu
def aspect_preserving_resize(image, resize_min):
"""
Resize images preserving the original aspect ratio.
:param image: A 3-D image `Tensor`.
:param resize_min: A python integer or scalar `Tensor` indicating the size ofthe smallest side after resize.
:return: A 3-D tensor containing the resized image.
"""
height, width = image.shape[0], image.shape[1]
new_height, new_width = _smallest_size_at_least(height, width, resize_min)
return resize(image, new_height, new_width), new_height, new_width