Skip to content
Snippets Groups Projects
Commit 808f3445 authored by titouan's avatar titouan
Browse files

Correction formules d'erreurs et typos.

Problème d'erreurs constantes
parent 3bc8b4ae
No related branches found
No related tags found
No related merge requests found
import PIL
from PIL import Image
import pandas as pd
import sklearn
import os
path = "./Init/Mer"
img = Image.open("./Init/Mer/838s.jpg")
print("Original size:", img.size)
new_size = (img.width * 2, img.height * 2) # Upscaling 2x
resized_image = img.resize(new_size, Image.LANCZOS) # Nearest-neighbor upscaling
print("Resized size:", resized_image.size)
\ No newline at end of file
import PIL
from PIL import Image
import os
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
def computeHisto(image: PIL.Image.Image):
return image.histogram()
def get_max_size(path1, path2):
max_size_x, max_size_y = 0, 0
max_size_x, max_size_y = compute_max_size_in_folder(max_size_x, max_size_y, path1)
max_size_x, max_size_y = compute_max_size_in_folder(max_size_x, max_size_y, path2)
return max_size_x, max_size_y
def compute_max_size_in_folder(max_size_x, max_size_y, path1):
for image_path in os.listdir(path1):
full_path = os.path.join(path1, image_path)
image = Image.open(full_path)
if max_size_x * max_size_y < image.size[0] * image.size[1]:
max_size_x = image.size[0]
max_size_y = image.size[1]
return max_size_x, max_size_y
def resizeImage (i, h, l) :
return i.resize((h,l), Image.LANCZOS)
def buildSampleFromPath(path1, path2, size=0):
S = []
max_size = get_max_size(path1, path2)
for image_path in os.listdir(path1)[:size if size > 0 else -1]:
compute_dict(S, image_path, path1, 1, max_size)
for image_path in os.listdir(path2)[:size if size > 0 else -1]:
compute_dict(S, image_path, path2, -1, max_size)
return S
def compute_dict(S, image_path, path, y_true_value, max_size: tuple):
full_path = os.path.join(path, image_path)
image = Image.open(full_path)
image = image.convert("RGB")
resized = resizeImage(image, *max_size)
#print("resized")
S.append({"name_path": full_path,
"resized_image": resized,
"X_histo": computeHisto(resized),
"y_true_class": y_true_value,
"y_predicted_class": None})
def fitFromHisto(S, algo) :
df = pd.DataFrame(S)
#X = np.array(df["X_histo"])
X = np.array([np.array(l) for l in df["X_histo"]])
y = np.array(df["y_true_class"])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
algo.fit(X_train, y_train)
return algo
def predictFromHisto(S, model):
#for image in S:
# image["y_predicted_class"] = model.predict(np.array(image["X_histo"]).reshape(1,-1))
tab = model.predict(np.array([x["X_histo"] for x in S]))
for i in range(len(S)):
S[i]["y_predicted_class"] = tab[i]
def erreur_empirique(S):
error_count = 0
for image in S:
if image["y_true_class"] != image["y_predicted_class"]:
error_count += 1
return error_count/len(S)
......@@ -4,7 +4,6 @@ import os
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
def buildSampleFromPath(path1, path2, size=0):
......@@ -13,7 +12,7 @@ def buildSampleFromPath(path1, path2, size=0):
used to train and test the model
:param path1: path for the goods images, (score 1)
:param path2: path for the bad images, (score -1)
:param size: Optionnal if you want to restrict the image pool
:param size: Optional if you want to restrict the image pool
:return: list"""
S = []
......@@ -31,7 +30,7 @@ def buildSampleFromPath(path1, path2, size=0):
def computeDict(image_path, path, y_true_value, max_size: tuple):
"""
Middle function to construct each dict for each image. Resizing, and fetching the histogram,
by calling ohter functions
by calling other functions
:param image_path: relative path of the image in the folder
:param path: path of the folder containing the image
:param y_true_value: is the image a good one (1) or a wrong one (-1)
......@@ -149,7 +148,7 @@ def empiricalError(S):
for image in S:
if image["y_true_class"] != image["y_predicted_class"]:
error_count += 1
return error_count / len(S)
return error_count
def realError(S_test):
"""
......@@ -161,6 +160,6 @@ def realError(S_test):
for image in S_test:
if image["y_predicted_class"] != image["y_true_class"]:
error_count += 1
return error_count/len(S_test)
return error_count
import os
from PIL import Image
from sklearn.naive_bayes import GaussianNB
......@@ -29,14 +28,13 @@ def test_sample():
def real_test():
S = TP.buildSampleFromPath(path1_t, path2_t)
print("built")
classifieur, S_test, y_test = TP.fitFromHisto(S, GaussianNB())
TP.predictFromHisto(S, classifieur)
classifier, S_test, y_test = TP.fitFromHisto(S, GaussianNB())
TP.predictFromHisto(S, classifier)
print(TP.empiricalError(S))
print(TP.realError(S_test))
if __name__ == "__main__":
#image = Image.open("/home/paul_pvc/PycharmProjects/pythonProject/TP_IA_L3/Init/Mer/838s.jpg")
#image = Image.open("./Init/Mer/838s.jpg")
test_Histogram()
#test_resizeImage(image, 1000, 1000)
real_test()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment