# 假设我们有多个图像的HOG特征和标签 features = np.array([hog.compute(cv2.imread(img)) for img in image_paths]) labels = np.array([1if'person'in img else0for img in image_paths])
# 加载 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()]
# 加载类名 withopen("coco.names", "r") as f: classes = [line.strip() for line in f.readlines()]
for cnt in contours: # 计算轮廓的面积,并过滤掉小轮廓 if cv2.contourArea(cnt) > 500: x, y, w, h = cv2.boundingRect(cnt) aspect_ratio = float(w) / h # 筛选出长方形区域 if2 < aspect_ratio < 5: plate_contours.append((x, y, w, h))
# 在原图上绘制检测到的车牌区域 for (x, y, w, h) in plate_contours: cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
for cnt in contours: if cv2.contourArea(cnt) > 500: x, y, w, h = cv2.boundingRect(cnt) aspect_ratio = float(w) / h if2 < aspect_ratio < 5: plate_contours.append((x, y, w, h))
for (x, y, w, h) in plate_contours: cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
if plate_contours: x, y, w, h = plate_contours[0] plate_image = gray[y:y + h, x:x + w] text = pytesseract.image_to_string(plate_image, config='--psm 8') print(f"识别到的车牌号码: {text.strip()}")
# 绘制边界框 for i inrange(len(boxes)): if i in indexes: x, y, w, h = boxes[i] label = str(classes[class_ids[i]]) color = (0, 255, 0) cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2) cv2.putText(frame, label, (x, y + 30), cv2.FONT_HERSHEY_PLAIN, 3, color, 3)