opencv中自帶了haar人臉特征分類器借宵。
1.1 image如果為彩色圖:image.shape[0] [1] [2] (水平壤玫、垂直像素、通道數(shù))
1.2將圖片變?yōu)榛叶葓D
1.3它可以檢測(cè)出圖片中所有的人臉楚里,并將人臉用vector保存各個(gè)人臉的坐標(biāo)猎贴、大小(用矩形表示)
1.4調(diào)整scaleFactor參數(shù)的大小达址,可以增加識(shí)別的靈敏度沉唠,推薦1.1
1.5CASC_PATH = 你的haarcascade_frontalface_alt2.xml文件地址
def format_image(image):
# image如果為彩色圖:image.shape[0][1][2](水平苛败、垂直像素、通道數(shù))
if len(image.shape) > 2 and image.shape[2] == 3:
# 將圖片變?yōu)榛叶葓D
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 它可以檢測(cè)出圖片中所有的人臉嘀韧,并將人臉用vector保存各個(gè)人臉的坐標(biāo)乳蛾、大斜杀摇(用矩形表示)
# 調(diào)整scaleFactor參數(shù)的大小,可以增加識(shí)別的靈敏度因惭,推薦1.1
#CASC_PATH = 你的haarcascade_frontalface_alt2.xml文件地址
CASC_PATH = 'E:/miniconda/envs/lstm/Lib/site-packages/cv2/data/haarcascade_frontalface_alt2.xml' #
cascade_classifier = cv2.CascadeClassifier(CASC_PATH)
faces = cascade_classifier.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5)
# 如果圖片中沒有檢測(cè)到人臉蹦魔,則返回None
if not len(faces) > 0:
return None, None
# max_are_face包含了人臉的坐標(biāo)咳燕,大小
max_are_face = faces[0]
# 在所有人臉中選一張最大的臉
for face in faces:
if face[2] * face[3] > max_are_face[2] * max_are_face[3]:
max_are_face = face
# 這兩步可有可無
face_coor = max_are_face
image = image[face_coor[1]:(face_coor[1] + face_coor[2]), face_coor[0]:(face_coor[0] + face_coor[3])]
# 調(diào)整圖片大小招盲,變?yōu)?8*48
try:
image = cv2.resize(image, (48, 48), interpolation=cv2.INTER_CUBIC)
except Exception:
print("problem during resize")
return None, None
return image, face_coor
對(duì)接到視頻中,使用cv2.VideoCapture(0)
capture = cv2.VideoCapture(0)
fps = 0.0
while (True):
t1 = time.time()
# 讀取某一幀
ref, frame = capture.read()
# 格式轉(zhuǎn)變咆繁,BGRtoRGB
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 轉(zhuǎn)變成Image
# 進(jìn)行檢測(cè)
(p_image, face_coor) = format_image(frame)
if face_coor is not None:
# 獲取人臉的坐標(biāo),并用矩形框出
[x, y, w, h] = face_coor
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 1)
# time.sleep(0.2)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
# fps = int(round(capture.get(cv2.CAP_PROP_FPS)))
fps = (fps + (1. / (time.time() - t1)))/2
frame = cv2.putText(frame, "fps= %.2f" % (fps), (0, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow("video", frame)
c = cv2.waitKey(1) & 0xff
if c == 27:
capture.release()
break
capture.release()
cv2.destroyAllWindows()
整體代碼:
import time
import cv2
def format_image(image):
# image如果為彩色圖:image.shape[0][1][2](水平玩般、垂直像素坏为、通道數(shù))
if len(image.shape) > 2 and image.shape[2] == 3:
# 將圖片變?yōu)榛叶葓D
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 它可以檢測(cè)出圖片中所有的人臉久脯,并將人臉用vector保存各個(gè)人臉的坐標(biāo)镰吆、大小(用矩形表示)
# 調(diào)整scaleFactor參數(shù)的大小摧找,可以增加識(shí)別的靈敏度牢硅,推薦1.1
#CASC_PATH = 你的haarcascade_frontalface_alt2.xml文件地址
CASC_PATH = 'E:/miniconda/envs/lstm/Lib/site-packages/cv2/data/haarcascade_frontalface_alt2.xml' #
cascade_classifier = cv2.CascadeClassifier(CASC_PATH)
faces = cascade_classifier.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5)
# 如果圖片中沒有檢測(cè)到人臉减余,則返回None
if not len(faces) > 0:
return None, None
# max_are_face包含了人臉的坐標(biāo),大小
max_are_face = faces[0]
# 在所有人臉中選一張最大的臉
for face in faces:
if face[2] * face[3] > max_are_face[2] * max_are_face[3]:
max_are_face = face
# 這兩步可有可無
face_coor = max_are_face
image = image[face_coor[1]:(face_coor[1] + face_coor[2]), face_coor[0]:(face_coor[0] + face_coor[3])]
# 調(diào)整圖片大小堡牡,變?yōu)?8*48
try:
image = cv2.resize(image, (48, 48), interpolation=cv2.INTER_CUBIC)
except Exception:
print("problem during resize")
return None, None
return image, face_coor
if __name__ == "__main__":
capture = cv2.VideoCapture(0)
fps = 0.0
while (True):
t1 = time.time()
# 讀取某一幀
ref, frame = capture.read()
# 格式轉(zhuǎn)變晤柄,BGRtoRGB
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 轉(zhuǎn)變成Image
# 進(jìn)行檢測(cè)
(p_image, face_coor) = format_image(frame)
if face_coor is not None:
# 獲取人臉的坐標(biāo),并用矩形框出
[x, y, w, h] = face_coor
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 1)
# time.sleep(0.2)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
# fps = int(round(capture.get(cv2.CAP_PROP_FPS)))
fps = (fps + (1. / (time.time() - t1)))/2
frame = cv2.putText(frame, "fps= %.2f" % (fps), (0, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow("video", frame)
c = cv2.waitKey(1) & 0xff
if c == 27:
capture.release()
break
capture.release()
cv2.destroyAllWindows()