-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·87 lines (71 loc) · 3.07 KB
/
utils.py
File metadata and controls
executable file
·87 lines (71 loc) · 3.07 KB
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
"""
utils.py
Utility functions.
Written by Phil Ferriere
Licensed under the MIT License (see LICENSE for details)
"""
from __future__ import absolute_import, division, print_function
import os
import numpy as np
import tensorflow as tf
import cv2
def clean_dst_file(dst_file):
"""Create the output folder, if necessary; empty the output folder of previous predictions, if any
Args:
dst_file: Destination path
"""
# Create the output folder, if necessary
dst_file_dir = os.path.dirname(dst_file)
if not os.path.exists(dst_file_dir):
os.makedirs(dst_file_dir)
# Empty the output folder of previous predictions, if any
if os.path.exists(dst_file):
os.remove(dst_file)
def tf_where(condition, x=None, y=None, *args, **kwargs):
"""tf.where does not support broadcasting like its numpy equivaleny. This function implements it.
Args:
Same as tf.where
Refs:
- Broadcasting support in `tf.where
https://github.com/tensorflow/tensorflow/issues/9284#issuecomment-294778959
Written by Till Hoffmann
"""
if x is None and y is None:
return tf.where(condition, x, y, *args, **kwargs)
else:
_shape = tf.broadcast_dynamic_shape(tf.shape(condition), tf.shape(x))
_broadcaster = tf.ones(_shape)
return tf.where(condition & (_broadcaster > 0.0), x * _broadcaster, y * _broadcaster, *args, **kwargs)
def scale(img, zoom_factor):
"""
Center zoom in/out of the given image and returning an enlarged/shrinked view of
the image without changing dimensions
Args:
img : Image array
zoom_factor : amount of zoom as a ratio (0 to Inf)
Based on:
- Scipy rotate and zoom an image without changing its dimensions
https://stackoverflow.com/a/48097478
Written by Mohamed Ezz
License: MIT License
"""
height, width = img.shape[:2] # It's also the final desired shape
new_height, new_width = int(height * zoom_factor), int(width * zoom_factor)
# Crop only the part that will remain in the result (more efficient)
# Centered bbox of the final desired size in resized (larger/smaller) image coordinates
y1, x1 = max(0, new_height - height) // 2, max(0, new_width - width) // 2
y2, x2 = y1 + height, x1 + width
bbox = np.array([y1, x1, y2, x2])
# Map back to original image coordinates
bbox = (bbox / zoom_factor).astype(np.int)
y1, x1, y2, x2 = bbox
cropped_img = img[y1:y2, x1:x2]
# Handle padding when downscaling
resize_height, resize_width = min(new_height, height), min(new_width, width)
pad_height1, pad_width1 = (height - resize_height) // 2, (width - resize_width) // 2
pad_height2, pad_width2 = (height - resize_height) - pad_height1, (width - resize_width) - pad_width1
pad_spec = [(pad_height1, pad_height2), (pad_width1, pad_width2)] + [(0, 0)] * (img.ndim - 2)
result = cv2.resize(cropped_img, (resize_width, resize_height))
result = np.pad(result, pad_spec, mode='constant')
assert(result.shape[0] == height and result.shape[1] == width)
return result