【人脸识别】OpenCV获取自己的图像

思路:先获取10000张自己的图像,然后通过CNN神经网络进行学习。
第一步:先获取自己的脸的数据。如何做?
代码如下:

import cv2
import os
import sys
import random

output_dir = 'E:/myface'

# 上述目录不存在,创建一个temp目录
if not os.path.exists(output_dir):
    os.makedirs(output_dir)


# 打开摄像头 参数为输入流,可以为摄像头或视频文件
camera = cv2.VideoCapture(0)

index = 1
while True:
	if (index <= 10000):
		print('Being processed picture %s' % index)
## 获取我的脸部图片
		success, img = camera.read()
		cv2.imshow('image', img)
		cv2.imwrite(output_dir+'/'+str(index)+'.jpg', img)
		index += 1
	else:
		print('Finished!')
		break

图像效果:

猜你喜欢

转载自www.cnblogs.com/zhangshengdong/p/12682160.html