-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
boss_input.py
66 lines (49 loc) · 1.67 KB
/
boss_input.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
# -*- coding: utf-8 -*-
import os
import numpy as np
import cv2
IMAGE_SIZE = 64
def resize_with_pad(image, height=IMAGE_SIZE, width=IMAGE_SIZE):
def get_padding_size(image):
h, w, _ = image.shape
longest_edge = max(h, w)
top, bottom, left, right = (0, 0, 0, 0)
if h < longest_edge:
dh = longest_edge - h
top = dh // 2
bottom = dh - top
elif w < longest_edge:
dw = longest_edge - w
left = dw // 2
right = dw - left
else:
pass
return top, bottom, left, right
top, bottom, left, right = get_padding_size(image)
BLACK = [0, 0, 0]
constant = cv2.copyMakeBorder(image, top , bottom, left, right, cv2.BORDER_CONSTANT, value=BLACK)
resized_image = cv2.resize(constant, (height, width))
return resized_image
images = []
labels = []
def traverse_dir(path):
for file_or_dir in os.listdir(path):
abs_path = os.path.abspath(os.path.join(path, file_or_dir))
print(abs_path)
if os.path.isdir(abs_path): # dir
traverse_dir(abs_path)
else: # file
if file_or_dir.endswith('.jpg'):
image = read_image(abs_path)
images.append(image)
labels.append(path)
return images, labels
def read_image(file_path):
image = cv2.imread(file_path)
image = resize_with_pad(image, IMAGE_SIZE, IMAGE_SIZE)
return image
def extract_data(path):
images, labels = traverse_dir(path)
images = np.array(images)
labels = np.array([0 if label.endswith('boss') else 1 for label in labels])
return images, labels