forked from cynthia0811/Face-Recognition-master
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 981a081
Showing
12 changed files
with
58,224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
利用OpenCV、CNN进行人脸识别 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/bin/python | ||
# -*- coding: utf8 -*- | ||
# __author__ = '尹籽' | ||
|
||
import cv2 | ||
import dlib | ||
import os | ||
import sys | ||
import random | ||
|
||
output_dir = './faces_creat/222' | ||
size = 200 | ||
|
||
if not os.path.exists(output_dir): | ||
os.makedirs(output_dir) | ||
|
||
# 改变图片的亮度与对比度 | ||
def relight(img, light=1, bias=0): | ||
w = img.shape[1] | ||
h = img.shape[0] | ||
#image = [] | ||
for i in range(0,w): | ||
for j in range(0,h): | ||
for c in range(3): | ||
tmp = int(img[j,i,c]*light + bias) | ||
if tmp > 255: | ||
tmp = 255 | ||
elif tmp < 0: | ||
tmp = 0 | ||
img[j,i,c] = tmp | ||
return img | ||
|
||
#使用dlib自带的frontal_face_detector作为我们的特征提取器 | ||
detector = dlib.get_frontal_face_detector() | ||
# 打开摄像头 参数为输入流,可以为摄像头或视频文件 | ||
camera = cv2.VideoCapture(0) | ||
|
||
index = 1 | ||
while True: | ||
if (index <= 600): | ||
print('Being processed picture %s' % index) | ||
# 从摄像头读取照片 | ||
success, img = camera.read() | ||
# 转为灰度图片 | ||
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | ||
# 使用detector进行人脸检测 | ||
dets = detector(gray_img, 1) | ||
|
||
for i, d in enumerate(dets): | ||
x1 = d.top() if d.top() > 0 else 0 | ||
y1 = d.bottom() if d.bottom() > 0 else 0 | ||
x2 = d.left() if d.left() > 0 else 0 | ||
y2 = d.right() if d.right() > 0 else 0 | ||
|
||
face = img[x1:y1,x2:y2] | ||
# 调整图片的对比度与亮度, 对比度与亮度值都取随机数,这样能增加样本的多样性 | ||
face = relight(face, random.uniform(0.5, 1.5), random.randint(-50, 50)) | ||
|
||
face = cv2.resize(face, (size,size)) | ||
gray= cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) | ||
|
||
cv2.imshow('image', face) | ||
|
||
cv2.imwrite(output_dir+'/'+str(index)+'l.jpg', gray) | ||
|
||
index += 1 | ||
key = cv2.waitKey(30) & 0xff | ||
if key == 27: | ||
break | ||
else: | ||
print('Finished!') | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import cv2 | ||
import dlib | ||
import os | ||
import sys | ||
import random | ||
|
||
output_dir = './faces_creat/xueru' | ||
size = 200 | ||
|
||
if not os.path.exists(output_dir): | ||
os.makedirs(output_dir) | ||
|
||
# 改变图片的亮度与对比度 | ||
def relight(img, light=1, bias=0): | ||
w = img.shape[1] | ||
h = img.shape[0] | ||
#image = [] | ||
for i in range(0,w): | ||
for j in range(0,h): | ||
for c in range(3): | ||
tmp = int(img[j,i,c]*light + bias) | ||
if tmp > 255: | ||
tmp = 255 | ||
elif tmp < 0: | ||
tmp = 0 | ||
img[j,i,c] = tmp | ||
return img | ||
|
||
#使用dlib自带的frontal_face_detector作为我们的特征提取器 | ||
detector = dlib.get_frontal_face_detector() | ||
# 打开摄像头 参数为输入流,可以为摄像头或视频文件 | ||
camera = cv2.VideoCapture(0) | ||
|
||
index = 1 | ||
while True: | ||
if (index <= 600): | ||
print('Being processed picture %s' % index) | ||
# 从摄像头读取照片 | ||
success, img = camera.read() | ||
# 转为灰度图片 | ||
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | ||
# 使用detector进行人脸检测 | ||
dets = detector(gray_img, 1) | ||
|
||
for i, d in enumerate(dets): | ||
x1 = d.top() if d.top() > 0 else 0 | ||
y1 = d.bottom() if d.bottom() > 0 else 0 | ||
x2 = d.left() if d.left() > 0 else 0 | ||
y2 = d.right() if d.right() > 0 else 0 | ||
|
||
face = img[x1:y1,x2:y2] | ||
# 调整图片的对比度与亮度, 对比度与亮度值都取随机数,这样能增加样本的多样性 | ||
face = relight(face, random.uniform(0.5, 1.5), random.randint(-50, 50)) | ||
|
||
face = cv2.resize(face, (size,size)) | ||
gray= cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) | ||
|
||
cv2.imshow('image', face) | ||
|
||
cv2.imwrite(output_dir+'/'+str(index)+'.jpg', gray) | ||
|
||
index += 1 | ||
key = cv2.waitKey(30) & 0xff | ||
if key == 27: | ||
break | ||
else: | ||
print('Finished!') | ||
break |
Oops, something went wrong.