-
Notifications
You must be signed in to change notification settings - Fork 0
/
black_out_h.py
111 lines (91 loc) · 3.53 KB
/
black_out_h.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
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 3 14:35:47 2020
@author: safayet_khan
"""
import os
import glob
import csv
import numpy as np
import pandas as pd
import cv2
import matplotlib.image as mpimg
IMAGE_SIZE = 112
CHANNEL_NUMBER = 3
IMAGE_SIZE_3D = (IMAGE_SIZE, IMAGE_SIZE, CHANNEL_NUMBER)
IMAGE_SIZE_2D = (IMAGE_SIZE, IMAGE_SIZE)
MAIN_DIR = 'C:/Users/safayet_khan/Desktop/BCR/Numta/numta/'
FOLDER_LIST = [['training-a', '*.png'], ['training-b', '*.png'],
['training-c', '*.png'], ['training-d', '*.png'],
['training-e', '*.png'], ['training-f', '*.jpg'],
['training-g', '*.jpg']]
CSV_LIST = ['training-a.csv', 'training-b.csv', 'training-c.csv',
'training-d.csv', 'training-e.csv', 'training-f-bit.csv',
'training-g-color.csv']
image_path = []
data_frame = pd.DataFrame()
for i in range(np.shape(FOLDER_LIST)[0]):
temp_image_path = glob.glob(os.path.join(MAIN_DIR, FOLDER_LIST[i][0],
FOLDER_LIST[i][1]))
image_path += temp_image_path
print(np.shape(image_path))
for i in range(np.shape(CSV_LIST)[0]):
temp_csv_path = os.path.join(MAIN_DIR, CSV_LIST[i])
temp_data_frame = pd.read_csv(temp_csv_path)
temp_data_frame = temp_data_frame[['filename', 'digit']]
data_frame = data_frame.append(temp_data_frame)
data_frame = data_frame.set_index('filename')
print(np.shape(data_frame))
y_label = np.empty((np.shape(image_path)[0], 1), dtype=np.uint8)
x_array = np.empty((np.shape(image_path)[0], IMAGE_SIZE_3D[0],
IMAGE_SIZE_3D[1], IMAGE_SIZE_3D[2]), dtype=np.uint8)
for i, path in enumerate(image_path):
name = path.split(sep=os.path.sep)[-1]
y_label[i, ] = (data_frame.loc[name]['digit'])
img_array = cv2.imread(path, cv2.IMREAD_COLOR)
img_array = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)
img_array = cv2.resize(src=img_array, dsize=IMAGE_SIZE_2D)
x_array[i, ] = img_array
print(np.shape(x_array))
print(np.shape(y_label))
def black_out(old_image):
'''
Parameters
----------
old_image : randomly selected image from the training directory
Returns
-------
new_image : return an image with part of it blacked out.
'''
new_image = old_image.copy()
for _ in range(np.random.randint(2, 5)):
y_axis = np.random.randint(0, 85)
y_axis_lim = np.random.randint(10, 25)
x_axis = np.random.randint(0, 85)
x_axis_lim = np.random.randint(10, 25)
new_image[y_axis:(y_axis+y_axis_lim),
x_axis:(x_axis+x_axis_lim), :] = 0
return new_image
SAVE_INFO = ['training-h', 'training-h-blackout.csv']
WRITE_PATH = os.path.join(MAIN_DIR, SAVE_INFO[0])
WRITE_CSV = os.path.join(MAIN_DIR, SAVE_INFO[1])
COUNTER = 0
NUM_NEW_IMAGE = 10000
name_label = [['filename', 'digit']]
if not os.path.exists(path=WRITE_PATH):
os.mkdir(path=WRITE_PATH)
os.chdir(path=WRITE_PATH)
for img_array in range(0, NUM_NEW_IMAGE):
name_label_row = []
FILE_NAME = 'h{:05d}.jpg'.format(COUNTER)
name_label_row.append(FILE_NAME)
image_index = np.random.randint(0, x_array.shape[0])
name_label_row.append(int(y_label[image_index]))
name_label.append(name_label_row)
noisy_image = black_out(x_array[image_index])
mpimg.imsave(FILE_NAME, noisy_image)
COUNTER = COUNTER+1
print(np.shape(noisy_image))
with open(WRITE_CSV, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(name_label)