0

i have 13 deer images and 5 non-deer images, i want to use HOG + SVM to detect deer in an image, i am trying the same using this code but getting an error i have understood why i get that error, but dont know how to remove it, can someone please help me with the same code or provide me with a different code that does the same thing, here's my code:

from skimage.feature import local_binary_pattern
from skimage.feature import hog
from skimage.io import imread
import joblib
# To read file names
import argparse as ap
import glob
import os
import cv2

pos_im_path= "/content/drive/MyDrive/data.zip (Unzipped Files)/deer-train_cropped/deer"
neg_im_path= "/content/drive/MyDrive/data.zip (Unzipped Files)/deer-train_cropped/non-deer"

pos_feat_ph= "/content/pos_feat_ph"
neg_feat_ph= "/content/neg_feat_ph"

import cv2

print("Calculating the descriptors for the positive samples and saving them")
for im_path in glob.glob(os.path.join(pos_im_path, "*")):
    im = cv2.imread(im_path, 0)
    #if des_type == "HOG":
    fd = hog(im, orientations= 9, pixels_per_cell= [8,8], cells_per_block= [3,3], visualize= True, block_norm='L1')

        #fd = hog(im, orientations, pixels_per_cell, cells_per_block, visualize, normalize)
    fd_name = os.path.split(im_path)[1].split(".")[0] + ".feat"
    fd_path = os.path.join(pos_feat_ph, fd_name)
    joblib.dump(fd, fd_path)
    
print("Positive features saved in {}".format(pos_feat_ph))

print("Calculating the descriptors for the negative samples and saving them")

for im_path in glob.glob(os.path.join(neg_im_path, "*")):
    im = cv2.imread(im_path, 0)
    #if des_type == "HOG":
    fd = hog(im, orientations= 9, pixels_per_cell= [8,8], cells_per_block= [3,3], visualize= True , block_norm='L1')

        #fd = hog(im,  orientations, pixels_per_cell, cells_per_block, visualize, normalize)
    fd_name = os.path.split(im_path)[1].split(".")[0] + ".feat"
    fd_path = os.path.join(neg_feat_ph, fd_name)
    joblib.dump(fd, fd_path)

print("Negative features saved in {}".format(neg_feat_ph))

print("Completed calculating features from training images")
from sklearn.svm import LinearSVC, SVC
from sklearn.neural_network import MLPClassifier

import numpy as np

# Classifiers supported
clf_type = 'LIN_SVM'
pos_feat_path= "/content/pos_feat_ph"
neg_feat_path= "/content/neg_feat_ph"



fds = []
labels = []
# Load the positive features
for feat_path in glob.glob(os.path.join(pos_feat_path,"*.feat")):
    fd = joblib.load(feat_path)
    #fds.append(np.array(fd).flatten().tolist())  # flatten the feature vector and convert to a list


    fds.append(fd)
    labels.append(1)

# Load the negative features
for feat_path in glob.glob(os.path.join(neg_feat_path,"*.feat")):
    fd = joblib.load(feat_path)
    #fds.append(np.array(fd).flatten().tolist())  # flatten the feature vector and convert to a list

    fds.append(fd)
    labels.append(0)




if clf_type is "LIN_SVM":
    #clf = LinearSVC()

    clf = MLPClassifier(hidden_layer_sizes=(100, 50), max_iter=500)

    print("Training a Linear SVM Classifier")
    clf.fit(fds, labels)
    # If feature directories don't exist, create them
    if not os.path.isdir(os.path.split(model_path)[0]):
        os.makedirs(os.path.split(model_path)[0])
    joblib.dump(clf, model_path)
    print("Classifier saved to {}".format(model_path))

i am using the code from this Github repository: (https://github.com/bikz05/object-detector/tree/master/object-detector)

i tried whatever is commented, and also tried padding with zeros, but both didnt work

Zac Anger
  • 6,983
  • 2
  • 15
  • 42

0 Answers0