Skip to content
Snippets Groups Projects
Commit bdd3b7fa authored by VIGEOLAS-CHOURY Paul's avatar VIGEOLAS-CHOURY Paul
Browse files

Merge branch 'gris' into 'main'

Gris

See merge request !1
parents 005a2d06 480c60fa
No related branches found
No related tags found
1 merge request!1Gris
...@@ -5,6 +5,8 @@ import pandas as pd ...@@ -5,6 +5,8 @@ import pandas as pd
import numpy as np import numpy as np
from sklearn.metrics import accuracy_score from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
from skimage.feature import graycomatrix, graycoprops
from sklearn.naive_bayes import GaussianNB
def buildSampleFromPath(path1, path2, size=0): def buildSampleFromPath(path1, path2, size=0):
...@@ -29,9 +31,45 @@ def buildSampleFromPath(path1, path2, size=0): ...@@ -29,9 +31,45 @@ def buildSampleFromPath(path1, path2, size=0):
return S return S
def computePixelBW(image): def computePixelBW_histo(image_gl):
return np.array(image.convert("L")) return image_gl.histogram()
def compute_glcm_caracteristics(image_gl):
image_arr= np.array(image_gl)
#print(image_arr.shape)
glcm = graycomatrix(image_arr, distances=[5], angles=[0], levels=256,
symmetric=True, normed=True)
return [graycoprops(glcm, 'dissimilarity')[0, 0], graycoprops(glcm, 'correlation')[0, 0], graycoprops(glcm, 'contrast')[0, 0],
graycoprops(glcm, 'energy')[0, 0], graycoprops(glcm, 'homogeneity')[0, 0]]
"""def compute_glcm(image):
img_array = np.array(image)
distance, angle = 5, 0
glcm = np.zeros((256, 256))
offsets = (int(distance * np.cos(angle)), int(distance * np.sin(angle)))
for i in range(image.height - abs(offsets[1])):
for j in range(image.width - abs(offsets[0])):
px1 = img_array[i, j]
px2 = img_array[i + offsets[1], j + offsets[0]]
glcm[px1, px2] += 1
glcm /= np.sum(glcm)
return glcm
def extract_data_glcm(glcm):
contrast, energy, homogeneity = 0, 0, 0
for i in range(glcm.shape[0]):
for j in range(glcm.shape[1]):
contrast += (i - j) ** 2 * glcm[i, j]
energy += glcm[i, j]**2
homogeneity += (glcm[i, j] / (1 + abs(i - j)))
contrast = np.sum([[(i - j) ** 2 * glcm[i, j] for j in range(glcm.shape[1])] for i in range(glcm.shape[0])])
energy = np.sum(glcm ** 2)
homogeneity = np.sum(
[[(glcm[i, j] / (1 + abs(i - j))) for j in range(glcm.shape[1])] for i in range(glcm.shape[0])])
return [contrast, energy, homogeneity]"""
def computeDict(image_path, path, y_true_value, max_size: tuple): def computeDict(image_path, path, y_true_value, max_size: tuple):
""" """
...@@ -48,13 +86,15 @@ def computeDict(image_path, path, y_true_value, max_size: tuple): ...@@ -48,13 +86,15 @@ def computeDict(image_path, path, y_true_value, max_size: tuple):
image = image.convert("RGB") image = image.convert("RGB")
resized = resizeImage(image, *max_size) # On ne stocke pas resized image, on calcule tout avant de l'oublier resized = resizeImage(image, *max_size) # On ne stocke pas resized image, on calcule tout avant de l'oublier
image_gl = resized.convert("L")
#print(computePixelBW_histo(resized))
return {"name_path": full_path, return {"name_path": full_path,
#"resized_image": resized, #"resized_image": resized,
"X_histo": computeHisto(resized), "X_histo": computeHisto(resized),
"X_pixelbw": computePixelBW(resized), "X_pixelbw": computePixelBW_histo(resized),
#"X_glcm_data": extract_data_glcm(compute_glcm(resized)),
"X_glcm_data": compute_glcm_caracteristics(image_gl),
"y_true_class": y_true_value, "y_true_class": y_true_value,
"y_predicted_class": None} "y_predicted_class": None}
...@@ -93,7 +133,10 @@ def fitFromHisto(S, algo): ...@@ -93,7 +133,10 @@ def fitFromHisto(S, algo):
S_train, S_test, y_train, y_test = train_test_split(S, y, test_size=0.2, random_state=42) S_train, S_test, y_train, y_test = train_test_split(S, y, test_size=0.2, random_state=42)
X_train = np.array([np.array(l["X_histo"]) for l in S_train]) X_train = np.array([np.array(l["X_histo"]+l["X_glcm_data"]) for l in S_train])
#X_train = df[["X_histo", "X_pixelbw"]]
#print(X_train)
#print(len(X_train[0]))
algo.fit(X_train, y_train) algo.fit(X_train, y_train)
...@@ -109,7 +152,7 @@ def predictFromHisto(S, model, list_dict=True): ...@@ -109,7 +152,7 @@ def predictFromHisto(S, model, list_dict=True):
:param list_dict: is the sample in list(dict) :param list_dict: is the sample in list(dict)
:return: None :return: None
""" """
tab = model.predict(np.array([x["X_histo"] for x in S])) tab = model.predict(np.array([x["X_histo"]+x["X_glcm_data"] for x in S]))
if list_dict: if list_dict:
for i in range(len(S)): for i in range(len(S)):
S[i]["y_predicted_class"] = tab[i] S[i]["y_predicted_class"] = tab[i]
......
...@@ -2,6 +2,7 @@ from PIL import Image ...@@ -2,6 +2,7 @@ from PIL import Image
from sklearn.naive_bayes import GaussianNB, CategoricalNB from sklearn.naive_bayes import GaussianNB, CategoricalNB
from sklearn.svm import SVC from sklearn.svm import SVC
import TP import TP
from xgboost import XGBClassifier
path1_t = "./Init/Mer" path1_t = "./Init/Mer"
path2_t = "./Init/Ailleurs" path2_t = "./Init/Ailleurs"
...@@ -28,7 +29,7 @@ def test_sample(): ...@@ -28,7 +29,7 @@ def test_sample():
def global_test(): def global_test():
S = TP.buildSampleFromPath(path1_t, path2_t) S = TP.buildSampleFromPath(path1_t, path2_t)
classifier, S_test, y_test, S_train, y_train = TP.fitFromHisto(S, SVC()) classifier, S_test, y_test, S_train, y_train = TP.fitFromHisto(S, SVC(kernel="rbf"))
TP.predictFromHisto(S, classifier) TP.predictFromHisto(S, classifier)
print("Erreur empirique :", TP.computeError(S_train), "erreurs") print("Erreur empirique :", TP.computeError(S_train), "erreurs")
print("Erreur réelle :", TP.computeError(S_test), "erreurs") print("Erreur réelle :", TP.computeError(S_test), "erreurs")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment