计算机视觉 - 目标检测
什么是目标检测?
目标检测是一种计算机视觉技术,用于定位图像或视频中物体的实例。
目标是识别物体的存在,并在其周围绘制边界框以指示其位置。目标检测结合了图像分类和目标定位。
目标检测的重要性
目标检测对于各种现实世界的应用非常重要,例如:
- 自动驾驶汽车:检测道路上的行人、车辆和障碍物。
- 监控:监控活动并识别可疑物体。
- 医疗保健:检测医学图像中的异常情况。
- 机器人技术:使机器人能够与其环境中的物体交互。
目标检测技术
目标检测有多种技术,它们是:
- 传统方法
- 基于机器学习的方法
- 基于深度学习的方法
传统方法
传统的目标检测方法依赖于图像处理技术和定制的特征。这些方法的准确性通常不如现代基于机器学习的方法,但更简单、更快。
常用的传统方法是Haar级联。它使用级联分类器,这些分类器通过正负图像训练来检测物体。
import cv2
# Load the pre-trained Haar Cascade classifier for face detection
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Read the input image
image = cv2.imread('image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
# Draw bounding boxes around detected faces
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow('Detected Faces', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
基于机器学习的方法
基于机器学习的方法使用从数据中学习以检测物体的算法。这些方法通常涉及对标记数据集进行分类器训练。
最常用的基于机器学习的方法是方向梯度直方图 (HOG) + SVM。这提取HOG特征并使用支持向量机 (SVM) 对物体进行分类。
from skimage.feature import hog
from sklearn.svm import LinearSVC
import joblib
# Load the pre-trained HOG + SVM model
model = joblib.load('hog_svm_model.pkl')
# Extract HOG features from the input image
features, _ = hog(image, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), block_norm='L2-Hys', visualize=True)
# Predict the presence of objects using the trained SVM model
prediction = model.predict([features])
基于深度学习的方法
基于深度学习的方法凭借其高精度和处理复杂图像的能力,彻底改变了目标检测。这些方法使用卷积神经网络 (CNN) 来学习特征并执行检测。
常见的基于深度学习的方法如下所示:
- R-CNN(基于区域的卷积神经网络):提出候选区域并使用CNN对其进行分类。
- YOLO(你只需要看一次):将图像划分为网格,并直接预测每个网格单元的边界框和类别概率。
- SSD(单次多盒检测器):类似于YOLO,但使用不同的架构来实现更快、更准确的检测。
YOLO(你只需要看一次)
YOLO是一个流行且高效的目标检测模型。它将图像划分为网格,并预测每个网格单元的边界框和类别概率。
您可以按照以下步骤使用YOLO:
- 步骤1:加载预训练的YOLO模型。
import cv2
import numpy as np
# Load YOLO
net = cv2.dnn.readNet('yolov3.weights', 'yolov3.cfg')
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
# Load the input image
image = cv2.imread('image.jpg')
height, width, channels = image.shape
# Prepare the image for YOLO
blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
# Run the model outs = net.forward(output_layers)
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
# Object detected
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# Rectangle coordinates
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
# Apply Non-Maximum Suppression
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# Draw bounding boxes
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(class_ids[i])
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(image, label, (x, y + 30), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255, 0), 3)
cv2.imshow('Object Detection', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
广告