目前可依靠模塊化方式實現(xiàn)圖像處理管道,檢測一堆圖像文件中的人臉,并將其與漂亮的結構化JSON摘要文件一起保存在單獨的文件夾中。
讓我們對視頻流也可以進行同樣的操作。
首先,我們需要捕獲視頻流。該管線任務將從視頻文件或網絡攝像頭(逐幀)生成一系列圖像。接下來,我們將檢測每個幀上的臉部并將其保存。接下來的三個塊是可選的,它們的目標是創(chuàng)建帶有注釋的輸出視頻,例如在檢測到的人臉周圍的框。我們可以顯示帶注釋的視頻并將其保存。最后一個任務將收集有關檢測到的面部的信息,并保存帶有面部的框坐標和置信度的JSON摘要文件。
如果尚未設置jagin / image-processing-pipeline存儲庫以查看源代碼并運行一些示例,則可以立即執(zhí)行以下操作:
$ git clone git://github.com/jagin/image-processing-pipeline.git
$ cd image-processing-pipeline
$ git checkout 7df1963247caa01b503980fe152138b88df6c526
$ conda env create -f environment.yml
$ conda activate pipeline
如果已經克隆了存儲庫并設置了環(huán)境,請使用以下命令對其進行更新:
$ git pull
$ git checkout 7df1963247caa01b503980fe152138b88df6c526
$ conda env update -f environment.yml
拍攝影片
使用OpenCV捕獲視頻非常簡單。我們需要創(chuàng)建一個VideoCapture對象,其中參數(shù)是設備索引(指定哪個攝像機的數(shù)字)或視頻文件的名稱。然后,我們可以逐幀捕獲視頻流。
我們可以使用以下CaptureVideo擴展類來實現(xiàn)捕獲視頻任務Pipeline:
import cv2
from pipeline.pipeline import Pipeline
class CaptureVideo(Pipeline):
def __init__(self, src=0):
self.cap = cv2.VideoCapture(src)
if not self.cap.isOpened():
raise IOError(f"Cannot open video {src}")
self.fps = int(self.cap.get(cv2.CAP_PROP_FPS))
self.frame_count = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
super(CaptureVideo, self).__init__()
def generator(self):
image_idx = 0
while self.has_next():
ret, image = self.cap.read()
if not ret:
# no frames has been grabbed
break
data = {
"image_id": f"{image_idx:05d}",
"image": image,
}
if self.filter(data):
image_idx += 1
yield self.map(data)
def cleanup(self):
# Closes video file or capturing device
self.cap.release()
使用__init__我們創(chuàng)建VideoCapture對象(第6行)并提取視頻流的屬性,例如每秒幀數(shù)和幀數(shù)。我們將需要它們顯示進度條并正確保存視頻。圖像幀將在具有字典結構的generator函數(shù)(第30行)中產生:
data = {
"image_id": f"{image_idx:05d}",
"image": image,
}
當然,數(shù)據(jù)中也包括圖像的序列號和幀的二進制數(shù)據(jù)。
檢測人臉
我們準備檢測面部。這次,我們將使用OpenCV的深度神經網絡模塊,而不是我在上一個故事中所承諾的Haar級聯(lián)。我們將要使用的模型更加準確,并且還為我們提供了置信度得分。
從版本3.3開始,OpenCV支持許多深度學習框架,例如Caffe,TensorFlow和PyTorch,從而使我們能夠加載模型,預處理輸入圖像并進行推理以獲得輸出分類。
有一位優(yōu)秀的博客文章中阿德里安·羅斯布魯克(Adrian Rosebrock)解釋如何使用OpenCV和深度學習實現(xiàn)人臉檢測。我們將在FaceDetector類中使用部分代碼:
import cv2
import numpy as np
class FaceDetector:
def __init__(self, prototxt, model, confidence=0.5):
self.confidence = confidence
self.net = cv2.dnn.readNetFromCaffe(prototxt, model)
def detect(self, images):
# convert images into blob
blob = self.preprocess(images)
# pass the blob through the network and obtain the detections and predictions
self.net.setInput(blob)
detections = self.net.forward()
# Prepare storage for faces for every image in the batch
faces = dict(zip(range(len(images)), [[] for _ in range(len(images))]))
# loop over the detections
for i in range(0, detections.shape[2]):
# extract the confidence (i.e., probability) associated with the prediction
confidence = detections[0, 0, i, 2]
# filter out weak detections by ensuring the `confidence` is
# greater than the minimum confidence
if confidence < self.confidence:
continue
# grab the image index
image_idx = int(detections[0, 0, i, 0])
# grab the image dimensions
(h, w) = images[image_idx].shape[:2]
# compute the (x, y)-coordinates of the bounding box for the object
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
# Add result
faces[image_idx].append((box, confidence))
return faces
def preprocess(self, images):
return cv2.dnn.blobFromImages(images, 1.0, (300, 300), (104.0, 177.0, 123.0))
-
攝像頭
+關注
關注
60文章
4871瀏覽量
96401 -
OpenCV
+關注
關注
31文章
635瀏覽量
41595 -
JSON
+關注
關注
0文章
119瀏覽量
7021
發(fā)布評論請先 登錄
相關推薦
樹莓派上使用OpenCV和Python實現(xiàn)實時人臉檢測
基于OPENCV的相機捕捉視頻進行人臉檢測--米爾NXP i.MX93開發(fā)板
![基于<b class='flag-5'>OPENCV</b>的相機捕捉<b class='flag-5'>視頻</b>進行<b class='flag-5'>人臉</b><b class='flag-5'>檢測</b>--米爾NXP i.MX93開發(fā)板](https://file.elecfans.com/web2/M00/08/64/pYYBAGDwFEGADIPWAAFlJOlmLxg664.jpg)
如何用OpenCV的相機捕捉視頻進行人臉檢測--基于米爾NXP i.MX93開發(fā)板
基于openCV的人臉檢測系統(tǒng)的設計
【TL6748 DSP申請】基于TMS320C6748 DSP人臉檢測及跟蹤
【NanoPi2申請】基于opencv的人臉識別門禁系統(tǒng)
【AI技能解析】人臉識別是怎么做到的?
【EASY EAI Nano開源套件試用體驗】4AI功能測試之人臉檢測
【飛凌RK3568開發(fā)板試用體驗】使用OpenCV進行人臉識別
【飛凌RK3588開發(fā)板試用】實現(xiàn)人臉檢測
基于openCV的人臉檢測識別系統(tǒng)的設計
![基于<b class='flag-5'>openCV</b>的<b class='flag-5'>人臉</b><b class='flag-5'>檢測</b>識別系統(tǒng)的設計](https://file.elecfans.com/web2/M00/49/5F/pYYBAGKhtEqASYB-AAAM0Mt5Jg4768.jpg)
Android系統(tǒng)下OpenCV的人臉檢測模塊的設計
![Android系統(tǒng)下<b class='flag-5'>OpenCV</b>的<b class='flag-5'>人臉</b><b class='flag-5'>檢測</b>模塊的設計](https://file.elecfans.com/web2/M00/49/6A/pYYBAGKhtE6AI3x9AAAL-oODLh8716.jpg)
openCV人臉檢測系統(tǒng)的設計方案探究
OpenCV的視頻處理之人臉檢測 2
Android系統(tǒng)下OpenCV的人臉檢測模塊的設計
![Android系統(tǒng)下<b class='flag-5'>OpenCV</b>的<b class='flag-5'>人臉</b><b class='flag-5'>檢測</b>模塊的設計](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
評論