假设有这样一个场景,我们想统计某个时间段内经过某个位置的车辆数。一个最简单的做法就是站在那里一个一个的数,就像在梦里数羊一样。哈哈,这里开个玩笑。
基于现在计算机技术的发展,准确来讲是计算机视觉领域的发展,写一个自动计数的程序并不是什么难事,它可以替代人来计数,可谓是绝对的好助手。所以,今天,我向大家介绍一个可以用来计算车辆数的程序,amazing!
废话不多数,直接亮代码:
import numpy as np
from ultralytics import YOLO
import cv2
import cvzone
import math
from sort import *
cap = cv2.VideoCapture("cars.mp4") # For Video
model = YOLO("yolov8n.pt")
classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
"traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
"dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
"handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",
"baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
"fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
"carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed",
"diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone",
"microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
"teddy bear", "hair drier", "toothbrush"
]
mask = cv2.imread("mask.png")
# Tracking
tracker = Sort(max_age=20, min_hits=3, iou_threshold=0.3)
limits = [400, 297, 673, 297]
totalCount = []
while True:
success, img = cap.read()
imgRegion = cv2.bitwise_and(img, mask)
imgGraphics = cv2.imread("graphics.png", cv2.IMREAD_UNCHANGED)
img = cvzone.overlayPNG(img, imgGraphics, (0, 0))
results = model(imgRegion, stream=True)
detections = np.empty((0, 5))
for r in results:
boxes = r.boxes
for box in boxes:
# Bounding Box
x1, y1, x2, y2 = box.xyxy[0]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
# cv2.rectangle(img,(x1,y1),(x2,y2),(255,0,255),3)
w, h = x2 - x1, y2 - y1
# Confidence
conf = math.ceil((box.conf[0] * 100)) / 100
# Class Name
cls = int(box.cls[0])
currentClass = classNames[cls]
if currentClass == "car" or currentClass == "truck" or currentClass == "bus" \
or currentClass == "motorbike" and conf > 0.3:
# cvzone.putTextRect(img, f'{currentClass} {conf}', (max(0, x1), max(35, y1)),
# scale=0.6, thickness=1, offset=3)
# cvzone.cornerRect(img, (x1, y1, w, h), l=9, rt=5)
currentArray = np.array([x1, y1, x2, y2, conf])
detections = np.vstack((detections, currentArray))
resultsTracker = tracker.update(detections)
cv2.line(img, (limits[0], limits[1]), (limits[2], limits[3]), (0, 0, 255), 5)
for result in resultsTracker:
x1, y1, x2, y2, id = result
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
print(result)
w, h = x2 - x1, y2 - y1
cvzone.cornerRect(img, (x1, y1, w, h), l=9, rt=2, colorR=(255, 0, 255))
#cvzone.putTextRect(img, f' {int(id)}', (max(0, x1), max(35, y1)),scale=2, thickness=3, offset=10)
cx, cy = x1 + w // 2, y1 + h // 2
cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED)
if limits[0] < cx < limits[2] and limits[1] - 15 < cy < limits[1] + 15:
if totalCount.count(id) == 0:
totalCount.append(id)
cv2.line(img, (limits[0], limits[1]), (limits[2], limits[3]), (0, 255, 0), 5)
# cvzone.putTextRect(img, f' Count: {len(totalCount)}', (50, 50))
cv2.putText(img, str(len(totalCount)), (255, 100), cv2.FONT_HERSHEY_PLAIN, 5, (50, 50, 255), 8)
cv2.imshow("Image", img)
# cv2.imshow("ImageRegion", imgRegion)
cv2.waitKey(1)
看看效果吧:
想明白具体细节,欢迎私信交流哦,一起学习进步!