As a novice, this is a simple recognition model, is not difficult, the amount of code is not too much, the following one by one to explain, prepare the data set is not to say, vary.
A. Dataset acquired all paths
Os is generated using all of the data module contains a list of paths
def my_face():
path = os.listdir("./my_faces")
image_path = [os.path.join("./my_faces/",img) for img in path]
return image_path
def other_face():
path = os.listdir("./other_faces")
image_path = [os.path.join("./other_faces/",img) for img in path]
return image_path
image_path = my_face () .__ add __ (other_face ()) # The two merged into a list list
1
2
3
4
5
6
7
8
9
II. Construction label
Label structure is relatively simple, 1 represents himself, 0 for others.
label_my= [1 for i in my_face()]
label_other = [0 for i in other_face()]
label = label_my .__ add __ (label_other) # merge two list
1
2
3
III. Configuration dataset
Using tf.data.Dataset.from_tensor_slices () construct data sets,
def preprocess(x,y):
x = tf.io.read_file (x) # read data
x = tf.image.decode_jpeg (x, channels = 3) # decoded into data, jpg
x = tf.cast (x, tf.float32) / 255.0 # normalized
y = tf.convert_to_tensor(y) #转成tensor
return x,y
data = tf.data.Dataset.from_tensor_slices((image_path,label))
data_loader = data.repeat().shuffle(5000).map(preprocess).batch(128).prefetch(1)
1
2
3
4
5
6
7
8
9
IV. Structural model
class CNN_WORK(Model):
def __init__(self):
super(CNN_WORK,self).__init__()
self.conv1 = layers.Conv2D(32,kernel_size=5,activation=tf.nn.relu)
self.maxpool1 = layers.MaxPool2D(2,strides=2)
self.conv2 = layers.Conv2D(64,kernel_size=3,activation=tf.nn.relu)
self.maxpool2 = layers.MaxPool2D(2,strides=2)
self.flatten = layers.Flatten()
self.fc1 = layers.Dense(1024)
self.dropout = layers.Dropout(rate=0.5)
self.out = layers.Dense(2)
def call(self,x,is_training=False):
x = self.conv1(x)
x = self.maxpool1(x)
x = self.conv2(x)
x = self.maxpool2(x)
x = self.flatten(x)
x = self.fc1(x)
x = self.dropout(x,training=is_training)
x = self.out(x)
if not is_training:
x = tf.nn.softmax(x)
return x
model = CNN_WORK()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
V. loss function defined accuracy function optimization function
def cross_entropy_loss(x,y):
y = tf.cast(y,tf.int64)
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y,logits=x)
return tf.reduce_mean(loss)
def accuracy(y_pred,y_true):
correct_pred = tf.equal(tf.argmax(y_pred,1),tf.cast(y_true,tf.int64))
return tf.reduce_mean(tf.cast(correct_pred,tf.float32),axis=-1)
optimizer = tf.optimizers.SGD(0.002)
1
2
3
4
5
6
7
8
9
VI. Started running our model
def run_optimizer(x,y):
with tf.GradientTape() as g:
pred = model(x,is_training=True)
loss = cross_entropy_loss(pred,y)
training_variabel = model.trainable_variables
gradient = g.gradient(loss,training_variabel)
optimizer.apply_gradients(zip(gradient,training_variabel))
model.save_weights("face_weight") #保存模型
1
2
3
4
5
6
7
8
The last run of accuracy can still be pricey.
Seven .openCV debut
Finally OpenCV face detection module, the detected face into our training better models to predict identified according to the results predicted.
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('C:\\Users\Wuhuipeng\AppData\Local\Programs\Python\Python36\Lib\site-packages\cv2\data/haarcascade_frontalface_alt.xml')
while True:
ret,frame = cap.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5,minSize=(5,5))
for (x,y,z,t) in faces:
img = frame[x:x+z,y:y+t]
try:
img = cv2.resize(img,(64,64))
img = tf.cast(img,tf.float32) / 255.0
img = tf.reshape(img,[-1,64,64,3])
pred = model(img)
pred = tf.argmax(pred,axis=1).numpy()
except:
pass
if(pred[0]==1):
cv2.putText(frame,"wuhuipeng",(x-10,y-10),cv2.FONT_HERSHEY_SIMPLEX,1.2,(255,255,0),2)
cv2.rectangle(frame,(x,y),(x+z,y+t),(0,255,0),2)
cv2.imshow('find faces',frame)
if cv2.waitKey(1)&0xff ==ord('q'):
break Zhengzhou Men's Hospital: http: //www.ytsgnk.com/ see Zhengzhou Men's Hospital Which is better: http: //www.ytsgnk.com/ Zhengzhou men and hospital ranking: http: //www.ytsgnk.com/
cap.release()
cv2.destroyAllWindows()
————————————————
Disclaimer: This article is CSDN bloggers 'struggle Xiaopeng' original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/weixin_44678052/article/details/102286945