-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre_processing.py
48 lines (40 loc) · 1.26 KB
/
pre_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
import os
import numpy as np
import matplotlib.pyplot as plt
import random
import cv2 as cv
def convertor(path) -> str:
return '/'.join(path.split('\\'))
def data(path, number_of_folder=1):
data = []
for i in os.listdir(path)[:number_of_folder]:
print(f"Folder Name: {i}")
n_path = os.path.join(path,i)
for j in os.listdir(n_path):
data.append(cv.imread(os.path.join(n_path,j)))
return np.array(data)/255.0
def vis(data, no=25):
row_col = int(np.sqrt(no))
plt.figure(figsize=(15,12))
r = np.random.randint(data.shape[0], size=no)
for index,i in enumerate(r):
plt.subplot(row_col,row_col, index+1)
plt.imshow(data[i])
plt.axis('off')
plt.show()
def pair_data(data,positive=0.5):
pr_data = []
labels = []
a = hot_encoding([1,0])
for index,i in enumerate(data):
while True:
r = random.randint(0, data.shape[0]-1)
if index !=r:
break
pr_data += [[i,i]]
pr_data += [[i,data[r]]]
labels += [a[0],a[1]]
return np.array(pr_data),np.array(labels)
def hot_encoding(list_of_labels):
return np.eye(len(list_of_labels))
print(hot_encoding([1,2]))