import argparse
import cv2
construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument(“-i”, “–image”, required=True,
help=“path to input image”)
ap.add_argument(“-p”, “–prototxt”, required=True,
help=“path to Caffe ‘deploy’ prototxt file”)
ap.add_argument(“-m”, “–model”, required=True,
help=“path to Caffe pre-trained model”)
ap.add_argument(“-c”, “–confidence”, type=float, default=0.5,
help=“minimum probability to filter weak detections”)
args = vars(ap.parse_args())
導(dǎo)入所需的包并解析命令行參數(shù)。 我們有三個必需的參數(shù):
–image :輸入圖像的路徑。
–prototxt :Caffe prototxt 文件的路徑。
–model :預(yù)訓(xùn)練 Caffe 模型的路徑。
可選參數(shù) --confidence 可以覆蓋默認閾值 0.5。 從那里讓我們加載我們的模型并從我們的圖像創(chuàng)建一個 blob:
load our serialized model from disk
print(“[INFO] loading model…”)
net = cv2.dnn.readNetFromCaffe(args[“prototxt”], args[“model”])
load the input image and construct an input blob for the image
by resizing to a fixed 300x300 pixels and then normalizing it
image = cv2.imread(args[“image”])
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0,
(300, 300), (104.0, 177.0, 123.0))
首先,我們使用 --prototxt 和 --model 文件路徑加載我們的模型。 我們將模型存儲為 net(第 20 行)。
然后我們加載圖像,提取尺寸,并創(chuàng)建一個 blob。 dnn.blobFromImage 負責(zé)預(yù)處理,包括設(shè)置 blob 尺寸和規(guī)范化。 如果您有興趣了解有關(guān) dnn.blobFromImage 函數(shù)的更多信息,我會在這篇博文中詳細介紹。 接下來,我們將應(yīng)用人臉檢測:
pass the blob through the network and obtain the detections and
predictions
print(“[INFO] computing object detections…”)
net.setInput(blob)
detections = net.forward()
為了檢測人臉,將 blob 通過網(wǎng)絡(luò)。 然后我們將循環(huán)檢測并在檢測到的人臉周圍繪制框:
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 > args[“confidence”]:
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])
(startX, startY, endX, endY) = box.astype(“int”)
draw the bounding box of the face along with the associated
probability
text = “{:.2f}%”.format(confidence * 100)
y = startY - 10 if startY - 10 > 10 else startY + 10
cv2.rectangle(image, (startX, startY), (endX, endY),
(0, 0, 255), 2)
cv2.putText(image, text, (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
show the output image
cv2.imshow(“Output”, image)
cv2.waitKey(0)
遍歷檢測結(jié)果。
然后,我們提取置信度并將其與置信度閾值進行比較。 我們執(zhí)行此檢查以過濾掉弱檢測。 如果置信度滿足最小閾值,我們繼續(xù)繪制一個矩形以及檢測概率。
為此,我們首先計算邊界框的 (x, y) 坐標(biāo)。 然后我們構(gòu)建包含檢測概率的置信文本字符串。 如果我們的文本偏離圖像(例如當(dāng)面部檢測發(fā)生在圖像的最頂部時),我們將其向下移動 10 個像素。 我們的面部矩形和置信文本繪制在圖像上。
然后,我們再次循環(huán)執(zhí)行該過程后的其他檢測。 如果沒有檢測到,我們準備在屏幕上顯示我們的輸出圖像)。
打開一個終端并執(zhí)行以下命令:
python detect_faces.py --image 2.jpg --prototxt deploy.proto.txt --model res10_300x300_ssd_iter_140000_fp16.caffemodel
使用 OpenCV 和深度學(xué)習(xí)在視頻和網(wǎng)絡(luò)攝像頭中進行人臉檢測
==========================================================================================
既然我們已經(jīng)學(xué)習(xí)了如何將 OpenCV 的人臉檢測應(yīng)用于單個圖像,讓我們還將人臉檢測應(yīng)用于視頻、視頻流和網(wǎng)絡(luò)攝像頭。 對我們來說幸運的是,我們在上一節(jié)中使用 OpenCV 在單個圖像中進行人臉檢測的大部分代碼都可以在這里重用!
打開一個新文件,將其命名為 detect_faces_video.py ,并插入以下代碼:
import the necessary packages
from imutils.video import VideoStream
import numpy as np
import argparse
import imutils
import time
import cv2
construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument(“-p”, “–prototxt”, required=True,
help=“path to Caffe ‘deploy’ prototxt file”)
ap.add_argument(“-m”, “–model”, required=True,
help=“path to Caffe pre-trained model”)
ap.add_argument(“-c”, “–confidence”, type=float, default=0.5,
help=“minimum probability to filter weak detections”)
args = vars(ap.parse_args())
與上面相比,我們需要導(dǎo)入三個額外的包: VideoStream 、 imutils 和 time 。 如果您的虛擬環(huán)境中沒有 imutils,您可以通過以下方式安裝它:
pip install imutils
我們的命令行參數(shù)基本相同,只是這次我們沒有 --image 路徑參數(shù)。 我們將改用網(wǎng)絡(luò)攝像頭的視頻源。 從那里我們將加載我們的模型并初始化視頻流:
load our serialized model from disk
print(“[INFO] loading model…”)
net = cv2.dnn.readNetFromCaffe(args[“prototxt”], args[“model”])
initialize the video stream and allow the camera sensor to warm up
print(“[INFO] starting video stream…”)
vs = VideoStream(src=0).start()
time.sleep(2.0)
加載模型與上面相同。 我們初始化一個 VideoStream 對象,指定索引為零的相機作為源(通常這將是您筆記本電腦的內(nèi)置相機或檢測到的臺式機的第一個相機)。 這里有一些快速說明:
如果您希望使用 Raspberry Pi 攝像頭模塊,Raspberry Pi + picamera 用戶可以將其替換為 vs = VideoStream(usePiCamera=True).start()。 如果要解析視頻文件(而不是視頻流),請將 VideoStream 類換成 FileVideoStream。
然后我們讓相機傳感器預(yù)熱 2 秒。 從那里我們循環(huán)幀并使用 OpenCV 計算人臉檢測:
loop over the frames from the video stream
while True:
grab the frame from the threaded video stream and resize it
to have a maximum width of 400 pixels
frame = vs.read()
frame = imutils.resize(frame, width=400)
grab the frame dimensions and convert it to a blob
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0,
(300, 300), (104.0, 177.0, 123.0))
pass the blob through the network and obtain the detections and
predictions
net.setInput(blob)
detections = net.forward()
這個塊應(yīng)該看起來很熟悉上一節(jié)中的靜態(tài)圖像版本。 在這個塊中,我們從視頻流中讀取一個幀,創(chuàng)建一個 blob,并將 blob 傳遞給深度神經(jīng)網(wǎng)絡(luò)以獲得面部檢測。
自我介紹一下,小編13年上海交大畢業(yè),曾經(jīng)在小公司待過,也去過華為、OPPO等大廠,18年進入阿里一直到現(xiàn)在。
深知大多數(shù)Python工程師,想要提升技能,往往是自己摸索成長或者是報班學(xué)習(xí),但對于培訓(xùn)機構(gòu)動則幾千的學(xué)費,著實壓力不小。自己不成體系的自學(xué)效果低效又漫長,而且極易碰到天花板技術(shù)停滯不前!
因此收集整理了一份《2024年P(guān)ython開發(fā)全套學(xué)習(xí)資料》,初衷也很簡單,就是希望能夠幫助到想自學(xué)提升又不知道該從何學(xué)起的朋友,同時減輕大家的負擔(dān)。
既有適合小白學(xué)習(xí)的零基礎(chǔ)資料,也有適合3年以上經(jīng)驗的小伙伴深入學(xué)習(xí)提升的進階課程,基本涵蓋了95%以上Python開發(fā)知識點,真正體系化!
由于文件比較大,這里只是將部分目錄大綱截圖出來,每個節(jié)點里面都包含大廠面經(jīng)、學(xué)習(xí)筆記、源碼講義、實戰(zhàn)項目、講解視頻,并且后續(xù)會持續(xù)更新
如果你覺得這些內(nèi)容對你有幫助,可以添加V獲?。簐ip1024c (備注Python)
(1)Python所有方向的學(xué)習(xí)路線(新版)
這是我花了幾天的時間去把Python所有方向的技術(shù)點做的整理,形成各個領(lǐng)域的知識點匯總,它的用處就在于,你可以按照上面的知識點去找對應(yīng)的學(xué)習(xí)資源,保證自己學(xué)得較為全面。
最近我才對這些路線做了一下新的更新,知識體系更全面了。
(2)Python學(xué)習(xí)視頻
包含了Python入門、爬蟲、數(shù)據(jù)分析和web開發(fā)的學(xué)習(xí)視頻,總共100多個,雖然沒有那么全面,但是對于入門來說是沒問題的,學(xué)完這些之后,你可以按照我上面的學(xué)習(xí)路線去網(wǎng)上找其他的知識資源進行進階。
(3)100多個練手項目
我們在看視頻學(xué)習(xí)的時候,不能光動眼動腦不動手,比較科學(xué)的學(xué)習(xí)方法是在理解之后運用它們,這時候練手項目就很適合了,只是里面的項目比較多,水平也是參差不齊,大家可以挑自己能做的項目去練練。
文章來源:http://www.zghlxwxcb.cn/news/detail-852987.html
一個人可以走的很快,但一群人才能走的更遠。不論你是正從事IT行業(yè)的老鳥或是對IT行業(yè)感興趣的新人,都歡迎掃碼加入我們的的圈子(技術(shù)交流、學(xué)習(xí)資源、職場吐槽、大廠內(nèi)推、面試輔導(dǎo)),讓我們一起學(xué)習(xí)成長!
cb197de0dc0438ec5.png#pic_center)
(3)100多個練手項目
我們在看視頻學(xué)習(xí)的時候,不能光動眼動腦不動手,比較科學(xué)的學(xué)習(xí)方法是在理解之后運用它們,這時候練手項目就很適合了,只是里面的項目比較多,水平也是參差不齊,大家可以挑自己能做的項目去練練。
一個人可以走的很快,但一群人才能走的更遠。不論你是正從事IT行業(yè)的老鳥或是對IT行業(yè)感興趣的新人,都歡迎掃碼加入我們的的圈子(技術(shù)交流、學(xué)習(xí)資源、職場吐槽、大廠內(nèi)推、面試輔導(dǎo)),讓我們一起學(xué)習(xí)成長!
[外鏈圖片轉(zhuǎn)存中…(img-V1v6nRv8-1712936501316)]文章來源地址http://www.zghlxwxcb.cn/news/detail-852987.html
到了這里,關(guān)于人臉檢測實戰(zhàn):使用opencv加載深度學(xué)習(xí)模型實現(xiàn)人臉檢測(1)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!