使用 OpenCV 進(jìn)行人臉檢測(cè)
使用 OpenCV 和 Python 檢測(cè)人臉的一種非常流行且簡(jiǎn)單的方法
步驟 01
我為此使用 Google Colab,首先,請(qǐng)確保你已安裝 OpenCV。你可以使用 pip 安裝它:
pip install opencv-python
步驟 02
請(qǐng)確保這些庫(kù)已經(jīng)安裝。
import cv2
pip install numpy
pip install matplotlib
步驟 03
在檢測(cè)人臉之前,我們必須使用 Google Colab 打開網(wǎng)絡(luò)攝像頭。
from google.colab.patches import cv2_imshow
步驟 04
運(yùn)行這兩個(gè)代碼后,網(wǎng)絡(luò)攝像頭打開,你可以拍張照片。
from IPython.display import display, Javascript
from google.colab.output import eval_js
from base64 import b64decode
def take_photo(filename='photo.jpg', quality=0.8):
js = Javascript('''
async function takePhoto(quality) {
const div = document.createElement('div');
const capture = document.createElement('button');
capture.textContent = 'Capture';
div.a(chǎn)ppendChild(capture);
const video = document.createElement('video');
video.style.display = 'block';
const stream = await navigator.mediaDevices.getUserMedia({video: true});
document.body.a(chǎn)ppendChild(div);
div.a(chǎn)ppendChild(video);
video.srcObject = stream;
await video.play();
// Resize the output to fit the video element.
google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);
// Wait for Capture to be clicked.
await new Promise((resolve) => capture.onclick = resolve);
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
stream.getVideoTracks()[0].stop();
div.remove();
return canvas.toDataURL('image/jpeg', quality);
}
''')
display(js)
data = eval_js('takePhoto({})'.format(quality))
binary = b64decode(data.split(',')[1])
with open(filename, 'wb') as f:
f.write(binary)
return filename
from IPython.display import Image
try:
filename = take_photo()
print('Saved to {}'.format(filename))
# Show the image which was just taken.
display(Image(filename))
except Exception as err:
# Errors will be thrown if the user does not have a webcam or if they do not
# grant the page permission to access it.
print(str(err))
照片保存為 photo.jpg。
photo.jpg
使用 Haar 級(jí)聯(lián)的人臉檢測(cè)是一種基于機(jī)器學(xué)習(xí)的方法,其中使用一組輸入數(shù)據(jù)訓(xùn)練級(jí)聯(lián)函數(shù)。OpenCV 已經(jīng)包含許多針對(duì)面部、眼睛、微笑等的預(yù)訓(xùn)練分類器。今天我們將使用面部分類器。你也可以嘗試使用其他分類器。
要檢測(cè)圖像中的人臉:
步驟 05
import cv2
img = cv2.imread('photo.jpg')
gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
nose_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_nose.xml')
# detect all the faces in the image
faces = face_cascade.detectMultiScale(gray_img,1.1,4)
# print the number of faces detected
print(f"{len(faces)} faces detected in the image.")
對(duì)于每個(gè)人臉,繪制一個(gè)綠色矩形:
步驟 06
for x, y, width, height in faces:
cv2.rectangle(img, (x, y), (x + width, y + height), color=(0, 255, 0), thickness=2)
用矩形保存圖像:
步驟 07
# save the image with rectangles
cv2.imwrite("photo_detected.jpg", img)
轉(zhuǎn)到文件 photo_detected.jpg 并打開 。
結(jié)果:
photo_detected.jpg
原文標(biāo)題 : 使用 OpenCV 進(jìn)行人臉檢測(cè)

發(fā)表評(píng)論
請(qǐng)輸入評(píng)論內(nèi)容...
請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字
圖片新聞
-
馬云重返一線督戰(zhàn),阿里重啟創(chuàng)始人模式
-
機(jī)器人奧運(yùn)會(huì)戰(zhàn)報(bào):宇樹機(jī)器人摘下首金,天工Ultra搶走首位“百米飛人”
-
存儲(chǔ)圈掐架!江波龍起訴佰維,索賠121萬(wàn)
-
長(zhǎng)安汽車母公司突然更名:從“中國(guó)長(zhǎng)安”到“辰致科技”
-
豆包前負(fù)責(zé)人喬木出軌BP后續(xù):均被辭退
-
字節(jié)AI Lab負(fù)責(zé)人李航卸任后返聘,Seed進(jìn)入調(diào)整期
-
員工持股爆雷?廣汽埃安緊急回應(yīng)
-
中國(guó)“智造”背后的「關(guān)鍵力量」
最新活動(dòng)更多
-
10月23日火熱報(bào)名中>> 2025是德科技創(chuàng)新技術(shù)峰會(huì)
-
10月23日立即報(bào)名>> Works With 開發(fā)者大會(huì)深圳站
-
10月24日立即參評(píng)>> 【評(píng)選】維科杯·OFweek 2025(第十屆)物聯(lián)網(wǎng)行業(yè)年度評(píng)選
-
即日-11.25立即下載>>> 費(fèi)斯托白皮書《柔性:汽車生產(chǎn)未來(lái)的關(guān)鍵》
-
11月27日立即報(bào)名>> 【工程師系列】汽車電子技術(shù)在線大會(huì)
-
12月18日立即報(bào)名>> 【線下會(huì)議】OFweek 2025(第十屆)物聯(lián)網(wǎng)產(chǎn)業(yè)大會(huì)
推薦專題
-
10 大模型的盡頭是開源
- 1 特斯拉工人被故障機(jī)器人打成重傷,索賠3.6億
- 2 【行業(yè)深度研究】退居幕后四年后,張一鳴終于把算法公司變成AI公司?
- 3 AI 時(shí)代,阿里云想當(dāng)“安卓” ,那誰(shuí)是“蘋果”?
- 4 華為公布昇騰芯片三年計(jì)劃,自研HBM曝光
- 5 硬剛英偉達(dá)!華為發(fā)布全球最強(qiáng)算力超節(jié)點(diǎn)和集群
- 6 機(jī)器人9月大事件|3家國(guó)產(chǎn)機(jī)器人沖刺IPO,行業(yè)交付與融資再創(chuàng)新高!
- 7 谷歌“香蕉”爆火啟示:國(guó)產(chǎn)垂類AI的危機(jī)還是轉(zhuǎn)機(jī)?
- 8 00后華裔女生靠?jī)刹緼I電影狂賺7.8億人民幣,AI正式進(jìn)軍好萊塢
- 9 美光:AI Capex瘋投不止,終于要拉起存儲(chǔ)超級(jí)周期了?
- 10 華為已殺入!AI領(lǐng)域最熱黃金賽道,大廠的數(shù)字人美女讓我一夜沒(méi)睡著覺(jué)