计算机视觉是机器学习的一种应用,而且是最有价值的应用
import cv2 as cv
import numpy as np
# 第一步,创建Haar级联器
facer = cv.CascadeClassifier(r'C:\ProgramData\Anaconda3\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')
# 第二步,导入人脸识别的图片并将其灰度化
img = cv.imread(r"C:\Users\Administrator\Desktop\girl.png")
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
# 第三步,进行人脸识别
# [[x,y,w,h]]
faces = facer.detectMultiScale(gray, 1.2, 5)
for (x, y, w, h) in faces:
cv.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2)
cv.imshow("img", img)
cv.waitKey(0)
import cv2 as cv
import numpy as np
# 第一步,创建Haar级联器
facer = cv.CascadeClassifier(r'C:\ProgramData\Anaconda3\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')
eye = cv.CascadeClassifier(r'C:\ProgramData\Anaconda3\Lib\site-packages\cv2\data\haarcascade_eye.xml')
# 第二步,导入人脸识别的图片并将其灰度化
img = cv.imread('./tt.jpg')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# 第三步,进行人脸识别
# [[x,y,w,h]]
faces = facer.detectMultiScale(gray, 1.2, 5)
i = 0
for (x, y, w, h) in faces:
cv.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2)
roi_img = img[y:y+h, x:x+w]
eyes = eye.detectMultiScale(roi_img, 1.2, 5)
for (x, y, w, h) in eyes:
cv.rectangle(roi_img, (x, y), (x + w, y + h), (255, 0, 0), 2)
i = i+1
winname = "fave" + str(i)
cv.imshow(winname, roi_img)
cv.waitKey(0)