test1
This commit is contained in:
parent
3c354c2357
commit
a8c21517ce
@ -27,7 +27,7 @@ pd.set_option('display.max_columns', 30*30 + 1)
|
||||
|
||||
|
||||
|
||||
import tensorflow as tf
|
||||
#import tensorflow as tf
|
||||
def load_train_images(container_path, newSize=(64, 64), interpol=cv.INTER_AREA, colorConv=None):
|
||||
'''
|
||||
Obraz jest wczytywany przez funkcję imread z skimage (zwraca ndr)
|
||||
|
239
loadImages.py.bak
Normal file
239
loadImages.py.bak
Normal file
@ -0,0 +1,239 @@
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import os
|
||||
from skimage.io import imread
|
||||
import cv2 as cv
|
||||
from pathlib import Path
|
||||
import random
|
||||
from shutil import copyfile, rmtree
|
||||
import json
|
||||
|
||||
|
||||
import seaborn as sns
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
import matplotlib
|
||||
|
||||
|
||||
|
||||
from keras.preprocessing import image
|
||||
from sklearn.model_selection import GridSearchCV, train_test_split
|
||||
import warnings
|
||||
warnings.filterwarnings('ignore')
|
||||
pd.set_option('display.max_columns', 30*30 + 1)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
import tensorflow as tf
|
||||
def load_train_images(container_path, newSize=(64, 64), interpol=cv.INTER_AREA, colorConv=None):
|
||||
'''
|
||||
Obraz jest wczytywany przez funkcję imread z skimage (zwraca ndr)
|
||||
|
||||
:param container_path: katalog z podkatalogami oznaczającymi kategorie do klasyfikacji
|
||||
:param newSize: docelowey rozmiar obrazu dla funkcji resize z OpenCV
|
||||
:param interpol: typ interpolacji rozmiaru obrazu dla funkcji resize z OpenCV
|
||||
:param colorConv: stała konwersji - konversja przez funkcję cvtColor z OpenCV
|
||||
:return:
|
||||
'''
|
||||
|
||||
image_dir = Path(container_path)
|
||||
categories_name = []
|
||||
for file in os.listdir(image_dir):
|
||||
d = os.path.join(image_dir, file)
|
||||
if os.path.isdir(d):
|
||||
categories_name.append(file)
|
||||
|
||||
folders = [directory for directory in image_dir.iterdir() if directory.is_dir()]
|
||||
|
||||
train_img = []
|
||||
categories_count=[]
|
||||
labels=[]
|
||||
for i, direc in enumerate(folders):
|
||||
count = 0
|
||||
for obj in direc.iterdir():
|
||||
if os.path.isfile(obj) and os.path.basename(os.path.normpath(obj)) != 'desktop.ini':
|
||||
labels.append(os.path.basename(os.path.normpath(direc)))
|
||||
count += 1
|
||||
img = imread(obj)#zwraca ndarry postaci xSize x ySize x colorDepth
|
||||
if colorConv:# przykładowo: cv.COLOR_BGR2GRAY
|
||||
img = cv.cvtColor(img, colorConv)# zwraca ndarray, , domyślnie rgb zwraca jako bgr
|
||||
img = cv.resize(img, newSize, interpolation=interpol)# zwraca ndarray
|
||||
img = img / 255#normalizacja
|
||||
train_img.append(img)
|
||||
categories_count.append(count)
|
||||
X={}
|
||||
X["data"] = np.array(train_img)
|
||||
X["categories_name"] = categories_name
|
||||
X["categories_count"] = categories_count
|
||||
X["labels"]=labels
|
||||
return X
|
||||
|
||||
def load_test_images(container_path, newSize=(64, 64), interpol=cv.INTER_AREA, colorConv=None):
|
||||
'''
|
||||
Obraz jest wczytywany przez funkcję imread z skimage (zwraca ndr)
|
||||
|
||||
:param container_path: katalog z podkatalogami oznaczającymi kategorie do klasyfikacji
|
||||
:param newSize: docelowey rozmiar obrazu dla funkcji resize z OpenCV
|
||||
:param interpol: typ interpolacji rozmiaru obrazu dla funkcji resize z OpenCV
|
||||
:param colorConv: stała konwersji - konversja przez funkcję cvtColor z OpenCV
|
||||
:return:
|
||||
'''
|
||||
|
||||
|
||||
|
||||
image_path = Path(container_path)
|
||||
|
||||
labels_path = image_path.parents[0] / 'test_labels.json'
|
||||
|
||||
#with labels_path.open("r", encoding ="utf-8") as f:
|
||||
jsonString = labels_path.read_text()
|
||||
objects = json.loads(jsonString)
|
||||
|
||||
print(objects)
|
||||
|
||||
categories_name = []
|
||||
categories_count=[]
|
||||
count = 0
|
||||
c = objects[0]['value']
|
||||
for e in objects:
|
||||
if e['value'] != c:
|
||||
print(count)
|
||||
print(c)
|
||||
categories_count.append(count)
|
||||
c = e['value']
|
||||
count = 1
|
||||
else:
|
||||
count += 1
|
||||
if not e['value'] in categories_name:
|
||||
categories_name.append(e['value'])
|
||||
|
||||
categories_count.append(count)
|
||||
|
||||
|
||||
|
||||
test_img = []
|
||||
|
||||
labels=[]
|
||||
for e in objects:
|
||||
p = image_path / e['filename']
|
||||
img = imread(p)#zwraca ndarry postaci xSize x ySize x colorDepth
|
||||
if colorConv:# przykładowo: cv.COLOR_BGR2GRAY
|
||||
img = cv.cvtColor(img, colorConv)# zwraca ndarray, , domyślnie rgb zwraca jako bgr
|
||||
img = cv.resize(img, newSize, interpolation=interpol)# zwraca ndarray
|
||||
img = img / 255#normalizacja
|
||||
test_img.append(img)
|
||||
labels.append(e['value'])
|
||||
|
||||
|
||||
X={}
|
||||
X["data"] = np.array(test_img)
|
||||
X["categories_name"] = categories_name
|
||||
X["categories_count"] = categories_count
|
||||
X["labels"]=labels
|
||||
return X
|
||||
|
||||
|
||||
def gen_train_test(container_dir):
|
||||
'''
|
||||
Zwraca podkatalogi train i test wewnątrz katalogu z listą podkatalogów oznaczających katagorie
|
||||
Podkatalog train ma strukturę identyczną z oryginalną (podkatalogi kategorii)
|
||||
Podkatalog test zawiera listę plików testowych i plik .csv z dwukolumnową kolumną: nazwa pliku, nazwa klasy
|
||||
:param X:
|
||||
:return:
|
||||
'''
|
||||
image_path = Path(container_dir)
|
||||
categories_name = []
|
||||
for file in os.listdir(image_path):
|
||||
d = os.path.join(image_path, file)
|
||||
if os.path.isdir(d):
|
||||
categories_name.append(file)
|
||||
|
||||
folders = [directory for directory in image_path.iterdir() if directory.is_dir()]#zwraca listę obiektów Path
|
||||
|
||||
train_test_image_path = image_path.absolute().parents[0] / 'train_test_sw'
|
||||
|
||||
if not train_test_image_path.is_dir():
|
||||
train_test_image_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
train_image_path = train_test_image_path / 'train_sw'
|
||||
test_image_path = train_test_image_path / 'test_sw'
|
||||
|
||||
if train_image_path.is_dir():
|
||||
rmtree(train_image_path)
|
||||
|
||||
if test_image_path.is_dir():
|
||||
rmtree(test_image_path)
|
||||
|
||||
train_image_path.mkdir(parents=True, exist_ok=True)
|
||||
test_image_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
labels = []
|
||||
|
||||
for i, direc in enumerate(folders):
|
||||
lf = list(direc.glob('*'))
|
||||
count = len(lf)
|
||||
train_filenames = random.sample(lf, int(0.8*count))
|
||||
|
||||
train_category_image_path = train_image_path / direc.parts[len(direc.parts)-1]
|
||||
|
||||
train_category_image_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
for src_path in train_filenames:
|
||||
dest_path = train_image_path / src_path.parts[len(src_path.parts)-2] / src_path.parts[len(src_path.parts)-1]
|
||||
copyfile(src_path,dest_path)
|
||||
|
||||
|
||||
for src_path in direc.glob('*'):
|
||||
if not src_path in train_filenames:
|
||||
dest_path = test_image_path / src_path.parts[len(src_path.parts)-1]
|
||||
copyfile(src_path,dest_path)
|
||||
labels.append({"filename": src_path.parts[len(src_path.parts)-1], "value":src_path.parts[len(src_path.parts)-2]})
|
||||
|
||||
print(dest_path)
|
||||
|
||||
|
||||
|
||||
jsonString = json.dumps(labels, indent=4)
|
||||
print(jsonString)
|
||||
|
||||
json_path = train_test_image_path / "test_labels.json"
|
||||
|
||||
with json_path.open("w", encoding ="utf-8") as f:
|
||||
f.write(jsonString)
|
||||
|
||||
print("koniec")
|
||||
|
||||
def show_image_corelations(X):
|
||||
matplotlib.use('TkAgg')
|
||||
|
||||
# Apply the default theme
|
||||
sns.set_theme()
|
||||
|
||||
sns.relplot(
|
||||
data=X,
|
||||
x="categories_name", y="categories_count",
|
||||
)
|
||||
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
def show_categories(X, dim = (2,0,1)):
|
||||
'''
|
||||
Wizualizacja kategorii: 1D,2D
|
||||
:param X: słownik zwracany przez funkcję load_images
|
||||
:param dim: wymiar i współrzędne wizualizacji - krotka (n,mx,my): n - wymiar, mx,my: wizualizowane indeksy składowych obrazu
|
||||
(1,0,0) - dane monochromatyczne - wizualizcja 1D
|
||||
(2,mx,my) - dane 3D lub 4D, (mx,my) dwa indeksy piksela obrazu ze zbioru {0,1,2}
|
||||
:return:
|
||||
'''
|
||||
|
||||
if dim == (1,0,0):
|
||||
#wizualizacja 1D
|
||||
print("test")
|
||||
else:
|
||||
#wizualizacja 2D danych X["data"]
|
||||
print("test")
|
Loading…
Reference in New Issue
Block a user