{ "cells": [ { "cell_type": "markdown", "id": "f0d53356-45d1-4af1-9350-53075e04bcb8", "metadata": {}, "source": [ "#
طبقه‌بندی با استخراج ویژگی با HOG
" ] }, { "cell_type": "markdown", "id": "7db33fce-992a-483e-b0ea-5971896027b0", "metadata": {}, "source": [ "###
لود کتابخانه‌های مورد نیاز
" ] }, { "cell_type": "code", "execution_count": 1, "id": "49efdb31-5903-4473-b5af-64255c09364c", "metadata": {}, "outputs": [], "source": [ "import cv2\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "3b3240f0-26f8-445f-8254-cda393de9708", "metadata": {}, "source": [ "###
لود مجموعه داده
" ] }, { "cell_type": "code", "execution_count": 2, "id": "26c4c56a-4bbd-4b35-9c9c-3d5a5ddf2a99", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(5000, 20, 20)\n", "(5000,)\n" ] } ], "source": [ "def split2d(img, cell_size, flatten=True):\n", " h, w = img.shape[:2]\n", " sx, sy = cell_size\n", " cells = [np.hsplit(row, w//sx) for row in np.vsplit(img, h//sy)]\n", " cells = np.array(cells)\n", " if flatten:\n", " cells = cells.reshape(-1, sy, sx)\n", " return cells\n", "\n", "def load_digits(file_name):\n", " digits_img = cv2.imread(file_name, 0)\n", " digits = split2d(digits_img, (20, 20))\n", " labels = np.repeat(np.arange(10), len(digits)/10)\n", " return digits, labels\n", "\n", "digits, labels = load_digits('dataset/digits.png')\n", "print(digits.shape)\n", "print(labels.shape)" ] }, { "cell_type": "markdown", "id": "08b8f15b-d3ad-4530-9501-0fff36f27f6d", "metadata": {}, "source": [ "###
بر زدن مجموعه داده (shuffle کردن)
" ] }, { "cell_type": "code", "execution_count": 3, "id": "e9c1196d-3709-4438-858c-ca6eed547949", "metadata": {}, "outputs": [], "source": [ "rand = np.random.RandomState(10)\n", "shuffle = rand.permutation(len(digits))\n", "digits, labels = digits[shuffle], labels[shuffle]" ] }, { "cell_type": "markdown", "id": "c66bd4aa-4a85-49f8-9f38-bd4381c1cae6", "metadata": {}, "source": [ "###
ایجاد شئ HOG برای استخراج ویژگی
" ] }, { "cell_type": "code", "execution_count": 4, "id": "e2ad8150-4ffe-429c-905e-274dd826cf18", "metadata": {}, "outputs": [], "source": [ "winSize = (20,20)\n", "blockSize = (8,8)\n", "blockStride = (4,4)\n", "cellSize = (8,8)\n", "nbins = 9\n", "derivAperture = 1\n", "winSigma = -1.\n", "histogramNormType = 0\n", "L2HysThreshold = 0.2\n", "gammaCorrection = 1\n", "nlevels = 64\n", "signedGradient = True\n", "\n", "hog = cv2.HOGDescriptor(winSize,blockSize,blockStride,cellSize,nbins,derivAperture,winSigma,histogramNormType,L2HysThreshold,gammaCorrection,nlevels, signedGradient)" ] }, { "cell_type": "markdown", "id": "c79a8624-763f-4961-8c4b-095f6fe45d49", "metadata": {}, "source": [ "###
استخراج ویژگی‌های تصویر با HOG
" ] }, { "cell_type": "code", "execution_count": 5, "id": "9d44f59b-89fd-4877-bc4d-b2a2cda5d898", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5000, 144)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hog_descriptors = []\n", "for img in digits:\n", " hog_descriptors.append(hog.compute(img))\n", "hog_descriptors = np.squeeze(hog_descriptors)\n", "hog_descriptors.shape" ] }, { "cell_type": "markdown", "id": "79e77621-8cf6-4312-930f-0fe5e8f1c6c3", "metadata": {}, "source": [ "###
جدا کردن مجموعه داده آموزش و آزمون (train/test)
" ] }, { "cell_type": "code", "execution_count": 6, "id": "9f7c80c6-b248-4178-b9c8-0c43f98cf305", "metadata": {}, "outputs": [], "source": [ "train_n=int(0.9*len(hog_descriptors))\n", "digits_train, digits_test = np.split(digits, [train_n])\n", "hog_descriptors_train, hog_descriptors_test = np.split(hog_descriptors, [train_n])\n", "labels_train, labels_test = np.split(labels, [train_n])" ] }, { "cell_type": "markdown", "id": "79028a0c-d824-467a-8a65-11c3dc7163cf", "metadata": {}, "source": [ "###
ایجاد یک طبقه‌بند - در اینجا SVM
" ] }, { "cell_type": "code", "execution_count": 7, "id": "4f954b2a-5461-4d2d-bb5c-86a0391593bc", "metadata": {}, "outputs": [], "source": [ "C=12.5\n", "gamma=0.50625\n", "model = cv2.ml.SVM_create()\n", "model.setGamma(gamma)\n", "model.setC(C)\n", "model.setKernel(cv2.ml.SVM_RBF)\n", "model.setType(cv2.ml.SVM_C_SVC)" ] }, { "cell_type": "markdown", "id": "fa8fdd23-e768-4358-bf1b-f0cd1e72f123", "metadata": {}, "source": [ "###
آموزش طبقه‌بند
" ] }, { "cell_type": "code", "execution_count": 8, "id": "302c219b-969e-4361-8247-24b837265dc3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.train(hog_descriptors_train, cv2.ml.ROW_SAMPLE, labels_train)" ] }, { "cell_type": "markdown", "id": "4196435d-6334-4bbf-aefd-36147c0256e3", "metadata": {}, "source": [ "###
مشاهده‌ی نتیجه
" ] }, { "cell_type": "code", "execution_count": 9, "id": "13391187-83ed-4ffb-82ff-38e08e138fd1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Percentage Accuracy: 98.60 %\n" ] } ], "source": [ "predictions = model.predict(hog_descriptors_test)[1].ravel()\n", "accuracy = (labels_test == predictions).mean()\n", "print('Percentage Accuracy: %.2f %%' % (accuracy*100))" ] }, { "cell_type": "markdown", "id": "f440a4e4-64fd-45f9-a12e-5bbddc5ff032", "metadata": {}, "source": [ "Source and More complete example:\n", "https://github.com/spmallick/learnopencv/blob/master/digits-classification/train_digits.py" ] } ], "metadata": { "kernelspec": { "display_name": "vision-class", "language": "python", "name": "tensorflow_25" }, "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }