davisking / dlib

A toolkit for making real world machine learning and data analysis applications in C++
http://dlib.net
Boost Software License 1.0
13.55k stars 3.38k forks source link

need HOG + PCA feature #2550

Closed fatalfeel closed 2 years ago

fatalfeel commented 2 years ago

import glob import numpy as np import time import random import PIL import cv2 from skimage.feature import hog from sklearn import svm from sklearn.decomposition import PCA from sklearn.model_selection import train_test_split

train pos use 96X160H96

if name == 'main': car_paths = glob.glob("./dataset/car_images" + "/*")[:5000] neg_paths = [] pos_images = [] neg_images = []

for class_path in glob.glob("./dataset/natural_images" + "/*"):
    # if class_path != "./dataset/natural_images/car":
    paths = random.choices(glob.glob(class_path + "/*"), k=700)
    neg_paths = paths + neg_paths

for car_path in car_paths:
    img = np.asarray(PIL.Image.open(car_path))
    # We don't have to use RGB channels to extract features, Grayscale is enough.
    img = cv2.cvtColor(cv2.resize(img, (96, 64)), cv2.COLOR_RGB2GRAY)
    img = hog(img,
              orientations=9,
              pixels_per_cell=(16, 16),
              cells_per_block=(2, 2))

    pos_images.append(img)

for neg_path in neg_paths:
    img = np.asarray(PIL.Image.open(neg_path))
    img = cv2.cvtColor(cv2.resize(img, (96, 64)), cv2.COLOR_RGB2GRAY)
    img = hog(img,
              orientations=9,
              pixels_per_cell=(16, 16),
              cells_per_block=(2, 2))

    neg_images.append(img)

pos_labels  = np.ones(len(car_paths))
neg_labels  = np.zeros(len(neg_paths))
x           = np.asarray(pos_images + neg_images)
y           = np.asarray(list(pos_labels) + list(neg_labels))

#########pca enable or disable this section######## pca = PCA(n_components=40) pca.fit(x) x = np.array(pca.fit_transform(x)) #########pca end##########

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=0)

svc     = svm.SVC()
start   = time.time()
clf     = svc.fit(x_train, y_train)
print(clf.score(x_test, y_test))

processTime = round(time.time() - start, 2)
print("svm has taken {} seconds".format(processTime))

########################test one picture##################### test_list = [] example_image = np.asarray(PIL.Image.open(car_paths[0])) example_image = cv2.cvtColor(cv2.resize(example_image, (96, 64)), cv2.COLOR_RGB2GRAY) example_image = hog(example_image, orientations=9, pixels_per_cell=(16, 16), cells_per_block=(2, 2)) test_list.append(example_image) x = np.asarray(test_list) x = np.array(pca.transform(x)) decision = svc.predict(x) print(decision)

########################refer to py source and do dlib in c++#################### i found an example here https://books.google.com.tw/books?id=ozDnDwAAQBAJ&pg=PA187&lpg=PA187&dq=dlib::vector_normalizer_pca+train&source=bl&ots=4jnTiSl1Mj&sig=ACfU3U23Tm922fkpJNu49oBpjzgZvr6R7w&hl=zh-TW&sa=X&ved=2ahUKEwjUy7G9pd72AhWgsFYBHeNCAMIQ6AF6BAgSEAM#v=onepage&q=dlib%3A%3Avector_normalizer_pca%20train&f=false

fhog_ex.cpp extract_fhog_features(img, hog, sbin);

svm_c_ex.cpp svm_c_trainer trainer;

pca example: https://github.com/PacktPublishing/Hands-On-Machine-Learning-with-CPP/blob/master/Chapter06/dlib/dlib-dr.cc

how to combine hog + pca + svm to detector in dlib c++?

fatalfeel commented 2 years ago

here i make some test for pca~~~ when add pca speed up 2.5~3 times https://github.com/fatalfeel/hog_pca_svm_detection

fatalfeel commented 2 years ago

I do this version for cpp detect 21 fps in old cpu 2012 i7-3770k https://github.com/fatalfeel/hog_pca_svm_slider_nms_cpp

dlib-issue-bot commented 2 years ago

Warning: this issue has been inactive for 35 days and will be automatically closed on 2022-05-17 if there is no further activity.

If you are waiting for a response but haven't received one it's possible your question is somehow inappropriate. E.g. it is off topic, you didn't follow the issue submission instructions, or your question is easily answerable by reading the FAQ, dlib's official compilation instructions, dlib's API documentation, or a Google search.

dlib-issue-bot commented 2 years ago

Warning: this issue has been inactive for 42 days and will be automatically closed on 2022-05-17 if there is no further activity.

If you are waiting for a response but haven't received one it's possible your question is somehow inappropriate. E.g. it is off topic, you didn't follow the issue submission instructions, or your question is easily answerable by reading the FAQ, dlib's official compilation instructions, dlib's API documentation, or a Google search.

dlib-issue-bot commented 2 years ago

Notice: this issue has been closed because it has been inactive for 45 days. You may reopen this issue if it has been closed in error.