基於微信的拍照花卉識別

      這個例子實現了可以完成通過拍照發送到指定的微信賬號,自動回覆花的類型。當然需要預先安裝Python運行環境以及TensorFlow相關工具。具體通過搜索引擎查一下,很多博客文章可以參考。我的安裝環境如下:

C:\Users\Administrator.WIN7-1609091712>python
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.__version__
'1.4.0'
>>> tf.__path__
['D:\\Program Files\\Python\\Python36\\lib\\site-packages\\tensorflow']
>>>

實現步驟:

1.下載花卉樣本數據集

首先下載flowers樣本數據

鏈接:https://pan.baidu.com/s/1Sh3f09K27RV64q0yxC-GHw 密碼:zx7x

保存到D:\study\tensorflow\flower_photos

2.通過訓練程序生成識別模型並保存

新建python文件flower.py,內容如下:

from skimage import io,transform
import glob
import os
import tensorflow as tf
import numpy as np
import time

#數據集地址
path='D:/study/tensorflow/flower_photos/'
#模型保存地址
model_path='D:/study/tensorflow/flower/model.ckpt'

#將所有的圖片resize成100*100
w=100
h=100
c=3


#讀取圖片
def read_img(path):
    cate=[path+x for x in os.listdir(path) if os.path.isdir(path+x)]
    imgs=[]
    labels=[]
    for idx,folder in enumerate(cate):
        for im in glob.glob(folder+'/*.jpg'):
            print('reading the images:%s'%(im))
            img=io.imread(im)
            img=transform.resize(img,(w,h))
            imgs.append(img)
            labels.append(idx)
    return np.asarray(imgs,np.float32),np.asarray(labels,np.int32)
data,label=read_img(path)


#打亂順序
num_example=data.shape[0]
arr=np.arange(num_example)
np.random.shuffle(arr)
data=data[arr]
label=label[arr]


#將所有數據分爲訓練集和驗證集
ratio=0.8
s=np.int(num_example*ratio)
x_train=data[:s]
y_train=label[:s]
x_val=data[s:]
y_val=label[s:]

#-----------------構建網絡----------------------
#佔位符
x=tf.placeholder(tf.float32,shape=[None,w,h,c],name='x')
y_=tf.placeholder(tf.int32,shape=[None,],name='y_')

def inference(input_tensor, train, regularizer):
    with tf.variable_scope('layer1-conv1'):
        conv1_weights = tf.get_variable("weight",[5,5,3,32],initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv1_biases = tf.get_variable("bias", [32], initializer=tf.constant_initializer(0.0))
        conv1 = tf.nn.conv2d(input_tensor, conv1_weights, strides=[1, 1, 1, 1], padding='SAME')
        relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))

    with tf.name_scope("layer2-pool1"):
        pool1 = tf.nn.max_pool(relu1, ksize = [1,2,2,1],strides=[1,2,2,1],padding="VALID")

    with tf.variable_scope("layer3-conv2"):
        conv2_weights = tf.get_variable("weight",[5,5,32,64],initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv2_biases = tf.get_variable("bias", [64], initializer=tf.constant_initializer(0.0))
        conv2 = tf.nn.conv2d(pool1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME')
        relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))

    with tf.name_scope("layer4-pool2"):
        pool2 = tf.nn.max_pool(relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')

    with tf.variable_scope("layer5-conv3"):
        conv3_weights = tf.get_variable("weight",[3,3,64,128],initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv3_biases = tf.get_variable("bias", [128], initializer=tf.constant_initializer(0.0))
        conv3 = tf.nn.conv2d(pool2, conv3_weights, strides=[1, 1, 1, 1], padding='SAME')
        relu3 = tf.nn.relu(tf.nn.bias_add(conv3, conv3_biases))

    with tf.name_scope("layer6-pool3"):
        pool3 = tf.nn.max_pool(relu3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')

    with tf.variable_scope("layer7-conv4"):
        conv4_weights = tf.get_variable("weight",[3,3,128,128],initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv4_biases = tf.get_variable("bias", [128], initializer=tf.constant_initializer(0.0))
        conv4 = tf.nn.conv2d(pool3, conv4_weights, strides=[1, 1, 1, 1], padding='SAME')
        relu4 = tf.nn.relu(tf.nn.bias_add(conv4, conv4_biases))

    with tf.name_scope("layer8-pool4"):
        pool4 = tf.nn.max_pool(relu4, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
        nodes = 6*6*128
        reshaped = tf.reshape(pool4,[-1,nodes])

    with tf.variable_scope('layer9-fc1'):
        fc1_weights = tf.get_variable("weight", [nodes, 1024],
                                      initializer=tf.truncated_normal_initializer(stddev=0.1))
        if regularizer != None: tf.add_to_collection('losses', regularizer(fc1_weights))
        fc1_biases = tf.get_variable("bias", [1024], initializer=tf.constant_initializer(0.1))

        fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_weights) + fc1_biases)
        if train: fc1 = tf.nn.dropout(fc1, 0.5)

    with tf.variable_scope('layer10-fc2'):
        fc2_weights = tf.get_variable("weight", [1024, 512],
                                      initializer=tf.truncated_normal_initializer(stddev=0.1))
        if regularizer != None: tf.add_to_collection('losses', regularizer(fc2_weights))
        fc2_biases = tf.get_variable("bias", [512], initializer=tf.constant_initializer(0.1))

        fc2 = tf.nn.relu(tf.matmul(fc1, fc2_weights) + fc2_biases)
        if train: fc2 = tf.nn.dropout(fc2, 0.5)

    with tf.variable_scope('layer11-fc3'):
        fc3_weights = tf.get_variable("weight", [512, 5],
                                      initializer=tf.truncated_normal_initializer(stddev=0.1))
        if regularizer != None: tf.add_to_collection('losses', regularizer(fc3_weights))
        fc3_biases = tf.get_variable("bias", [5], initializer=tf.constant_initializer(0.1))
        logit = tf.matmul(fc2, fc3_weights) + fc3_biases

    return logit

#---------------------------網絡結束---------------------------
regularizer = tf.contrib.layers.l2_regularizer(0.0001)
logits = inference(x,False,regularizer)

#(小處理)將logits乘以1賦值給logits_eval,定義name,方便在後續調用模型時通過tensor名字調用輸出tensor
b = tf.constant(value=1,dtype=tf.float32)
logits_eval = tf.multiply(logits,b,name='logits_eval') 

loss=tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y_)
train_op=tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
correct_prediction = tf.equal(tf.cast(tf.argmax(logits,1),tf.int32), y_)    
acc= tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


#定義一個函數,按批次取數據
def minibatches(inputs=None, targets=None, batch_size=None, shuffle=False):
    assert len(inputs) == len(targets)
    if shuffle:
        indices = np.arange(len(inputs))
        np.random.shuffle(indices)
    for start_idx in range(0, len(inputs) - batch_size + 1, batch_size):
        if shuffle:
            excerpt = indices[start_idx:start_idx + batch_size]
        else:
            excerpt = slice(start_idx, start_idx + batch_size)
        yield inputs[excerpt], targets[excerpt]

n_epoch=10                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
batch_size=64
saver=tf.train.Saver()
sess=tf.Session()  
sess.run(tf.global_variables_initializer())
for epoch in range(n_epoch):
    start_time = time.time()

    #training
    train_loss, train_acc, n_batch = 0, 0, 0
    for x_train_a, y_train_a in minibatches(x_train, y_train, batch_size, shuffle=True):
        _,err,ac=sess.run([train_op,loss,acc], feed_dict={x: x_train_a, y_: y_train_a})
        train_loss += err; train_acc += ac; n_batch += 1
    print("   train loss: %f" % (np.sum(train_loss)/ n_batch))
    print("   train acc: %f" % (np.sum(train_acc)/ n_batch))

    #validation
    val_loss, val_acc, n_batch = 0, 0, 0
    for x_val_a, y_val_a in minibatches(x_val, y_val, batch_size, shuffle=False):
        err, ac = sess.run([loss,acc], feed_dict={x: x_val_a, y_: y_val_a})
        val_loss += err; val_acc += ac; n_batch += 1
    print("   validation loss: %f" % (np.sum(val_loss)/ n_batch))
    print("   validation acc: %f" % (np.sum(val_acc)/ n_batch))
saver.save(sess,model_path)
sess.close()

運行python flower.py

D:\study\tensorflow>python flower.py
reading the images:D:/study/tensorflow/flower_photos/daisy\100080576_f52e8ee070_
n.jpg
D:\Program Files\Python\Python36\lib\site-packages\skimage\transform\_warps.py:8
4: UserWarning: The default mode, 'constant', will be changed to 'reflect' in sk
image 0.15.
  warn("The default mode, 'constant', will be changed to 'reflect' in "
reading the images:D:/study/tensorflow/flower_photos/daisy\10140303196_b88d3d6ce
c.jpg
reading the images:D:/study/tensorflow/flower_photos/daisy\10172379554_b296050f8
2_n.jpg

運行完成後,在flower目錄下生成訓練後的模型。包含四個文件。

D:\study\tensorflow\flower>tree /f
文件夾 PATH 列表
卷序列號爲 FEAC-6D64
D:.
    checkpoint
    model.ckpt.data-00000-of-00001
    model.ckpt.index
    model.ckpt.meta

3.安裝Itchat工具包和scikit-image工具包

pip install itchat

pip install scikit-image

安裝出現問的話,可以下載藍燈代理。

itchat是一個開源的微信個人號接口,使用python調用微信,簡化操作。

scikit-image (a.k.a. skimage) 是一個圖像處理和計算機視覺的算法集合

具體查閱ItChat介紹以及scikit-image官網

4.訓練模型調用代碼文件imagerec.py

import sys, getopt

from skimage import io, transform
import tensorflow as tf
import numpy as np


flower_dict = {
    0: '菊花',
    1: '蒲公英',
    2: '玫瑰',
    3: '向日葵',
    4: '鬱金香'
}

def main(argv):
    inputfile = ""
    try:
        opts, args = getopt.getopt(argv, "hi:o:", ["infile=", "outfile="])
    except getopt.GetoptError:
        print('Error: test_arg.py -i <inputfile> -o <outputfile>')
        print('   or: test_arg.py --infile=<inputfile> --outfile=<outputfile>')
        sys.exit(2)

    for opt, arg in opts:
        if opt == "-h":
            print('test_arg.py -i <inputfile> -o <outputfile>')
            print('or: test_arg.py --infile=<inputfile> --outfile=<outputfile>')

            sys.exit()
        elif opt in ("-i", "--infile"):
            inputfile = arg
    print(inputfile)
    print(recgnize(inputfile))


def read_one_image(path):
        img = io.imread(path)
        img = transform.resize(img, (100, 100))
        return np.asarray(img)



def recgnize(filename):
            with tf.Session() as sess:
                data = []
                data.append(read_one_image(filename))

                saver = tf.train.import_meta_graph('D:/study/tensorflow/flower/model.ckpt.meta')
                saver.restore(sess, tf.train.latest_checkpoint('D:/study/tensorflow/flower/'))

                graph = tf.get_default_graph()
                x = graph.get_tensor_by_name("x:0")
                feed_dict = {
                x: data
                }
                logits = graph.get_tensor_by_name("logits_eval:0")
                classification_result = sess.run(logits, feed_dict)# 打印出預測矩陣
                #print(classification_result)# 打印出預測矩陣每一行最大值的索引
                #print(tf.argmax(classification_result, 1).eval())# 根據索引通過字典對應花的分類
                output = []
                output = tf.argmax(classification_result, 1).eval()
                for i in range(len(output)):
                    #print("第", i + 1, "朵花預測:" + flower_dict[output[i]])
                    return flower_dict[output[i]]

if __name__ == "__main__":
    main(sys.argv[1: ])

5.微信集成wx.py

import itchat, time
from itchat.content import *
from imagerec import recgnize

@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
def text_reply(msg):
    msg.user.send('%s: %s' % ('回覆', msg["Text"]))

@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
    msg.download(msg.fileName)
    type1 = recgnize(msg.fileName)
    print(type1)
    typeSymbol = {
        PICTURE: 'img',
        VIDEO: 'vid', }.get(msg.type, 'fil')
		
    msg.user.send(type1)
    #return '@%s@%s' % (typeSymbol, msg.fileName)

@itchat.msg_register(FRIENDS)
def add_friend(msg):
    msg.user.verify()
    msg.user.send('Nice to meet you!')



itchat.auto_login()
itchat.run(True)

運行python wx.py


微信掃描生成的QR.PNG文件,登錄確認,這樣你的微信可以進行花卉識別了。

找個好友發個玫瑰花的圖片給你,效果如下:


可以動手試試,有啥問題歡迎溝通!





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章