Camera face recognition capture and format storage based on opencv (python)

I just came into contact with opencv. I made a small code for video avatar capture with reference to the sample example of opencv. By the way, I will learn and use it together. Let’s start with the video capture and storage code:

# -*- coding: cp936 -*-
import cv2

capture=cv2.VideoCapture(0)
#将capture保存为motion-jpeg,cv_fourcc为保存格式
size = (int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
        int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
#cv_fourcc值要设置对,不然无法写入,而且不报错,坑
video=cv2.VideoWriter("VideoTest.avi", cv2.cv.CV_FOURCC('I','4','2','0'), 30, size)
#isopened可以查看摄像头是否开启
print capture.isOpened()
num=0
#要不断读取image需要设置一个循环
while True:
    ret,img=capture.read()
    #视频中的图片一张张写入
    video.write(img)
    cv2.imshow('Video',img)
    key=cv2.waitKey(1)#里面数字为delay时间,如果大于0为刷新时间,
    #超过指定时间则返回-1,等于0没有返回值,但也可以读取键盘数值,
    cv2.imwrite('%s.jpg'%(str(num)),img)
    num=num+1
    if key==ord('q'):#ord为键盘输入对应的整数,
        break
video.release()
#如果不用release方法的话无法储存,要等结束程序再等摄像头关了才能显示保持成功
capture.release()#把摄像头也顺便关了

cv2.destroyAllWindows()

Opencv video capture is very simple, you can mainly use videowriter. The main thing to note is that the capture in opencv is placed in memory, so you need a release command, otherwise you can only wait until the program is closed for garbage collection. released. The video capture will not be on the picture.

Then there is face recognition. Opencv comes with a lot of feature libraries, including faces, and there are many eyes. The principle is the same, but the recognition rate of the eye library is not high, so go directly to the code:

#coding=utf-8
import cv2
import cv2.cv as cv

img = cv2.imread("5.jpg")

def detect(img, cascade):
    '''detectMultiScale函数中smallImg表示的是要检测的输入图像为smallImg,
faces表示检测到的人脸目标序列,1.3表示每次图像尺寸减小的比例为1.3,
 4表示每一个目标至少要被检测到3次才算是真的目标(因为周围的像素和不同的窗口大小都可以检测到人脸),
 CV_HAAR_SCALE_IMAGE表示不是缩放分类器来检测,而是缩放图像,Size(20, 20)为目标的最小最大尺寸'''
    rects = cascade.detectMultiScale(img, scaleFactor=1.3,
                                     minNeighbors=5, minSize=(30, 30), flags = cv.CV_HAAR_SCALE_IMAGE)
    if len(rects) == 0:
        return []
    rects[:,2:] += rects[:,:2]
    print rects
    return rects

#在img上绘制矩形
def draw_rects(img, rects, color):
    for x1, y1, x2, y2 in rects:
        cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)


#转换为灰度图
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#直方图均衡处理
gray = cv2.equalizeHist(gray)

#脸部特征分类地址,里面还有其他
cascade_fn = 'data/haarcascades/haarcascade_frontalface_alt.xml'

#读取分类器,CascadeClassifier下面有一个detectMultiScale方法来得到矩形
cascade = cv2.CascadeClassifier(cascade_fn)

#通过分类器得到rects
rects = detect(gray, cascade)

#vis为img副本
vis = img.copy()

#画矩形
draw_rects(vis, rects, (0, 255, 0))

cv2.imshow('facedetect', vis)

cv2.waitKey(0)
cv2.destroyAllWindows()

Directly on the effect image and the original image comparison:

http://my.oschina.net/Kanonpy/

------------ yao yao yao --------------- yao ---- --------yao------------yao-------------yao------------yao- ------

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324376087&siteId=291194637