python3 8動態人臉識別

2021-10-09 14:45:08 字數 2300 閱讀 6540

pip install dlib

pip python-opencv

#coding: utf-8

"""從視屏中識別人臉,並實時標出面部特徵點

"""import dlib #人臉識別的庫dlib

import cv2 #影象處理的庫opencv

# 使用特徵提取器get_frontal_face_detector

detector = dlib.get_frontal_face_detector()

# cap = cv2.videocapture("row.mp4")

#建cv2攝像頭物件,這裡使用電腦自帶攝像頭,如果接了外部攝像頭,則自動切換到外部攝像頭

cap = cv2.videocapture(0)

cap.set(3, 480)

# 截圖screenshoot的計數器

cnt = 0

# cap.isopened() 返回true/false 檢查初始化是否成功

while(cap.isopened()):

# cap.read()

# 返回兩個值:

# 影象物件,影象的三維矩陣

flag, im_rd = cap.read()

# 每幀資料延時1ms,延時為0讀取的是靜態幀

k = cv2.waitkey(1)

# 取灰度

img_gray = cv2.cvtcolor(im_rd, cv2.color_rgb2gray)

# 使用人臉檢測器檢測每一幀影象中的人臉。並返回人臉數rects

faces = detector(img_gray, 0)

# 待會要顯示在螢幕上的字型

font = cv2.font_hershey_******x

# 如果檢測到人臉

if(len(faces)!=0):

# 對每個人臉都畫出框框

for i in range(len(faces)):

# enumerate方法同時返回資料物件的索引和資料,k為索引,d為faces中的物件

for k, d in enumerate(faces):

# 用紅色矩形框出人臉

cv2.rectangle(im_rd, (d.left(), d.top()), (d.right(), d.bottom()), (0, 255, 0),2)

# 計算人臉熱別框邊長

face_width = d.right() - d.left()

#在上方顯示文字

cv2.puttext(im_rd, str(face_width) , (d.left(), d.top()-20), font, 0.5, (255, 0, 0), 1)

# 標出人臉數

cv2.puttext(im_rd, "faces: "+str(len(faces)), (20,50), font, 1, (0, 0, 255), 1, cv2.line_aa)

else:

# 沒有檢測到人臉

cv2.puttext(im_rd, "no face", (20, 50), font, 1, (0, 0, 255), 1, cv2.line_aa)

# 新增說明

im_rd = cv2.puttext(im_rd, "s: screenshot", (20, 400), font, 0.8, (0, 0, 255), 1, cv2.line_aa)

im_rd = cv2.puttext(im_rd, "q: quit", (20, 450), font, 0.8, (0, 0, 255), 1, cv2.line_aa)

#檢測按鍵

k = cv2.waitkey(1)

# 按下s鍵截圖儲存

# 按下q鍵退出

if(k == ord('q')):

break

# 視窗顯示

cv2.imshow("camera", im_rd)

# 釋放攝像頭

cap.release()

# 刪除建立的視窗

python之動態人臉識別

執行準備 所需檔案 haarcascade frontalface default.xml 獲取方式 1.開啟cmd,輸入指令 pip install i cv2 2.在安裝python的目錄下面,依次開啟資料夾 lib site packages cv2 data 所需檔案就在裡面了 實現效果 開...

python3 8使用aiml總結

切換到.aiml所在工作目錄 os.chdir alice path alice aiml.kernel 通過std startup.xml啟動aiml alice.learn std startup.xml aiml檔案有修改時可以通過load aiml b 在xml中pattern配置 進行修改...

Python3 8下使用tornado報錯

tornado官網文件 在windows上,tornado需要使用windowsselectoreventloop。這是python 3.7和更早版本的預設值,但python 3.8預設為事件迴圈,與tornado不相容。在python 3.8中使用tornado的應用程式必須在其主檔案 函式的開頭...