使用 OpenCV DNN 模組與 Res10 SSD 進行人臉偵測
OpenCV DNN 模組使用 Res10 SSD 進行人臉偵測教學 使用 OpenCV DNN 模組與 Res10 SSD 進行人臉偵測教學 OpenCV 提供了強大的 DNN 模組,讓我們能輕鬆載入深度學習預訓練模型並進行物件偵測。本教學將示範如何使用 Res10 SSD 模型對圖片中的人臉進行偵測,並詳細解說核心程式碼與座標換算邏輯。 一、教學目標與環境準備 使用 OpenCV DNN 模組載入 Res10 SSD 人臉偵測模型 從圖片中偵測人臉位置 在圖片中繪製偵測到的人臉方框 深入解析模型輸出座標如何轉換成像素座標 需準備: OpenCV(建議版本4.x以上) Res10 SSD 模型權重檔 res10_300x300_ssd_iter_140000_fp16.caffemodel 模型配置檔 deploy.prototxt 測試圖片(如 faces2.jpg ) 二、完整程式碼範例 import cv2 model_path = r"ch03/models/Res10_ssd/res10_300x300_ssd_iter_140000_fp16.caffemodel" config_path = r"ch03/models/Res10_ssd/deploy.prototxt" model = cv2.dnn.readNet(model=model_path, config=config_path, framework='Caffe') image = cv2.imread(r"ch03/images/faces2.jpg") image_height, image_width, _ = image.shape blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177)) model.setInput(blob) detections = model.forward() for face in detections[0][0]: face_confidence = face[2] pr...