Python影像辨識筆記(一):使用Open CV辨識圖片及影片中的人臉
5 min readApr 25, 2019
圖片人臉
安裝模組
pip install opencv-python
偵測人臉
import cv2# 載入分類器
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')# 讀取圖片
img = cv2.imread('a.jpg')# 轉成灰階圖片
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 偵測臉部faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.08,
minNeighbors=5,
minSize=(32, 32))# 繪製人臉部份的方框
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)#(0, 255, 0)欄位可以變更方框顏色(Blue,Green,Red)# 顯示成果
cv2.namedWindow('img', cv2.WINDOW_NORMAL) #正常視窗大小
cv2.imshow('img', img) #秀出圖片
cv2.imwrite( "result.jpg", img ) #保存圖片
cv2.waitKey(0) #等待按下任一按鍵
cv2.destroyAllWindows() #關閉視窗
- scaleFactor: Parameter specifying how much the image size is reduced at each image scale. Increasing it leads to faster detection with the risk of missing some objects, whereas a small value might sometimes be too thorough.
- minNeighbors: Parameter specifying how many neighbors each candidate rectangle should have to retain…