-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild_database.py
86 lines (57 loc) · 2.23 KB
/
build_database.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
import os
import numpy as np
from sklearn import model_selection
import cv2
def load_non_porn_images(root_path: str) -> (list, list):
x = list()
y = list()
for file in os.listdir(root_path):
try:
image: np.ndarray = cv2.imread(os.path.join(root_path, file))
if image is None:
os.remove(os.path.join(root_path, file))
else:
x.append(cv2.resize(image, (200, 200), interpolation=cv2.INTER_AREA))
y.append(0)
except:
print(file)
exit(1)
return x, y
def load_porn_images(root_path: str, size: int) -> (list, list):
x = list()
y = list()
'''
o número de fotos pornograficas são maiores do que as não pornográficas
então limitei o valor para ficar iguais
'''
number_of_porn_pictures = 0
for file in os.listdir(root_path):
if number_of_porn_pictures < size:
image: np.ndarray = cv2.imread(os.path.join(root_path, file))
if image is None:
os.remove(os.path.join(root_path, file))
else:
x.append(cv2.resize(image, (200, 200), interpolation=cv2.INTER_AREA))
y.append(1)
number_of_porn_pictures += 1
return x, y
def split_and_save_database(x: np.ndarray, y: np.ndarray, file_name: str):
x_train, y_train, x_test, y_test = model_selection.train_test_split(x, y, test_size=0.4, random_state=42)
np.savez(file_name, x_train=x_train, y_train=y_train, x_test=x_test, y_test=y_test)
def build_database(file_name: str):
non_porn_path = "dataset/nonPorn"
porn_path = "dataset/porn"
size = len(os.listdir(non_porn_path))
print("Loading non porn images")
x_non_porn, y_non_porn = load_non_porn_images(non_porn_path)
print("Loading porn images")
x_porn, y_porn = load_porn_images(porn_path, size)
print("splitting and saving database")
split_and_save_database(np.array(x_non_porn + x_porn), np.array(y_non_porn + y_porn), file_name)
'''
algoritmo feito para limpar datasets, após mineração de dados
'''
def clearing_data(model: str, dataset_: str):
pass
if __name__ == '__main__':
build_database("Pornography_Database")