-
Notifications
You must be signed in to change notification settings - Fork 1
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
coldzoo
committed
Jan 22, 2016
0 parents
commit eb122ff
Showing
4 changed files
with
21,019 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,80 @@ | ||
# -*-coding:utf-8-*- | ||
import cv2 | ||
import numpy as np | ||
import caffe | ||
import os | ||
|
||
SKIPCOUNT = 5 #跳帧数目 | ||
|
||
def fe_init(): | ||
global caffe_root | ||
global net | ||
global transformer | ||
caffe.set_mode_gpu() | ||
net = caffe.Net('deploy.prototxt', 'fe_train_iter_40000.caffemodel', caffe.TEST) | ||
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) | ||
transformer.set_transpose('data', (2, 0, 1)) | ||
transformer.set_mean('data', np.load('fe_test_mean.npy').mean(1).mean(1)) | ||
transformer.set_raw_scale('data', 255) | ||
transformer.set_channel_swap('data', (2, 1, 0)) | ||
net.blobs['data'].reshape(50, 3, 227, 227) | ||
|
||
def fe_predict(file_name): | ||
global net | ||
net.blobs['data'].data[...] = transformer.preprocess('data', caffe.io.load_image(file_name)) | ||
out = net.forward() | ||
labels = np.loadtxt('synset_words.txt', str, delimiter='\t') | ||
top_k = net.blobs['prob'].data[0].flatten().argsort()[-1:-6:-1] | ||
#print(labels[out['prob'].argmax()]) | ||
index = out['prob'].argmax() | ||
#print(out['prob'][index, index]) | ||
return [labels[out['prob'].argmax()], out['prob'][index, index]] | ||
|
||
|
||
cv2.namedWindow('faceDetect') | ||
cap = cv2.VideoCapture(0) #打开0号摄像头 | ||
success, frame = cap.read()#读取一桢图像,前一个返回值是是否成功,后一个返回值是图像本身 | ||
size = frame.shape[:2]#获得当前桢彩色图像的大小 | ||
color = (255, 0, 0)#设置人脸框的颜色 | ||
classifier = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml")#定义分类器 | ||
faceid = 0 | ||
fe_init() | ||
skipcount = SKIPCOUNT#每SKIPCOUNT帧判断一帧 | ||
while success: | ||
success, frame = cap.read() | ||
image = np.zeros(size, dtype=np.float16)#定义一个与当前桢图像大小相同的的灰度图像矩阵 | ||
image = cv2.cvtColor(frame, cv2.cv.CV_BGR2GRAY)#将当前桢图像转换成灰度图像 | ||
cv2.equalizeHist(image, image)#灰度图像进行直方图等距化 | ||
|
||
#如下三行是设定最小图像的大小 | ||
divisor = 8 | ||
h, w = size | ||
minSize = (w/divisor, h/divisor) | ||
faceRects = classifier.detectMultiScale(image, 1.1, 4, cv2.CASCADE_SCALE_IMAGE, minSize)#人脸检测 | ||
if len(faceRects) > 0: # 如果人脸数组长度大于0 | ||
faceRect = faceRects[0] | ||
x, y, w, h = faceRect | ||
if w > 0 and skipcount == 0: | ||
skipcount = SKIPCOUNT | ||
im = image[y:y+w, x:x+h] | ||
save_name = 'test/face'+str(faceid)+'.jpg' | ||
faceid += 1 | ||
im = cv2.resize(im, (227, 227)) | ||
cv2.imwrite(save_name, im) | ||
[emotion, confidence] = fe_predict(save_name) | ||
os.remove(save_name) | ||
if confidence > 0.5: | ||
print(emotion) | ||
elif w > 0: | ||
skipcount -= 1 | ||
cv2.rectangle(image, (x, y), (x+w, y+h), color) | ||
cv2.imshow("faceDetect", image)#显示图像 | ||
|
||
key= cv2.waitKey(10) | ||
c = chr(key & 255) | ||
if c in ['q', 'Q', chr(27)]: | ||
break | ||
|
||
|
||
|
||
cv2.destroyWindow("faceDetect") |
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,212 @@ | ||
name: "CaffeNet" | ||
input: "data" | ||
input_dim: 10 | ||
input_dim: 3 | ||
input_dim: 227 | ||
input_dim: 227 | ||
layer { | ||
name: "conv1" | ||
type: "Convolution" | ||
bottom: "data" | ||
top: "conv1" | ||
convolution_param { | ||
num_output: 96 | ||
kernel_size: 11 | ||
stride: 4 | ||
} | ||
} | ||
layer { | ||
name: "relu1" | ||
type: "ReLU" | ||
bottom: "conv1" | ||
top: "conv1" | ||
} | ||
layer { | ||
name: "pool1" | ||
type: "Pooling" | ||
bottom: "conv1" | ||
top: "pool1" | ||
pooling_param { | ||
pool: MAX | ||
kernel_size: 3 | ||
stride: 2 | ||
} | ||
} | ||
layer { | ||
name: "norm1" | ||
type: "LRN" | ||
bottom: "pool1" | ||
top: "norm1" | ||
lrn_param { | ||
local_size: 5 | ||
alpha: 0.0001 | ||
beta: 0.75 | ||
} | ||
} | ||
layer { | ||
name: "conv2" | ||
type: "Convolution" | ||
bottom: "norm1" | ||
top: "conv2" | ||
convolution_param { | ||
num_output: 256 | ||
pad: 2 | ||
kernel_size: 5 | ||
group: 2 | ||
} | ||
} | ||
layer { | ||
name: "relu2" | ||
type: "ReLU" | ||
bottom: "conv2" | ||
top: "conv2" | ||
} | ||
layer { | ||
name: "pool2" | ||
type: "Pooling" | ||
bottom: "conv2" | ||
top: "pool2" | ||
pooling_param { | ||
pool: MAX | ||
kernel_size: 3 | ||
stride: 2 | ||
} | ||
} | ||
layer { | ||
name: "norm2" | ||
type: "LRN" | ||
bottom: "pool2" | ||
top: "norm2" | ||
lrn_param { | ||
local_size: 5 | ||
alpha: 0.0001 | ||
beta: 0.75 | ||
} | ||
} | ||
layer { | ||
name: "conv3" | ||
type: "Convolution" | ||
bottom: "norm2" | ||
top: "conv3" | ||
convolution_param { | ||
num_output: 384 | ||
pad: 1 | ||
kernel_size: 3 | ||
} | ||
} | ||
layer { | ||
name: "relu3" | ||
type: "ReLU" | ||
bottom: "conv3" | ||
top: "conv3" | ||
} | ||
layer { | ||
name: "conv4" | ||
type: "Convolution" | ||
bottom: "conv3" | ||
top: "conv4" | ||
convolution_param { | ||
num_output: 384 | ||
pad: 1 | ||
kernel_size: 3 | ||
group: 2 | ||
} | ||
} | ||
layer { | ||
name: "relu4" | ||
type: "ReLU" | ||
bottom: "conv4" | ||
top: "conv4" | ||
} | ||
layer { | ||
name: "conv5" | ||
type: "Convolution" | ||
bottom: "conv4" | ||
top: "conv5" | ||
convolution_param { | ||
num_output: 256 | ||
pad: 1 | ||
kernel_size: 3 | ||
group: 2 | ||
} | ||
} | ||
layer { | ||
name: "relu5" | ||
type: "ReLU" | ||
bottom: "conv5" | ||
top: "conv5" | ||
} | ||
layer { | ||
name: "pool5" | ||
type: "Pooling" | ||
bottom: "conv5" | ||
top: "pool5" | ||
pooling_param { | ||
pool: MAX | ||
kernel_size: 3 | ||
stride: 2 | ||
} | ||
} | ||
layer { | ||
name: "fc6" | ||
type: "InnerProduct" | ||
bottom: "pool5" | ||
top: "fc6" | ||
inner_product_param { | ||
num_output: 4096 | ||
} | ||
} | ||
layer { | ||
name: "relu6" | ||
type: "ReLU" | ||
bottom: "fc6" | ||
top: "fc6" | ||
} | ||
layer { | ||
name: "drop6" | ||
type: "Dropout" | ||
bottom: "fc6" | ||
top: "fc6" | ||
dropout_param { | ||
dropout_ratio: 0.5 | ||
} | ||
} | ||
layer { | ||
name: "fc7" | ||
type: "InnerProduct" | ||
bottom: "fc6" | ||
top: "fc7" | ||
inner_product_param { | ||
num_output: 4096 | ||
} | ||
} | ||
layer { | ||
name: "relu7" | ||
type: "ReLU" | ||
bottom: "fc7" | ||
top: "fc7" | ||
} | ||
layer { | ||
name: "drop7" | ||
type: "Dropout" | ||
bottom: "fc7" | ||
top: "fc7" | ||
dropout_param { | ||
dropout_ratio: 0.5 | ||
} | ||
} | ||
layer { | ||
name: "myfc8" | ||
type: "InnerProduct" | ||
bottom: "fc7" | ||
top: "myfc8" | ||
inner_product_param { | ||
num_output: 7 | ||
} | ||
} | ||
layer { | ||
name: "prob" | ||
type: "Softmax" | ||
bottom: "myfc8" | ||
top: "prob" | ||
} |
Oops, something went wrong.