-
Notifications
You must be signed in to change notification settings - Fork 13
/
processing.py
164 lines (145 loc) · 4.98 KB
/
processing.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cv2
from glob import glob
import numpy as np
"""
PROCESSING
This script contains useful functions for processing images used for ASL
Classification
Signum: Software Design SP18 Final Project
Isaac Vandor, Utsav Gupta, Diego Berny
"""
'''
def preprocess_frame(directory, img_format="jpg", size=200,
gray=False):
"""Pre-processing for frames captured from video stored in single directory.
Useful for dataset creation
Parameters
----------
directory: str
Path to directory containing images to be processed
img_format: str
Format of image to be loaded; must be one of either 'jpg' or 'png'
default = 'jpg'
size: int
Size to which image is re-sized (square of shape: size x size)
gray: bool
Whether to convert image to gray scale
Returns
-------
images: np.ndarray
4D array of processed images
"""
assert img_format in ["jpg", "png"], "img_format parameter must be one of 'jpg' or 'png'"
img_format = "*." + img_format
nb_images = len(glob(directory + img_format))
assert nb_images > 0, "No images found in directory"
num_channels = 3
images = np.empty((nb_images, size, size, num_channels))
for i, infile in enumerate(glob(directory + img_format)):
img = cv2.imread(infile)
if drop_green:
img[:, :, 1] = 0
if gray:
num_channels = 1
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = square_pad(img)
img = cv2.resize(img, (size, size))
img = np.reshape(img, (1, size, size, num_channels))
images[i, :, :, :] = img
return images
'''
def square_pad(img, padding_color=[0, 0, 0]):
"""Add margins to image to make it square keeping largest original dimension
Parameters
----------
img: numpy.ndarray
Image to be processed
padding_color: list
Define background colour to pad image; preserves RGB/BGR colour channel order of img
Returns
-------
padded_img: np.ndarray
Image padded to a square shape
"""
height = img.shape[0]
width = img.shape[1]
# find difference between longest side
diff = np.abs(width - height)
# amount of padding = half the diff between width and height
pad_diff = diff // 2
if height > width:
# letter is longer than it is wide
pad_top = 0
pad_bottom = 0
pad_left = pad_diff
pad_right = pad_diff
padded_img = cv2.copyMakeBorder(img,
top=pad_top,
bottom=pad_bottom,
left=pad_left,
right=pad_right,
borderType=cv2.BORDER_CONSTANT,
value=padding_color)
elif width > height:
# image is wide
pad_top = pad_diff
pad_bottom = pad_diff
pad_left = 0
pad_right = 0
padded_img = cv2.copyMakeBorder(img,
top=pad_top,
bottom=pad_bottom,
left=pad_left,
right=pad_right,
borderType=cv2.BORDER_CONSTANT,
value=padding_color)
elif width == height:
padded_img = img.copy()
return padded_img
def preprocess(img, size=200, color=True):
"""Image pre-processing
Parameters
----------
img: numpy.ndarray
Image to be processed
size: int
Size to which image is re-sized (square of shape: size x size)
color: bool
If the image is colour (BGR colour channels), then it is zero-centred by mean pixel
Returns
-------
x: np.ndarray
Pre-processed image ready to feed into VGG16 network; re-shaped to (1, size, size, 3)
"""
img = cv2.resize(img, (size, size))
img = img.astype(np.float32)/255.0
img = img[:,:,::-1]
x = np.expand_dims(img, axis=0)
return x
'''
def edit_bg(img, bg_img_path):
"""Change black background to another image pixel-by-pixel
Parameters
----------
img: np.ndarray
Image to be processed; must have BLACK BACKGROUND
bg_img_path: str
Path to background image on which to superimpose original image
Returns
-------
img_front: np.ndarray
Original image superimposed on to new background image; black pixels are replaced by background image
"""
img_front = img.copy()
img_back = cv2.imread(bg_img_path)
height, width = img_front.shape[:2]
resize_back = cv2.resize(img_back, (width, height), interpolation=cv2.INTER_CUBIC)
for i in range(width):
for j in range(height):
pixel = img_front[j, i]
if np.all(pixel == [0, 0, 0]):
img_front[j, i] = resize_back[j, i]
return img_front
'''