Skip to content

Instantly share code, notes, and snippets.

@e96031413
Last active October 21, 2019 08:49
Show Gist options
  • Save e96031413/7baa50759d7010870bb8b65da07000fb to your computer and use it in GitHub Desktop.
Save e96031413/7baa50759d7010870bb8b65da07000fb to your computer and use it in GitHub Desktop.
使用Python的OpenCV進行影片人臉辨識
import cv2
# 載入分類器
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# 從視訊鏡頭擷取影片.
#cap = cv2.VideoCapture(0)
# 使用現有影片
cap = cv2.VideoCapture('test.mp4')
while True:
# Read the frame
_, img = cap.read()
# 轉成灰階
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 偵測臉部
faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=3,
minSize=(25, 25))
# 繪製人臉部份的方框
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 255, 0), 2)
# 顯示成果
cv2.imshow('img', img)
#計算找到幾張臉
print("找到了 {0} 張臉.".format(len(faces)))
# 按下ESC結束程式執行
k = cv2.waitKey(30) & 0xff
if k==27:
break
# Release the VideoCapture object
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment