diff --git a/radius_estimation/scripts/csvmerge.py b/radius_estimation/scripts/csvmerge.py new file mode 100644 index 0000000..8db7068 --- /dev/null +++ b/radius_estimation/scripts/csvmerge.py @@ -0,0 +1,21 @@ +import os +import csv + +directory = 'denemedataset2' +csv_file = 'combined2.csv' + +header = ['Radius', 'Depth', 'BBox_X', 'BBox_Y'] + +# open the combined csv file for writing and write the header row +with open(csv_file, 'w', newline='') as file: + writer = csv.writer(file) + writer.writerow(header) + + # loop over each txt file in the directory and append its data to the csv file + for filename in os.listdir(directory): + if filename.endswith('.txt'): + with open(os.path.join(directory, filename), 'r') as txt_file: + data = txt_file.read().splitlines() + writer.writerows([row.split() for row in data]) + +print('Combined csv file saved successfully!') diff --git a/radius_estimation/scripts/dataset_generator.py b/radius_estimation/scripts/dataset_generator.py index 0677290..bc3283a 100755 --- a/radius_estimation/scripts/dataset_generator.py +++ b/radius_estimation/scripts/dataset_generator.py @@ -16,12 +16,12 @@ def __init__(self): self.previous_depth = None self.image_sub = rospy.Subscriber('/yolov7/yolov7', Detection2DArray, self.detection_callback) self.depth_image_sub = rospy.Subscriber('/camera/aligned_depth_to_color/image_raw', Image, self.depth_callback) - self.dataset_path = "/home/cenk/datasetxxxxxx/" + self.dataset_path = "/home/cenk/denemedataset2/" if not os.path.exists(self.dataset_path): os.makedirs(self.dataset_path) def detection_callback(self, detection_array): - radius = 6.685 + radius = 3.1 for detection in detection_array.detections: if detection.results[0].id == 32: # nesne ID'si 33 ise print("detected") diff --git a/radius_estimation/scripts/predict.py b/radius_estimation/scripts/predict.py new file mode 100644 index 0000000..0d3fe4f --- /dev/null +++ b/radius_estimation/scripts/predict.py @@ -0,0 +1,12 @@ +# Modeli kaydetme +import joblib +# Kaydedilen modeli yükleme +loaded_model = joblib.load("regression_model.joblib") + +# Gerçek zamanlı kullanım +distance = 0.366 +bbox_x = 205 +bbox_y = 170 +features = [[distance, bbox_x, bbox_y]] +radius = loaded_model.predict(features) +print("Tahmin edilen yarıçap:", radius) diff --git a/radius_estimation/scripts/trainmodel.py b/radius_estimation/scripts/trainmodel.py new file mode 100644 index 0000000..14373fc --- /dev/null +++ b/radius_estimation/scripts/trainmodel.py @@ -0,0 +1,53 @@ +# Gerekli kütüphaneleri yükleme +import numpy as np +import pandas as pd +from sklearn.model_selection import train_test_split, GridSearchCV +from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet + +# Verileri yükleme +data = pd.read_csv("combined.csv", header=None, names=["radius", "depth", "bbox_x", "bbox_y"]) + +# Verileri özellik ve hedef değişkenleri olarak ayırma +X = data.drop("radius", axis=1) +y = data["radius"] + +# Train ve test verilerini ayırma +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) + +# Ridge Regresyon modelini eğitme ve optimum hiperparametreleri arama +ridge = Ridge() +params = {'alpha': [0.001, 0.01, 0.1, 1, 10]} +grid_ridge = GridSearchCV(estimator=ridge, param_grid=params, scoring='neg_mean_squared_error', cv=5) +grid_ridge.fit(X_train, y_train) +print("Ridge Regresyon Modeli En İyi Parametreler:", grid_ridge.best_params_) + +# Lasso Regresyon modelini eğitme ve optimum hiperparametreleri arama +lasso = Lasso() +params = {'alpha': [0.001, 0.01, 0.1, 1, 10]} +grid_lasso = GridSearchCV(estimator=lasso, param_grid=params, scoring='neg_mean_squared_error', cv=5) +grid_lasso.fit(X_train, y_train) +print("Lasso Regresyon Modeli En İyi Parametreler:", grid_lasso.best_params_) + +# Elastic Net Regresyon modelini eğitme ve optimum hiperparametreleri arama +elastic = ElasticNet() +params = {'alpha': [0.001, 0.01, 0.1, 1, 10], 'l1_ratio': [0.1, 0.3, 0.5, 0.7, 0.9]} +grid_elastic = GridSearchCV(estimator=elastic, param_grid=params, scoring='neg_mean_squared_error', cv=5) +grid_elastic.fit(X_train, y_train) +print("Elastic Net Regresyon Modeli En İyi Parametreler:", grid_elastic.best_params_) + +# En iyi modeli seçme ve eğitme +best_model = None +best_score = -np.inf + +for model in [LinearRegression(), grid_ridge.best_estimator_, grid_lasso.best_estimator_, grid_elastic.best_estimator_]: + model.fit(X_train, y_train) + score = model.score(X_test, y_test) + print("Model:", model.__class__.__name__, "R^2 Score:", score) + if score > best_score: + best_score = score + best_model = model + +print(best_model) +# Modeli kaydetme +import joblib +joblib.dump(best_model, "regression_model.joblib")