Like Share Discussion Bookmark Smile

J.J. Huang   2026-03-01   Python OpenCV 07.物件偵測與辨識篇   瀏覽次數:次   DMCA.com Protection Status

Python | OpenCV 多物件偵測與追蹤

📚 前言

在前一篇我們學會了 物件分類與定位
這一篇要進一步介紹 多物件偵測與追蹤 (Multi-Object Detection & Tracking)
偵測是同時找到多個物件的位置,追蹤則是持續更新它們的移動並維持 ID。這是進入智慧監控與交通分析的重要技術。

🎨 範例圖片/影片

這裡我們使用 Pexels 提供的免費圖片素材:Street Image
下載後將檔名改為 street.jpg,在程式碼範例中使用。這張圖片中有車輛與行人,非常適合用來測試物件分類與定位。

這裡我們使用 Pexels 提供的免費影片素材:Street Video
下載後將檔名改為 street.mp4,在程式碼範例中使用。影片中有車輛與行人,非常適合用來測試物件分類與定位。

🔎 原理說明

  • 多物件偵測:同時輸出多個邊界框與類別。
  • 追蹤 (Tracking):持續更新物件位置,並維持唯一 ID。
  • 常見方法
    • YOLO (You Only Look Once):即時多物件偵測。
    • SSD (Single Shot Detector):輕量化偵測模型。
    • DeepSORT:結合偵測結果,維持物件 ID。

📂 模型下載與使用說明

YOLOv3-tiny (Darknet)

💡 YOLOv3-tiny 是輕量化版本,適合即時應用。

🧠 函式與參數說明

📌 cv2.dnn.readNetFromDarknet()

載入 YOLO 模型

1
net = cv2.dnn.readNetFromDarknet(cfg, weights)
  • cfg:模型結構檔案。
  • weights:訓練好的權重檔案。

📌 cv2.dnn.blobFromImage()

將圖片轉換成 DNN 輸入格式

1
blob = cv2.dnn.blobFromImage(image, scalefactor, size, mean, swapRB, crop)
  • image:輸入圖片。
  • scalefactor:縮放比例,常用 1/255.0
  • size:輸入大小 (width, height),例如 (416, 416)
  • mean:減去的平均值,常用 (0, 0, 0)
  • swapRB:是否交換 R 與 B 通道,常用 True
  • crop:是否裁切圖片,常用 False

💻 範例程式 — YOLO 多物件偵測 (圖片)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import cv2

net = cv2.dnn.readNetFromDarknet("models/yolov3-tiny.cfg", "models/yolov3-tiny.weights")
layer_names = net.getLayerNames()
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]

img = cv2.imread("street.jpg")
(h, w) = img.shape[:2]
blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416), swapRB=True, crop=False)

net.setInput(blob)
outputs = net.forward(output_layers)

for output in outputs:
for detection in output:
scores = detection[5:]
class_id = int(scores.argmax())
confidence = scores[class_id]
if confidence > 0.5:
box = detection[0:4] * [w, h, w, h]
(centerX, centerY, width, height) = box.astype("int")
x = int(centerX - width / 2)
y = int(centerY - height / 2)
cv2.rectangle(img, (x, y), (x + int(width), y + int(height)), (0, 255, 0), 2)

cv2.imshow("YOLO Object Detection", img)
cv2.waitKey(0)
cv2.destroyAllWindows()


圖:圖片多物件偵測,框選多個物件

💻 範例程式 — YOLO 多物件偵測 (影片)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import cv2

net = cv2.dnn.readNetFromDarknet("models/yolov3-tiny.cfg", "models/yolov3-tiny.weights")
layer_names = net.getLayerNames()
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]

cap = cv2.VideoCapture("street.mp4")

while True:
ret, frame = cap.read()
if not ret:
break

(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 1/255.0, (416, 416), swapRB=True, crop=False)

net.setInput(blob)
outputs = net.forward(output_layers)

for output in outputs:
for detection in output:
scores = detection[5:]
class_id = int(scores.argmax())
confidence = scores[class_id]
if confidence > 0.5:
box = detection[0:4] * [w, h, w, h]
(centerX, centerY, width, height) = box.astype("int")
x = int(centerX - width / 2)
y = int(centerY - height / 2)
cv2.rectangle(frame, (x, y), (x + int(width), y + int(height)), (0, 255, 0), 2)

cv2.imshow("YOLO Video Detection", frame)

if cv2.waitKey(30) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()


圖:影片多物件偵測,逐幀框選多個物件

💻 範例程式 — YOLO + DeepSORT 多物件追蹤

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 範例僅示意,需安裝 deep_sort 套件
# pip install deep-sort-realtime

from deep_sort_realtime.deepsort_tracker import DeepSort
import cv2

net = cv2.dnn.readNetFromDarknet("models/yolov3-tiny.cfg", "models/yolov3-tiny.weights")
layer_names = net.getLayerNames()
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]

tracker = DeepSort(max_age=30)

cap = cv2.VideoCapture("street.mp4")

while True:
ret, frame = cap.read()
if not ret:
break

(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 1/255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
outputs = net.forward(output_layers)

detections = []
for output in outputs:
for detection in output:
scores = detection[5:]
class_id = int(scores.argmax())
confidence = scores[class_id]
if confidence > 0.5:
box = detection[0:4] * [w, h, w, h]
(centerX, centerY, width, height) = box.astype("int")
x = int(centerX - width / 2)
y = int(centerY - height / 2)
detections.append(([x, y, int(width), int(height)], confidence, class_id))

tracks = tracker.update_tracks(detections, frame=frame)
for track in tracks:
if not track.is_confirmed():
continue
x1, y1, x2, y2 = track.to_ltrb()
track_id = track.track_id
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 255), 2)
cv2.putText(frame, f"ID {track_id}", (int(x1), int(y1)-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,255), 2)

cv2.imshow("YOLO + DeepSORT Tracking", frame)

if cv2.waitKey(30) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()


圖:影片多物件偵測與追蹤,持續追蹤多個物件並維持唯一 ID

🛠️ 使用 DeepSORT 常見問題與解決辦法

在整合 YOLO 與 DeepSORT 的過程中,常會遇到一些環境或套件相容性的問題:

  • ModuleNotFoundError: No module named 'pkg_resources'

    • 原因:Python 3.12+ 移除了 pkg_resources,導致 DeepSORT 初始化失敗。
    • 解決:改用 Python 3.10 或 3.11 建立虛擬環境,並安裝 setuptools
  • ModuleNotFoundError: No module named 'torch'

    • 原因:PyTorch 尚未安裝。
    • 解決:在虛擬環境中安裝 PyTorch 與 TorchVision:
      1
      pip install torch torchvision
  • ModuleNotFoundError: No module named 'torchvision'

    • 原因:TorchVision 尚未安裝。
    • 解決:補安裝 TorchVision:
      1
      pip install torchvision
  • 環境衝突或安裝不完整

    • 症狀:明明安裝了套件,仍然報錯。
    • 解決:確認 Python 解譯器路徑正確,必要時刪掉 .venv,重新建立乾淨的虛擬環境,並一次安裝所有依賴:
      1
      2
      3
      4
      python3.10 -m venv .venv310
      .\.venv310\Scripts\activate
      pip install --upgrade pip wheel setuptools
      pip install numpy opencv-python torch torchvision deep-sort-realtime

💡 建議建立 requirements.txt,一次安裝所有依賴,避免逐一補套件。

⚠️ 注意事項

  • YOLO 模型檔案需先下載並放在專案目錄。
  • YOLOv3-tiny 適合即時應用,但準確度有限。
  • DeepSORT 需要額外的 ReID 模型,才能維持物件 ID。
  • 偵測速度與準確度會因模型不同而有差異。

📊 應用場景

  • 交通監控:同時追蹤多輛車輛。
  • 群眾分析:統計人流數量與移動路徑。
  • 智慧零售:追蹤顧客行為。
  • 安全監控:辨識並追蹤可疑人物。

🎯 結語

本篇我們學會了如何使用 OpenCV 與 YOLO 模型進行 多物件偵測與追蹤,並結合 DeepSORT 維持物件 ID。
這是從「物件分類與定位」延伸而來的重要技術,能應用在交通監控、群眾分析與智慧零售等場景。

📖 如在學習過程中遇到疑問,或是想了解更多相關主題,建議回顧一下 Python | OpenCV 系列導讀,掌握完整的章節目錄,方便快速找到你需要的內容。

註:以上參考了
OpenCV 官方文件 — Tutorials
OpenCV 官方文件 — Python Tutorials
YOLO 官方網站 — pjreddie.com/darknet/yolo
deep-sort-realtime GitHub
Pexels — 免費圖片與影片素材