Python实现基于HOG特征和SVM的行人检测
以下是用Python实现基于HOG特征和SVM方法的行人检测的示例代码。代码中使用了OpenCV和scikit-learn库。
首先,需要安装必要的库:
pip install opencv-python
pip install scikit-learn
然后,读取训练集和测试集的图像数据:
import cv2
import numpy as np
from sklearn.svm import LinearSVC
from sklearn.metrics import accuracy_score
# 读取训练集
train_imgs = []
train_labels = []
for i in range(1, 501):
img = cv2.imread(f'train/{i:03}.png', cv2.IMREAD_GRAYSCALE)
train_imgs.append(img)
train_labels.append(1)
for i in range(1, 501):
img = cv2.imread(f'neg/{i:03}.png', cv2.IMREAD_GRAYSCALE)
train_imgs.append(img)
train_labels.append(0)
# 读取测试集
test_imgs = []
test_labels = []
for i in range(1, 101):
img = cv2.imread(f'test/{i:03}.png', cv2.IMREAD_GRAYSCALE)
test_imgs.append(img)
test_labels.append(1)
for i in range(1, 101):
img = cv2.imread(f'neg_test/{i:03}.png', cv2.IMREAD_GRAYSCALE)
test_imgs.append(img)
test_labels.append(0)
# 转换为numpy数组
train_imgs = np.array(train_imgs)
train_labels = np.array(train_labels)
test_imgs = np.array(test_imgs)
test_labels = np.array(test_labels)
接下来,提取HOG特征:
# 提取HOG特征
winSize = (64, 128)
blockSize = (16, 16)
blockStride = (8, 8)
cellSize = (8, 8)
nbins = 9
hog = cv2.HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins)
train_hog = []
for img in train_imgs:
train_hog.append(hog.compute(img))
train_hog = np.array(train_hog).squeeze()
test_hog = []
for img in test_imgs:
test_hog.append(hog.compute(img))
test_hog = np.array(test_hog).squeeze()
最后,使用LinearSVC训练和测试SVM分类器:
# 训练SVM分类器
clf = LinearSVC()
clf.fit(train_hog, train_labels)
# 在测试集上进行预测
y_pred = clf.predict(test_hog)
accuracy = accuracy_score(test_labels, y_pred)
print(f'Accuracy: {accuracy}')
完整代码如下:
import cv2
import numpy as np
from sklearn.svm import LinearSVC
from sklearn.metrics import accuracy_score
# 读取训练集
train_imgs = []
train_labels = []
for i in range(1, 501):
img = cv2.imread(f'train/{i:03}.png', cv2.IMREAD_GRAYSCALE)
train_imgs.append(img)
train_labels.append(1)
for i in range(1, 501):
img = cv2.imread(f'neg/{i:03}.png', cv2.IMREAD_GRAYSCALE)
train_imgs.append(img)
train_labels.append(0)
# 读取测试集
test_imgs = []
test_labels = []
for i in range(1, 101):
img = cv2.imread(f'test/{i:03}.png', cv2.IMREAD_GRAYSCALE)
test_imgs.append(img)
test_labels.append(1)
for i in range(1, 101):
img = cv2.imread(f'neg_test/{i:03}.png', cv2.IMREAD_GRAYSCALE)
test_imgs.append(img)
test_labels.append(0)
# 转换为numpy数组
train_imgs = np.array(train_imgs)
train_labels = np.array(train_labels)
test_imgs = np.array(test_imgs)
test_labels = np.array(test_labels)
# 提取HOG特征
winSize = (64, 128)
blockSize = (16, 16)
blockStride = (8, 8)
cellSize = (8, 8)
nbins = 9
hog = cv2.HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins)
train_hog = []
for img in train_imgs:
train_hog.append(hog.compute(img))
train_hog = np.array(train_hog).squeeze()
test_hog = []
for img in test_imgs:
test_hog.append(hog.compute(img))
test_hog = np.array(test_hog).squeeze()
# 训练SVM分类器
clf = LinearSVC()
clf.fit(train_hog, train_labels)
# 在测试集上进行预测
y_pred = clf.predict(test_hog)
accuracy = accuracy_score(test_labels, y_pred)
print(f'Accuracy: {accuracy}')
原文地址: https://www.cveoy.top/t/topic/ntsT 著作权归作者所有。请勿转载和采集!