-
人脸识别的流程:人脸检测(人脸定位,特征点检测、68个特征点)、人脸对齐、提取人恋特征向量、人脸匹配。
-
应用:图片、视频-只看ta 如艺智美影视
识别图片中人脸
- 改变文件地址(文件地址最好不要有中文)
import cv2
import os
import matplotlib.pyplot as plt
os.chdir('D:\Python 32\Lib\site-packages\cv2\data')
print(os.getcwd())
- 此处更改的是py文件的地址,图片需要关联到相关data地址下,为今后方便再data下建立img文件夹
def detect(filename):
face_cascade = cv2.CascadeClassifier('D:\Python 32\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
plt.imshow(img)
plt.axis('off') #去掉坐标轴
plt.show()
- 运行函数
detect("img\old.jpg")
其他openCV文件的作用(data文件夹下)
openCV图像处理
-
参考链接:
知乎-openCV库基本介绍与基础函数
知乎-openCV库基本操作1
博客园-openCV图像处理
维基百科openCV
《python计算机视觉编程》 -
图片读取保存
import cv2
# 读取图像
im = cv2.imread('picpath')
h,w = im.shape[:2]
print h,w
# 保存图像
cv2.imwrite('result.png',im)
im = cv2.imread('empire.jpg')
# 创建灰度图像
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
#在读取原图像之后,紧接其后的是 OpenCV 颜色转换代码,其中最有用的一些转换
#代码如下:cv2.COLOR_BGR2GRAY(灰度图) /cv2.COLOR_BGR2RGB /cv2.COLOR_GRAY2BGR
openCV视频处理
- 参考文档:
openCV 视频文档
import cv2
cap = cv2.VideoCapture('footage.mp4')
while True:
ret, frame = cap.read()
if frame is None:
break
img_gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
img_blurred = cv2.GaussianBlur(img_gray, (5, 5), 0)
img_threshold1 = cv2.adaptiveThreshold(img_blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 2)
img_threshold1_blurred = cv2.GaussianBlur(img_threshold1, (5, 5), 0)
_, img_threshold2 = cv2.threshold(img_threshold1_blurred, 200, 255, cv2.THRESH_BINARY)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3, 3))
img_opening = cv2.bitwise_not(cv2.morphologyEx(cv2.bitwise_not(img_threshold2), cv2.MORPH_OPEN, kernel))
img_opening_blurred = cv2.GaussianBlur(img_opening, (3, 3), 0)
cv2.imshow('img_opening_blurred', img_opening_blurred)
if cv2.waitKey(40) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
import numpy as np
import cv2 as cv
cap = cv.VideoCapture('vtest.avi')
while cap.isOpened():
ret, frame = cap.read()
# if frame is read correctly ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
cv.imshow('frame', gray)
if cv.waitKey(1) == ord('q'):
break
cap.release()
cv.destroyAllWindows()
https://zhuanlan.zhihu.com/p/62703610
知乎openCV专栏: https://www.zhihu.com/topic/19587715/hot