Skip to content
Snippets Groups Projects
Commit 79a278f2 authored by VictorD's avatar VictorD
Browse files

[+] Add first analysis

parent 0a0920be
No related branches found
No related tags found
1 merge request!2Machine learning implementation
This diff is collapsed.
198,331,450,543,ceder
475,567,563,661,interdiction
297,185,352,367,fvert
276,609,303,690,fvert
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Entrainement d'un modèle avec la méthode des SVM"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Chargement des données d'entrainement"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import cv2\n",
"import numpy as np\n",
"import random"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"\"\"\" \n",
"We will create a dict with all the usefull datas of the training dataset\n",
"datas = {\n",
" \"XXXX\" (name of the file) : {\n",
" \"img\" : ndarray of the image,\n",
" \"labels\" (data of the labels): {\n",
" \"X\" index of the label (0,1,...,n) : {\n",
" \"name\" : name of the label,\n",
" \"coord\" : coord of the label like xmin, ymin, xmax, ymax,\n",
" \"img\" : crooped img of the label,\n",
" }\n",
" }\n",
" }\n",
"}\n",
"\n",
"\"\"\"\n",
"\n",
"def generate_empty_bbox(image_width, image_height):\n",
" # Thanks to the stats, we know that size of bbox will be (127, 145) -> Average size of labels \n",
" # Génération de coordonnées aléatoires pour le coin supérieur gauche de la boundebox\n",
" x_min = random.randint(0, image_width - 127)\n",
" y_min = random.randint(0, image_height - 145)\n",
" \n",
" # Calcul des coordonnées du coin inférieur droit de la boundebox\n",
" x_max = x_min + 127\n",
" y_max = y_min + 145\n",
" \n",
" return (x_min, y_min, x_max, y_max)\n",
"\n",
"def load_data(image_dir, label_dir):\n",
" datas = {}\n",
"\n",
" for image_file in os.listdir(image_dir):\n",
" # Computing name and files paths\n",
" image_path = image_dir + '/' + image_file\n",
" name = image_file.split('.')[0]\n",
" label_path = label_dir + '/' + name + '.csv'\n",
" \n",
" # Import image as array\n",
" image = cv2.imread(image_path)\n",
"\n",
" # Import labels as array \n",
" with open(label_path, 'r') as file:\n",
" rows = file.readlines()\n",
"\n",
" label_data = {}\n",
" if rows == ['\\n']: # Create a random empty label to balance model\n",
" # Create random coords for empty label\n",
" xmin, ymin, xmax, ymax = generate_empty_bbox(image.shape[1], image.shape[0])\n",
" \n",
" # Get the cropped image (as array) of the label\n",
" cropped_image = image[ymin:ymax, xmin:xmax]\n",
" \n",
" label_data[0] = {\n",
" \"name\":\"empty\",\n",
" \"coord\": (xmin, ymin, xmax, ymax),\n",
" \"img\":cropped_image\n",
" }\n",
" else:\n",
" for i, row in enumerate(rows): # One image can contain several labels\n",
" row = row.strip().split(\",\")\n",
"\n",
" # Compute coords of the label\n",
" xmin, ymin, xmax, ymax = map(int, row[0:4])\n",
"\n",
" # Get the label name\n",
" class_name = row[4]\n",
"\n",
" # Get the cropped image (as array) of the label\n",
" cropped_image = image[ymin:ymax, xmin:xmax]\n",
" \n",
" # Adding to the json\n",
" label_data[i] = {\n",
" \"name\":class_name,\n",
" \"coord\": (xmin, ymin, xmax, ymax),\n",
" \"img\":cropped_image\n",
" }\n",
"\n",
" datas[name] = {\n",
" \"img\" : image,\n",
" \"labels\" : label_data,\n",
" }\n",
" \n",
" return datas"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Creating the dict of the datas \n",
"\n",
"datas = load_data(\"../data/train/images\", \"../data/train/labels\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def extract_features(img):\n",
" # Convertion to gray level\n",
" gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
"\n",
" # Color Hist\n",
" hist_color = cv2.calcHist([img], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])\n",
" hist_color = cv2.normalize(hist_color, hist_color).flatten()\n",
" \n",
" # Gradient Hist\n",
" sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=5)\n",
" sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=5)\n",
" grad_mag = np.sqrt(sobelx**2 + sobely**2)\n",
" hist_gradient = cv2.calcHist([grad_mag.astype(np.uint8)], [0], None, [16], [0, 256])\n",
" hist_gradient = cv2.normalize(hist_gradient, hist_gradient).flatten()\n",
" \n",
" return np.concatenate((hist_color, hist_gradient))\n",
"\n",
"\n",
"# Dict to convert str class name to int\n",
"name_to_int = {\n",
" \"danger\": 0,\n",
" \"interdiction\": 1,\n",
" \"obligation\": 2,\n",
" \"stop\": 3,\n",
" \"ceder\": 4,\n",
" \"frouge\": 5,\n",
" \"forange\": 6,\n",
" \"fvert\": 7,\n",
" \"ff\": 8,\n",
" \"empty\": 9\n",
"}\n",
"\n",
"\n",
"# Creating arrays with all labels datas & classes\n",
"X_train = []\n",
"Y_train = []\n",
"\n",
"for name, data in datas.items():\n",
" for row in data[\"labels\"].values():\n",
" X_train.append(extract_features(row[\"img\"]))\n",
" Y_train.append(name_to_int[row[\"name\"]])\n",
"\n",
"X_train = np.array(X_train)\n",
"Y_train = np.array(Y_train)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 1 0 ... 1 5 7]\n"
]
}
],
"source": [
"from sklearn import svm\n",
"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n",
"\n",
"svm_model = svm.SVC(kernel='linear') # Choix du noyau linéaire\n",
"svm_model.fit(X_train, Y_train)\n",
"\n",
"print(svm_model)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment