近期辞了前端实习,开始考虑考研了。考研就不走开发,选个专业相关的方向大概还轻松点吧( 计算机视觉感觉挺好玩的,一定要学下去!!
一、前置条件
安装opencv
opencv官网
pip install opencv-python
二、开始学习
1.图片处理相关操作
别着急,这里只是学习基本操作
import cv2 as cv
img = cv.imread('face1.jpeg')
gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('gray', gray_img)
cv.imwrite('gray_fac1.jpeg', gray_img)
resize_img = cv.resize(img, dsize=(200,200))
cv.imshow('resize_img', resize_img)
x, y, w, h = 100, 100, 100, 100
cv.rectangle(img,(x, y, x+w, y+h), color=(0, 0, 255), thickness=1)
cv.circle(img, center=(x+w, y+h), radius=100, color=(255, 0, 0), thickness=2)
cv.imshow('read_img', img)
while True:
if ord('e') == cv.waitKey(0):
break
cv.destroyAllWindows()
2.视频检测
别着急,还没开始人脸识别,但是会出现摄像头!
import cv2 as cv
def face_detect_demo(img):
gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
face_detect = cv.CascadeClassifier('D:\GoogleDownload\opencv\opencv\sources\data\haarcascades/haarcascade_frontalface_default.xml')
face = face_detect.detectMultiScale(gray_img)
for x,y,w,h in face:
cv.rectangle(img, (x, y), (x+w, y+h), color=(0, 0, 255), thickness=2)
cv.imshow('result',img)
cap = cv.VideoCapture(0)
while True:
flag, frame = cap.read()
if not flag:
break
face_detect_demo(frame)
if ord('e') == cv.waitKey(0):
break
cv.destroyAllWindows()
cap.release()
3.信息录入
这段是将视频切成图片存储起来
import cv2
cap = cv2.VideoCapture(0)
flag = 1
num = 1
while(cap.isOpened()):
ret_flag, Vshow = cap.read()
cv2.imshow("Capture_Test",Vshow)
k = cv2.waitKey(1) & 0xFF
if k == ord('s'):
cv2.imwrite('E:/PythonFiles/opencv/data/'+str(num)+'.name'+".jpg", Vshow)
print("success to save"+str(num)+".jpg")
print("------------")
num += 1
elif k == ord(' '):
break
cap.release()
cv2.destroyAllWindows()
4.数据训练
开始训练数据了!
更新中…
5.人脸识别
终于开始识别了!
更新中…
|