{"id":15057241,"url":"https://github.com/hayatiyrtgl/julia-image-classification-svm","last_synced_at":"2026-03-17T18:08:20.988Z","repository":{"id":241558908,"uuid":"807038526","full_name":"HayatiYrtgl/julia-image-classification-svm","owner":"HayatiYrtgl","description":"The code loads dog and cat images, extracts HOG descriptors, labels them, splits the data into training and test sets, trains an SVM model, and predicts a test image.","archived":false,"fork":false,"pushed_at":"2024-05-28T11:19:32.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-14T10:14:29.327Z","etag":null,"topics":["binary-classification","image-processing","juli","julia-language"],"latest_commit_sha":null,"homepage":"","language":"Julia","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HayatiYrtgl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-28T11:18:38.000Z","updated_at":"2024-05-28T11:39:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"e04fea26-3653-4bde-a4de-ccee6d72dc8a","html_url":"https://github.com/HayatiYrtgl/julia-image-classification-svm","commit_stats":null,"previous_names":["hayatiyrtgl/julia-image-classification-svm"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HayatiYrtgl%2Fjulia-image-classification-svm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HayatiYrtgl%2Fjulia-image-classification-svm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HayatiYrtgl%2Fjulia-image-classification-svm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HayatiYrtgl%2Fjulia-image-classification-svm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HayatiYrtgl","download_url":"https://codeload.github.com/HayatiYrtgl/julia-image-classification-svm/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243558478,"owners_count":20310574,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binary-classification","image-processing","juli","julia-language"],"created_at":"2024-09-24T22:04:18.777Z","updated_at":"2025-12-28T18:21:51.017Z","avatar_url":"https://github.com/HayatiYrtgl.png","language":"Julia","funding_links":[],"categories":[],"sub_categories":[],"readme":"# julia-image-classification-svm\nThe code loads dog and cat images, extracts HOG descriptors, labels them, splits the data into training and test sets, trains an SVM model, and predicts a test image.\n\nLet's analyze the code block step by step:\n\n### Code Analysis\n\n#### 1. Importing Necessary Packages\n```julia\nusing ImageFeatures, Images\nusing Random\nusing ImageDraw, ImageView\nusing LIBSVM\n```\n- **ImageFeatures**: For creating image descriptors.\n- **Images**: For image loading and processing.\n- **Random**: For random operations, like shuffling.\n- **ImageDraw** and **ImageView**: Likely used for drawing and viewing images.\n- **LIBSVM**: For training and using an SVM model.\n\n#### 2. Defining Paths for Dog and Cat Image Directories\n```julia\ndog = \"DATASETS/archive/PetImages/Dog\"\ncat = \"DATASETS/archive/PetImages/Cat\"\n```\n\n#### 3. Counting the Number of Images\n```julia\nn_dogs = length(readdir(dog)[1:8000])\nn_cats = length(readdir(cat)[1:8000])\n```\n- **readdir(dog)[1:8000]**: Reads up to 8000 filenames from the dog directory.\n- **length**: Counts the number of files read.\n\n#### 4. Defining the Dataset Size and Initializing Arrays\n```julia\ntotal = n_cats + n_dogs\ndata = Array{Float64}(undef, 1764, total)\nlabels = Array{Int}(undef, total)\n```\n- **total**: Total number of images.\n- **data**: A 2D array to store the HOG descriptors (1764 features per image).\n- **labels**: An array to store labels (1 for dog, 0 for cat).\n\n#### 5. Creating the Dataset\n```julia\nindex_num = 1\nfor i in readdir(dog)[1:8000]\n    img = load(\"DATASETS/archive/PetImages/Dog/$i\")\n    data[:, index_num] = create_descriptor(img, HOG())\n    labels[index_num] = 1\n    global index_num += 1\nend\n\nfor i in readdir(cat)[1:8000]\n    img = load(\"DATASETS/archive/PetImages/Cat/$i\")\n    data[:, index_num] = create_descriptor(img, HOG())\n    labels[index_num] = 0\n    global index_num += 1\nend\n```\n- **index_num**: Tracks the current index in the dataset.\n- **readdir**: Reads filenames from the dog and cat directories.\n- **load**: Loads an image.\n- **create_descriptor(img, HOG())**: Creates a HOG (Histogram of Oriented Gradients) descriptor for the image.\n- **data[:, index_num] = create_descriptor(img, HOG())**: Stores the descriptor in the data array.\n- **labels[index_num] = 1**: Sets the label to 1 for dogs.\n- **labels[index_num] = 0**: Sets the label to 0 for cats.\n\n#### 6. Splitting the Dataset into Training and Test Sets\n```julia\nrandom_perm = randperm(total)\ntrain_id = random_perm[1:15000]\ntest_id = random_perm[15001:end]\n```\n- **randperm(total)**: Generates a random permutation of indices from 1 to `total`.\n- **train_id**: First 15000 indices for training.\n- **test_id**: Remaining indices for testing.\n\n#### 7. Training the SVM Model\n```julia\nmodel = svmtrain(data[:, train_id], labels[train_id])\n```\n- **svmtrain**: Trains the SVM model using the training data and labels.\n\n#### 8. Loading and Preprocessing a Test Image\n```julia\nmy_test_img = \"DATASETS/archive/PetImages/test/391.jpg\"\nmy_test_img = load(my_test_img)\ndescriptor = Array{Float64}(undef, 1764, 1)\ndescriptor[:, 1] = create_descriptor(my_test_img, HOG())\n```\n- **load**: Loads the test image.\n- **descriptor**: Array to store the descriptor for the test image.\n- **create_descriptor(my_test_img, HOG())**: Creates a HOG descriptor for the test image.\n\n#### 9. Predicting the Label of the Test Image\n```julia\npredicted_label, _ = svmpredict(model, descriptor)\nprint(\"This Photo belongs to the class: $(predicted_label[1])\")\n```\n- **svmpredict**: Predicts the label of the test image using the trained SVM model.\n- **predicted_label**: The predicted label for the test image.\n- **print**: Prints the predicted class.\n\n### Summary\nThis code trains an SVM classifier to differentiate between images of dogs and cats using HOG descriptors. It then predicts the class of a new test image. Here are the key steps:\n1. Load and preprocess the image data.\n2. Create HOG descriptors for each image.\n3. Label the data (1 for dogs, 0 for cats).\n4. Split the dataset into training and test sets.\n5. Train an SVM model on the training data.\n6. Predict the label of a new image using the trained model.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhayatiyrtgl%2Fjulia-image-classification-svm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhayatiyrtgl%2Fjulia-image-classification-svm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhayatiyrtgl%2Fjulia-image-classification-svm/lists"}