https://www.cnblogs.com/31415926535x/p/10620732.html
之前為了配置tensorflow-gpu的環境又是裝cuda,又是裝cudnn,還有tensoflow-gpu等等,,因為當時也是第一次搭建這個環境,所以完全是按照別人的搭建方法來一步一步的弄得,,后來我在給室友安裝環境的時候,發現cuda,cudnn什么的完全不用自己安裝,,,全部交給 anaconda3 (好東西)就行了
Anaconda3安裝
幾乎最后所有的東西都是用這個完成的,,所以先去安裝這玩意,,
直接官網下載就行了,,安裝的時候記得選擇 PATH 配置,,不然之后還得自己去弄環境變量,,
然后在 powershell 里檢查一下確實配置成功就行了 conda -V
配置一個環境
因為我的電腦是 win10x64+gtx1050,,所以選擇安裝 tensorflow-gpu-1.9.0版的,,gpu版的到時候訓練模型的時候跑的很快,,(大概1s2-3張照片吧),如果用cpu跑的話有些慢,,1張照片可能要2s左右,,,
打開powsershell,,(千萬不要換源,,千萬不要換源,,千萬不要換源,,
創建一個環境
conda create -n [name] python=3.5 tensorflow-gpu=1.9.0
可能這一步會很慢,,但是建議不要去換源,,因為會出現下的東西不全,最后可能不能使用gpu版的tensorflow,,,
輸完這段命令后,,等一會會出現一些要安裝的東西列表,,這時主要看一下有沒有python, tensorflow-gpu, cudnn, cudatoolkit,,,都有的話就y確定等就行了,,,
環境的名字隨便起,,
激活環境
因為這時是powershell下,,,激活環境會不成功,,所以直接切換到cmd模式就行了,,輸 cmd
,,,
activate [name]
這時會發現前面多了一個 ([name])
的東西,表示激活環境成功,,,
然后再測試一下python下能不能調用 tensorflow-gpu 版,,測試的方法可以參考我的上一篇博客里后面那一部分內容 ,,,
運行簡單的人臉識別的實例
前面的准備工作弄好之后就可以運行一個簡單的實例看一下在這個環境下的運行情況,,,
下面的python程序是學長給我的,,然后我發現學長的程序是這個博主寫的項目,,其中也有我的一些改動,,下面會提到,,
下面的操作都是在剛剛創建的環境下操作的,,,否則的話會是anaconda3默認的base環境下,,,
安裝必備的庫
因為這個人臉識別的實現用到了 opencv, dlib等等,,所以先安裝這些,,
安裝opencv
conda install opencv
安裝dlib
這個玩意的安裝有點坑,,有時貌似直接安裝會安裝不上,,會提示沒有 cmake
這個包管理軟件,,所以要先安裝cmake,,建議是在anaconda3主程序(開始菜單里找 Anaconda Navigator)中找到你的那個環境,,然后再 uninstall 中找到 cmake 然后安裝,,,
但是這樣可能還是安裝不了dlib,,無論是用conda還是pip安裝,,
conda install dlib
pip install dlib
后來我找到一個解決方法,,去下載 dlib****.whl
然后本地安裝,,
再 DownloadFiles 中找到一個這個東西,,
dlib-19.1.0-cp35-cp35m-win_amd64.whl
然后放到你現在的路徑下,,pip install dlib-19.1.0-cp35-cp35m-win_amd64.whl
應該這樣就可以安裝上了dlib,,,當然你可以用其他的方法安裝,,網上也有很多解決方法,,,也有可能直接用 pip 就能安裝上(比如我的電腦就能,,室友的就會出現上面的錯誤,,得繞一個彎子)
安裝sklearn
這個簡單,,會在訓練那一步用到
pip install sklearn
運行實例
- get_my_faces.py: 獲取人臉並識別出來裁剪出來作為元數據
- set_other_faces.py: 獲取14000張人臉的照片作為訓練所要用的數據
- train_faces.py: 訓練模型
- is_my_face.py: 實時獲取人臉,並判斷是否和第一步所錄入的人臉相匹配
get_my_faces
這一步可以使用 dlib 的人臉識別裁剪,也可以使用opencv自帶的來使用,,和室友試驗了一下,發現opencv的雖然相對較快,但是識別不佳,而且同樣大小的視頻最后生成的照片個數也很少(也有可能是那里沒寫好),,
原博主的程序是拍一張照片然后識別一張裁剪一張,,這樣很慢,,所以我把它改成了錄一段視頻,然后對於每一幀來識別裁剪,,這樣賊快,,,(按q退出錄制后自動進行后面的內容
注意復制代碼后要適當的改一些參數,,比如說opencv中hear的參數等等
import cv2
import os
import dlib
import sys
import random
import shutil
def make_video():
# 錄制視頻
shutil.rmtree('./my_faces')
"""使用opencv錄像"""
cap = cv2.VideoCapture(0) # 默認的攝像頭
# 指定視頻代碼
fourcc = cv2.VideoWriter_fourcc(*"DIVX")
out = cv2.VideoWriter('233.avi', fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret:
out.write(frame)
#
cv2.imshow('frame',frame)
# 等待按鍵q操作關閉攝像頭
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
# 改變圖片的亮度與對比度
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
def hhh():
# 利用dlib來實現
output_dir = './my_faces'
size = 64
if not os.path.exists(output_dir):
os.makedirs(output_dir)
#使用dlib自帶的frontal_face_detector作為我們的特征提取器
detector = dlib.get_frontal_face_detector()
# 打開攝像頭 參數為輸入流,可以為攝像頭或視頻文件
#camera = cv2.VideoCapture(0)
camera = cv2.VideoCapture("233.avi")
index = 1
while True:
if (index <= 10000):
print('Being processed picture %s' % index)
# 從攝像頭讀取照片
success, img = camera.read()
# 轉為灰度圖片
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 使用detector進行人臉檢測
dets = detector(gray_img, 1)
if success == False:
break
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))
cv2.imshow('image', face)
cv2.imwrite(output_dir+'/'+str(index)+'.jpg', face)
index += 1
key = cv2.waitKey(30) & 0xff
if key == 27:
break
else:
print('Finished!')
break
def hhhh():
# 利用opencv來實現
output_dir = './my_faces'
size = 64
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 獲取分類器
haar = cv2.CascadeClassifier(r'G:\DIP\Anaconda3\envs\test1\Library\etc\haarcascades\haarcascade_frontalface_default.xml')
# 打開攝像頭 參數為輸入流,可以為攝像頭或視頻文件
camera = cv2.VideoCapture("233.avi")
n = 1
while 1:
if (n <= 10000):
print('It`s processing %s image.' % n)
# 讀幀
success, img = camera.read()
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = haar.detectMultiScale(gray_img, 1.3, 5)
for f_x, f_y, f_w, f_h in faces:
face = img[f_y:f_y+f_h, f_x:f_x+f_w]
face = cv2.resize(face, (64,64))
'''
if n % 3 == 1:
face = relight(face, 1, 50)
elif n % 3 == 2:
face = relight(face, 0.5, 0)
'''
face = relight(face, random.uniform(0.5, 1.5), random.randint(-50, 50))
cv2.imshow('img', face)
cv2.imwrite(output_dir+'/'+str(n)+'.jpg', face)
n+=1
key = cv2.waitKey(30) & 0xff
if key == 27:
break
else:
break
if __name__ == '__main__':
make_video()
hhh()
set_other_faces
這一步主要是識別裁剪那堆別人的照片
先去下那一堆照片,,然后解壓,重命名為 input_img
(只是驗證一下整個項目的效果的話可以刪去一半的照片,,不然可能得跑個10分鍾左右,,,
# -*- codeing: utf-8 -*-
import sys
import os
import cv2
import dlib
input_dir = './input_img'
output_dir = './other_faces'
size = 64
if not os.path.exists(output_dir):
os.makedirs(output_dir)
#使用dlib自帶的frontal_face_detector作為我們的特征提取器
detector = dlib.get_frontal_face_detector()
index = 1
for (path, dirnames, filenames) in os.walk(input_dir):
for filename in filenames:
if filename.endswith('.jpg'):
print('Being processed picture %s' % index)
img_path = path+'/'+filename
# 從文件讀取圖片
img = cv2.imread(img_path)
# 轉為灰度圖片
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 使用detector進行人臉檢測 dets為返回的結果
dets = detector(gray_img, 1)
#使用enumerate 函數遍歷序列中的元素以及它們的下標
#下標i即為人臉序號
#left:人臉左邊距離圖片左邊界的距離 ;right:人臉右邊距離圖片左邊界的距離
#top:人臉上邊距離圖片上邊界的距離 ;bottom:人臉下邊距離圖片上邊界的距離
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
# img[y:y+h,x:x+w]
face = img[x1:y1,x2:y2]
# 調整圖片的尺寸
face = cv2.resize(face, (size,size))
cv2.imshow('image',face)
# 保存圖片
cv2.imwrite(output_dir+'/'+str(index)+'.jpg', face)
index += 1
key = cv2.waitKey(30) & 0xff
if key == 27:
sys.exit(0)
train_faces
這一步就是訓練模型,,,剛開始會卡頓一會,,,之后就會跑起來,,,看一下是不是gpu跑,,cpu的話賊慢,,,gpu的話不到一分鍾左右就可以了,,,
import tensorflow as tf
import cv2
import numpy as np
import os
import random
import sys
from sklearn.model_selection import train_test_split
my_faces_path = './my_faces'
other_faces_path = './other_faces'
size = 64
imgs = []
labs = []
def getPaddingSize(img):
h, w, _ = img.shape
top, bottom, left, right = (0,0,0,0)
longest = max(h, w)
if w < longest:
tmp = longest - w
# //表示整除符號
left = tmp // 2
right = tmp - left
elif h < longest:
tmp = longest - h
top = tmp // 2
bottom = tmp - top
else:
pass
return top, bottom, left, right
def readData(path , h=size, w=size):
for filename in os.listdir(path):
if filename.endswith('.jpg'):
filename = path + '/' + filename
img = cv2.imread(filename)
top,bottom,left,right = getPaddingSize(img)
# 將圖片放大, 擴充圖片邊緣部分
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=[0,0,0])
img = cv2.resize(img, (h, w))
imgs.append(img)
labs.append(path)
readData(my_faces_path)
readData(other_faces_path)
# 將圖片數據與標簽轉換成數組
imgs = np.array(imgs)
labs = np.array([[0,1] if lab == my_faces_path else [1,0] for lab in labs])
# 隨機划分測試集與訓練集
train_x,test_x,train_y,test_y = train_test_split(imgs, labs, test_size=0.05, random_state=random.randint(0,100))
# 參數:圖片數據的總數,圖片的高、寬、通道
train_x = train_x.reshape(train_x.shape[0], size, size, 3)
test_x = test_x.reshape(test_x.shape[0], size, size, 3)
# 將數據轉換成小於1的數
train_x = train_x.astype('float32')/255.0
test_x = test_x.astype('float32')/255.0
print('train size:%s, test size:%s' % (len(train_x), len(test_x)))
# 圖片塊,每次取100張圖片
batch_size = 100
num_batch = len(train_x) // batch_size
x = tf.placeholder(tf.float32, [None, size, size, 3])
y_ = tf.placeholder(tf.float32, [None, 2])
keep_prob_5 = tf.placeholder(tf.float32)
keep_prob_75 = tf.placeholder(tf.float32)
def weightVariable(shape):
init = tf.random_normal(shape, stddev=0.01)
return tf.Variable(init)
def biasVariable(shape):
init = tf.random_normal(shape)
return tf.Variable(init)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')
def maxPool(x):
return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
def dropout(x, keep):
return tf.nn.dropout(x, keep)
def cnnLayer():
# 第一層
W1 = weightVariable([3,3,3,32]) # 卷積核大小(3,3), 輸入通道(3), 輸出通道(32)
b1 = biasVariable([32])
# 卷積
conv1 = tf.nn.relu(conv2d(x, W1) + b1)
# 池化
pool1 = maxPool(conv1)
# 減少過擬合,隨機讓某些權重不更新
drop1 = dropout(pool1, keep_prob_5)
# 第二層
W2 = weightVariable([3,3,32,64])
b2 = biasVariable([64])
conv2 = tf.nn.relu(conv2d(drop1, W2) + b2)
pool2 = maxPool(conv2)
drop2 = dropout(pool2, keep_prob_5)
# 第三層
W3 = weightVariable([3,3,64,64])
b3 = biasVariable([64])
conv3 = tf.nn.relu(conv2d(drop2, W3) + b3)
pool3 = maxPool(conv3)
drop3 = dropout(pool3, keep_prob_5)
# 全連接層
Wf = weightVariable([8*8*64, 512])
bf = biasVariable([512])
drop3_flat = tf.reshape(drop3, [-1, 8*8*64])
dense = tf.nn.relu(tf.matmul(drop3_flat, Wf) + bf)
dropf = dropout(dense, keep_prob_75)
# 輸出層
Wout = weightVariable([512,2])
bout = biasVariable([2])
#out = tf.matmul(dropf, Wout) + bout
out = tf.add(tf.matmul(dropf, Wout), bout)
return out
def cnnTrain():
out = cnnLayer()
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out, labels=y_))
train_step = tf.train.AdamOptimizer(0.01).minimize(cross_entropy)
# 比較標簽是否相等,再求的所有數的平均值,tf.cast(強制轉換類型)
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(out, 1), tf.argmax(y_, 1)), tf.float32))
# 將loss與accuracy保存以供tensorboard使用
tf.summary.scalar('loss', cross_entropy)
tf.summary.scalar('accuracy', accuracy)
merged_summary_op = tf.summary.merge_all()
# 數據保存器的初始化
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
summary_writer = tf.summary.FileWriter('./tmp', graph=tf.get_default_graph())
for n in range(10):
# 每次取128(batch_size)張圖片
for i in range(num_batch):
batch_x = train_x[i*batch_size : (i+1)*batch_size]
batch_y = train_y[i*batch_size : (i+1)*batch_size]
# 開始訓練數據,同時訓練三個變量,返回三個數據
_,loss,summary = sess.run([train_step, cross_entropy, merged_summary_op],
feed_dict={x:batch_x,y_:batch_y, keep_prob_5:0.5,keep_prob_75:0.75})
summary_writer.add_summary(summary, n*num_batch+i)
# 打印損失
print(n*num_batch+i, loss)
if (n*num_batch+i) % 100 == 0:
# 獲取測試數據的准確率
acc = accuracy.eval({x:test_x, y_:test_y, keep_prob_5:1.0, keep_prob_75:1.0})
print(n*num_batch+i, acc)
# 准確率大於0.98時保存並退出
if acc > 0.98 and n > 2:
saver.save(sess, './train_faces.model', global_step=n*num_batch+i)
sys.exit(0)
print('accuracy less 0.98, exited!')
cnnTrain()
is_my_face
最后就是識別了,,,運行這個會出現兩個窗口,一個是實時的拍攝窗口,一個是識別的窗口(會出現藍色的框,,,
然后如果識別出來是之前錄入的那個人的話,,cmd里會出現True的字樣,,否則是False,,,如果沒有識別出來有人臉在畫面里的話會卡住不動,,,
大概之前錄的時間是2-3分鍾左右的准確度就很高了,,
import tensorflow as tf
import cv2
import dlib
import numpy as np
import os
import random
import sys
import time
from sklearn.model_selection import train_test_split
my_faces_path = './my_faces'
other_faces_path = './other_faces'
size = 64
imgs = []
labs = []
def getPaddingSize(img):
h, w, _ = img.shape
top, bottom, left, right = (0,0,0,0)
longest = max(h, w)
if w < longest:
tmp = longest - w
# //表示整除符號
left = tmp // 2
right = tmp - left
elif h < longest:
tmp = longest - h
top = tmp // 2
bottom = tmp - top
else:
pass
return top, bottom, left, right
def readData(path , h=size, w=size):
for filename in os.listdir(path):
if filename.endswith('.jpg'):
filename = path + '/' + filename
img = cv2.imread(filename)
top,bottom,left,right = getPaddingSize(img)
# 將圖片放大, 擴充圖片邊緣部分
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=[0,0,0])
img = cv2.resize(img, (h, w))
imgs.append(img)
labs.append(path)
readData(my_faces_path)
readData(other_faces_path)
# 將圖片數據與標簽轉換成數組
imgs = np.array(imgs)
labs = np.array([[0,1] if lab == my_faces_path else [1,0] for lab in labs])
# 隨機划分測試集與訓練集
train_x,test_x,train_y,test_y = train_test_split(imgs, labs, test_size=0.05, random_state=random.randint(0,100))
# 參數:圖片數據的總數,圖片的高、寬、通道
train_x = train_x.reshape(train_x.shape[0], size, size, 3)
test_x = test_x.reshape(test_x.shape[0], size, size, 3)
# 將數據轉換成小於1的數
train_x = train_x.astype('float32')/255.0
test_x = test_x.astype('float32')/255.0
print('train size:%s, test size:%s' % (len(train_x), len(test_x)))
# 圖片塊,每次取128張圖片
batch_size = 128
num_batch = len(train_x) // 128
x = tf.placeholder(tf.float32, [None, size, size, 3])
y_ = tf.placeholder(tf.float32, [None, 2])
keep_prob_5 = tf.placeholder(tf.float32)
keep_prob_75 = tf.placeholder(tf.float32)
def weightVariable(shape):
init = tf.random_normal(shape, stddev=0.01)
return tf.Variable(init)
def biasVariable(shape):
init = tf.random_normal(shape)
return tf.Variable(init)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')
def maxPool(x):
return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
def dropout(x, keep):
return tf.nn.dropout(x, keep)
def cnnLayer():
# 第一層
W1 = weightVariable([3,3,3,32]) # 卷積核大小(3,3), 輸入通道(3), 輸出通道(32)
b1 = biasVariable([32])
# 卷積
conv1 = tf.nn.relu(conv2d(x, W1) + b1)
# 池化
pool1 = maxPool(conv1)
# 減少過擬合,隨機讓某些權重不更新
drop1 = dropout(pool1, keep_prob_5)
# 第二層
W2 = weightVariable([3,3,32,64])
b2 = biasVariable([64])
conv2 = tf.nn.relu(conv2d(drop1, W2) + b2)
pool2 = maxPool(conv2)
drop2 = dropout(pool2, keep_prob_5)
# 第三層
W3 = weightVariable([3,3,64,64])
b3 = biasVariable([64])
conv3 = tf.nn.relu(conv2d(drop2, W3) + b3)
pool3 = maxPool(conv3)
drop3 = dropout(pool3, keep_prob_5)
# 全連接層
Wf = weightVariable([8*16*32, 512])
bf = biasVariable([512])
drop3_flat = tf.reshape(drop3, [-1, 8*16*32])
dense = tf.nn.relu(tf.matmul(drop3_flat, Wf) + bf)
dropf = dropout(dense, keep_prob_75)
# 輸出層
Wout = weightVariable([512,2])
bout = biasVariable([2])
out = tf.add(tf.matmul(dropf, Wout), bout)
return out
output = cnnLayer()
predict = tf.argmax(output, 1)
saver = tf.train.Saver()
sess = tf.Session()
saver.restore(sess, tf.train.latest_checkpoint('.'))
def is_my_face(image):
res = sess.run(predict, feed_dict={x: [image/255.0], keep_prob_5:1.0, keep_prob_75: 1.0})
if res[0] == 1:
return True
else:
return False
#使用dlib自帶的frontal_face_detector作為我們的特征提取器
detector = dlib.get_frontal_face_detector()
cam = cv2.VideoCapture(0)
while True:
time.sleep(0.2)
_, img = cam.read()
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dets = detector(gray_image, 1)
if not len(dets):
#print('Can`t get face.')
cv2.imshow('img', img)
key = cv2.waitKey(30) & 0xff
if key == 27:
sys.exit(0)
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 = cv2.resize(face, (size,size))
print('Is this my face? %s' % is_my_face(face))
cv2.rectangle(img, (x2,x1),(y2,y1), (255,0,0),3)
cv2.imshow('image',img)
key = cv2.waitKey(30) & 0xff
if key == 27:
sys.exit(0)
sess.close()
感想
后面就沒了,,建議弄過一個遍之后,代碼還是自己再重寫一別吧,,這樣能理解里面的細節的內容,,,
裝了4、5遍多的環境感覺每一次都有收獲,,雖然每次都會遇到一些問題,,但是都也能靠自己來解決,,,hhh,,,
(end)