目前可依靠模塊化方式實現圖像處理管道,檢測一堆圖像文件中的人臉,并將其與漂亮的結構化JSON摘要文件一起保存在單獨的文件夾中。
讓我們對視頻流也可以進行同樣的操作。
首先,我們需要捕獲視頻流。該管線任務將從視頻文件或網絡攝像頭(逐幀)生成一系列圖像。接下來,我們將檢測每個幀上的臉部并將其保存。接下來的三個塊是可選的,它們的目標是創建帶有注釋的輸出視頻,例如在檢測到的人臉周圍的框。我們可以顯示帶注釋的視頻并將其保存。最后一個任務將收集有關檢測到的面部的信息,并保存帶有面部的框坐標和置信度的JSON摘要文件。
如果尚未設置jagin / image-processing-pipeline存儲庫以查看源代碼并運行一些示例,則可以立即執行以下操作:
$ 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
如果已經克隆了存儲庫并設置了環境,請使用以下命令對其進行更新:
$ git pull
$ git checkout 7df1963247caa01b503980fe152138b88df6c526
$ conda env update -f environment.yml
拍攝影片
使用OpenCV捕獲視頻非常簡單。我們需要創建一個VideoCapture對象,其中參數是設備索引(指定哪個攝像機的數字)或視頻文件的名稱。然后,我們可以逐幀捕獲視頻流。
我們可以使用以下CaptureVideo擴展類來實現捕獲視頻任務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__我們創建VideoCapture對象(第6行)并提取視頻流的屬性,例如每秒幀數和幀數。我們將需要它們顯示進度條并正確保存視頻。圖像幀將在具有字典結構的generator函數(第30行)中產生:
data = {
"image_id": f"{image_idx:05d}",
"image": image,
}
當然,數據中也包括圖像的序列號和幀的二進制數據。
檢測人臉
我們準備檢測面部。這次,我們將使用OpenCV的深度神經網絡模塊,而不是我在上一個故事中所承諾的Haar級聯。我們將要使用的模型更加準確,并且還為我們提供了置信度得分。
從版本3.3開始,OpenCV支持許多深度學習框架,例如Caffe,TensorFlow和PyTorch,從而使我們能夠加載模型,預處理輸入圖像并進行推理以獲得輸出分類。
有一位優秀的博客文章中阿德里安·羅斯布魯克(Adrian Rosebrock)解釋如何使用OpenCV和深度學習實現人臉檢測。我們將在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文章
4862瀏覽量
96311 -
OpenCV
+關注
關注
31文章
635瀏覽量
41556 -
JSON
+關注
關注
0文章
119瀏覽量
7011
發布評論請先 登錄
相關推薦
評論