a

Lorem ipsum dolor sit amet, consectetur adicing elit ut ullamcorper. leo, eget euismod orci. Cum sociis natoque penati bus et magnis dis.Proin gravida nibh vel velit auctor aliquet. Leo, eget euismod orci. Cum sociis natoque penati bus et magnis dis.Proin gravida nibh vel velit auctor aliquet.

  /  Project   /  Blog: Python影像辨識筆記(一):使用Open CV辨識圖片及影片中的人臉

Blog: Python影像辨識筆記(一):使用Open CV辨識圖片及影片中的人臉


圖片人臉

安裝模組

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, 1.1, 4)
# 繪製人臉部份的方框
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
# 輸出成果
cv2.imshow('img', img)
cv2.waitKey()

影片人臉

import cv2
# 載入分類器
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# 從視訊盡頭擷取影片
cap = cv2.VideoCapture(0)
或者....
# 使用現有影片
cap = cv2.VideoCapture('filename.mp4')
while True:
# Read the frame
_, img = cap.read()
# 轉成灰階
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 偵測臉部
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# 繪製人臉部份的方框
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# 輸出成果
cv2.imshow('img', img)
# Stop if escape key is pressed
k = cv2.waitKey(30) & 0xff
if k==27:
break

# Release the VideoCapture object
cap.release()

Source: Artificial Intelligence on Medium

(Visited 673 times, 1 visits today)
Post a Comment

Newsletter