├── LICENSE ├── README.md ├── get_Inception_model.py ├── gui.py ├── image-20200401132142888.png ├── image-20200401132154818.png ├── image-20200401215811993.png ├── nodelookup.py └── tensorflow_predictor.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Nspyia 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 1.项目简介 2 | ![image-20200401215811993](image-20200401215811993.png) 3 | 4 | **主要功能**:利用tinker封装**InceptionV3**[[论文](https://arxiv.org/abs/1512.00567)]MOD进行图像分类的一个小Demo 5 | 6 | **环境**:**anaconda+Python3+tensorflow** 7 | 8 | **IDE**:**pycharm + jupyter notebook** 9 | 10 | 11 | 12 | 13 | 14 | # 2.代码框架 15 | 16 | 需要的库模块: 17 | 18 | * ```python 19 | os 20 | tarfile 21 | requests 22 | tensorflow 23 | numpy 24 | translate 25 | PIL 26 | ``` 27 | 28 | 29 | 30 | 一共四个代码文件: 31 | 32 | * get_Inception_model.py 33 | 34 | 方法模块,下载模型将模型保存到本地 35 | 36 | ```python 37 | def download_inception_model(): #下载模型将模型保存到本地 38 | '......' 39 | 40 | ``` 41 | 42 | * nodelookup.py 43 | 44 | 类文件,主要功能将官方标签解码成可读文本 45 | 46 | ```python 47 | class NodeLookup(object): 48 | def __init__(self): 49 | self.node_lookup # 字典,id to string 50 | '......' 51 | 52 | @staticmethod 53 | def _load(labels_path, uids_path): # 输入:node_id, 输出:id to string字典 54 | '......' 55 | return dict 56 | 57 | def id_to_string(self, node_id): # 输入:node_id, 输出:可读字符串 58 | '......' 59 | return str 60 | ``` 61 | 62 | * tensorflow_predictor.py 63 | 64 | 类文件,主要功能实现图像预测 65 | 66 | ```python 67 | class TensorflowPredictor(): 68 | def __init__(self): # 加载模型,新建session, 69 | '......' 70 | 71 | def predict_image(self, image_path): # 72 | '......' 73 | return str 74 | 75 | ``` 76 | 77 | 78 | 79 | * gui.py 80 | 81 | 界面代码,面向用户 82 | 83 | ```python 84 | btn_sel # 选择图片按钮 85 | img_label # 这是是显示预测图片的全局变量 86 | res_label # 这是是显示预测文字的全局变量 87 | 88 | def translator_prediction_result(pre_res):# 翻译模块 输入:英文字符串,输出:格式化中文字符串 89 | '......' 90 | return res 91 | 92 | def selector_image(): # 选择图片按钮点击发生的事件 93 | '......' 94 | 95 | root.mainloop() # 进入消息循环 96 | ``` 97 | 98 | 99 | 100 | 101 | 102 | # 3.实现细节 103 | 104 | 105 | 106 | ## 3.1.下载模型 107 | 108 | 109 | 110 | ### 3.1.1.实现功能 111 | 112 | 下载模型将模型保存到本地 113 | 114 | 115 | 116 | ### 3.1.2.Inception文件简介 117 | 118 | [Inception_v3模型源码下载](https://link.jianshu.com/?t=https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/slim/python/slim/nets/inception_v3.py) 119 | 120 | Inception为Google开源的CNN模型,至今已经公开四个版本,每一个版本都是基于大型图像数据库ImageNet中的数据训练而成。因此我们可以直接利用Google的Inception模型来实现图像分类。本项目主要以Inception_v3模型为基础。分类一张图像可以在几秒内完成。 121 | 122 | - InceptionV3网络图 123 | 124 | ![img](https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1585743446075&di=a2313edc991f7ad4b09486b33b6c4777&imgtype=0&src=http%3A%2F%2Fseo-1255598498.file.myqcloud.com%2Ffull%2Faa16cc25586fab61d8b5602ca50378a8c435d7b4.jpg) 125 | 126 | 127 | 128 | ### 3.1.3.流程图 129 | 130 | ```flow 131 | #operation 132 | st=>start: 开始 133 | end=>end: 结束 134 | cd1=>condition: 不存在"inception_model"文件夹? 135 | op1=>operation: 下载模型压缩包inception-2015-12-05.tgz 136 | op2=>operation: 创建"inception_model"文件夹 137 | op3=>operation: 解压inception-2015-12-05.tgz 138 | io1=>inputoutput: 打印"Downloading file" 139 | done=>inputoutput: 打印"Done." 140 | str=>inputoutput: 预测结果字符串 141 | 142 | cd1(yes)->op2->op1->op3->done->end 143 | cd1(no)->end 144 | ``` 145 | 146 | 147 | 148 | 149 | 150 | ### 3.1.4.代码 151 | 152 | ```python 153 | # get_Inception_model.py 154 | 155 | import tarfile 156 | import requests 157 | 158 | 159 | def download_inception_model(): 160 | # inception_v3模型下载 161 | inception_pre_mod_url = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz' 162 | # 模型存放地址 163 | inception_pre_mod_dir = "inception_model" 164 | if not os.path.exists(inception_pre_mod_dir): 165 | os.makedirs(inception_pre_mod_dir) 166 | # 获取文件名,以及文件路径 167 | filename = inception_pre_mod_url.split('/')[-1] 168 | filepath = os.path.join(inception_pre_mod_dir, filename) 169 | # 下载模型 170 | if not os.path.exists(filepath): 171 | print('Downloading: ', filename) 172 | r = requests.get(inception_pre_mod_url, stream=True) 173 | with open(filepath, 'wb') as f: 174 | for chunk in r.iter_content(chunk_size=1024): 175 | if chunk: f.write(chunk) 176 | print("Done: ", filename) 177 | # 解压文件 178 | tarfile.open(filepath, 'r:gz').extractall(inception_pre_mod_dir) 179 | ``` 180 | 181 | 182 | 183 | 184 | 185 | ## 3.2.标签解码 186 | 187 | 188 | 189 | ### 3.2.1.实现功能 190 | 191 | 将标签编码和标签内容一一对应(解码) 192 | 193 | 194 | 195 | ### 3.2.2.文件 196 | 197 | 官方下载的文件夹下有两个文件 198 | 199 | - imagenet_synset_to_human_label_map.txt 200 | - 201 | 202 | ![image-20200401132142888](image-20200401132142888.png) 203 | 204 | * imagenet_2012_challenge_label_map_proto.pbtx 205 | 206 | ![image-20200401132154818](image-20200401132154818.png) 207 | 208 | 209 | 210 | target_class对应着一个class_string,这里我们要做的任务就是将traget_class与human_string一一对应 211 | 212 | 213 | 214 | ### 3.2.3.代码 215 | 216 | ```python 217 | # nodelookup.py 218 | 219 | import tensorflow.compat.v1 as tf 220 | tf.disable_v2_behavior 221 | 222 | 223 | class NodeLookup(object): 224 | def __init__(self): 225 | labels_path = 'inception_model/imagenet_2012_challenge_label_map_proto.pbtxt' 226 | uids_path = 'inception_model/imagenet_synset_to_human_label_map.txt' 227 | self.node_lookup = self.load(labels_path, uids_path) 228 | 229 | @staticmethod 230 | def _load(labels_path, uids_path): 231 | uid_to_human = {} 232 | for line in tf.gfile.GFile(uids_path).readlines(): 233 | items = line.strip('\n').split('\t') 234 | uid_to_human[items[0]] = items[1] 235 | node_id_to_uid = {} 236 | for line in tf.gfile.GFile(labels_path).readlines(): 237 | if line.startswith(' target_class:'): 238 | target_class = int(line.split(': ')[1]) 239 | if line.startswith(' target_class_string:'): 240 | target_class_string = line.split(': ')[1] 241 | node_id_to_uid[target_class] = target_class_string[1:-2] 242 | node_id_to_name = {} 243 | for key, val in node_id_to_uid.items(): 244 | name = uid_to_human[val] 245 | node_id_to_name[key] = name 246 | return node_id_to_name 247 | 248 | def id_to_string(self, node_id): 249 | if node_id not in self.node_lookup: 250 | return '' 251 | return self.node_lookup[node_id] 252 | ``` 253 | 254 | 255 | 256 | 257 | 258 | ## 3.3.运行模型 259 | 260 | 261 | 262 | ### 3.3.1.流程图 263 | 264 | ```flow 265 | #operation 266 | st=>start: 开始 267 | e=>结束 268 | sub1=>subroutine: My Subroutine 269 | image=>inputoutput: 图像文件 270 | model=>subroutine: 模型预测函数 271 | str=>inputoutput: 预测结果字符串 272 | 273 | image->model->str 274 | ``` 275 | 276 | 277 | 278 | ### 3.3.2.代码 279 | 280 | ```python 281 | import tensorflow.compat.v1 as tf 282 | 283 | tf.disable_v2_behavior 284 | import numpy as np 285 | import nodelookup 286 | 287 | 288 | class TensorflowPredictor(): 289 | def __init__(self): 290 | self.sess = tf.Session() 291 | with tf.gfile.FastGFile('./inception_model/classify_image_graph_def.pb', 'rb') as f: 292 | graph_def = tf.GraphDef() # 定义一个计算图 293 | graph_def.ParseFromString(f.read()) # 294 | tf.import_graph_def(graph_def, name='') 295 | self.softmax_tensor = self.sess.graph.get_tensor_by_name('softmax:0') 296 | 297 | def predict_image(self, image_path): 298 | # 载入图片 299 | image_data = tf.gfile.FastGFile(image_path, 'rb').read() 300 | predictions = self.sess.run(self.softmax_tensor, {'DecodeJpeg/contents:0': image_data}) # 图片格式是jpg格式 301 | predictions = np.squeeze(predictions) # 把结果转为1维 302 | # 打印图片路径及名称 303 | res_str = '' 304 | res_str += '图片路径: ' + image_path + '\n' 305 | # 排序 306 | top_k = predictions.argsort()[-5:][::-1] 307 | node_lookup = nodelookup.NodeLookup() 308 | for node_id in top_k: 309 | # 获取分类名称 310 | name_str = node_lookup.id_to_string(node_id) 311 | # 获取该分类的置信度 312 | score = predictions[node_id] * 100 313 | res_str += '(%.2f' % (score) + '%), ' + name_str + '\n' 314 | return res_str 315 | ``` 316 | 317 | 318 | 319 | ## 3.4.GUI 320 | 321 | 322 | 323 | ### 3.4.1.运行图 324 | 325 | ![image-20200401215811993](image-20200401215811993.png) 326 | 327 | ### 3.4.2.代码 328 | 329 | ```python 330 | import os 331 | import tkinter 332 | from tkinter import * 333 | from tkinter import filedialog 334 | from PIL import ImageTk 335 | from translate import Translator 336 | 337 | import get_Inception_model 338 | from tensorflow_predictor import TensorflowPredictor 339 | 340 | root = tkinter.Tk() # 生成root主窗口 341 | root.title("图像分类") # 设置窗体标题 342 | root.geometry("800x800") # 设置窗体大小 343 | 344 | if not os.path.exists('./inception_model/classify_image_graph_def.pb'): # 如果没下载model,则下载model 345 | get_Inception_model.download_inception_model() # 下载model 346 | 347 | translator = Translator(to_lang="chinese") # 新建Translator对象 348 | 349 | def translator_prediction_result(pre_res): # 翻译模块 350 | res = pre_res.split("\n")[0] + '\n' 351 | for line in pre_res.split("\n")[1:-1]: 352 | s = translator.translate(line.split(',')[1]) 353 | res += line + " (机翻结果: " + s + ")\n" 354 | return res # 返回翻译结果 355 | 356 | 357 | img_label = Label(root, width='800', height='533') # 这是是显示预测图片的全局变量 358 | res_label = Label(root) # 这是是显示预测文字的全局变量 359 | pdt = TensorflowPredictor() # 新建预测类(自己写的) 360 | 361 | 362 | def selector_image(): # 选择图片按钮点击发生的事件 363 | img_path = filedialog.askopenfilename(initialdir='./images') # 弹窗选择图像文件返回图像地址 364 | pre_res = pdt.predict_image(image_path=img_path) # 利用地址调用预测函数返回结果字符串 365 | pre_res = translator_prediction_result(pre_res) # 机器翻译结果字符串 366 | photo = ImageTk.PhotoImage(file=img_path) 367 | img_label.config(imag=photo) # 更新图片 368 | img_label.pack() 369 | res_label.config(text=pre_res, justify=LEFT) # 更新文字 370 | res_label.pack() 371 | root.mainloop() # 进入消息循环 372 | return 373 | 374 | 375 | btn_sel = tkinter.Button(root, text='选择图片', command=selector_image) # 选择图片按钮 376 | btn_sel.pack() 377 | 378 | root.mainloop() # 进入消息循环(必需组件) 379 | ``` 380 | 381 | -------------------------------------------------------------------------------- /get_Inception_model.py: -------------------------------------------------------------------------------- 1 | import os 2 | import tarfile 3 | import requests 4 | 5 | 6 | def download_inception_model(): 7 | # inception_v3模型下载 8 | inception_pre_mod_url = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz' 9 | # 模型存放地址 10 | inception_pre_mod_dir = "inception_model" 11 | if not os.path.exists(inception_pre_mod_dir): 12 | os.makedirs(inception_pre_mod_dir) 13 | # 获取文件名,以及文件路径 14 | filename = inception_pre_mod_url.split('/')[-1] 15 | filepath = os.path.join(inception_pre_mod_dir, filename) 16 | # 下载模型 17 | if not os.path.exists(filepath): 18 | print('Downloading: ', filename) 19 | r = requests.get(inception_pre_mod_url, stream=True) 20 | with open(filepath, 'wb') as f: 21 | for chunk in r.iter_content(chunk_size=1024): 22 | if chunk: f.write(chunk) 23 | print("Done: ", filename) 24 | # 解压文件 25 | tarfile.open(filepath, 'r:gz').extractall(inception_pre_mod_dir) 26 | -------------------------------------------------------------------------------- /gui.py: -------------------------------------------------------------------------------- 1 | import os 2 | import tkinter 3 | from tkinter import * 4 | from tkinter import filedialog 5 | from PIL import ImageTk 6 | from translate import Translator 7 | 8 | import get_Inception_model 9 | from tensorflow_predictor import TensorflowPredictor 10 | 11 | root = tkinter.Tk() # 生成root主窗口 12 | root.title("图像分类") # 设置窗体标题 13 | root.geometry("820x820") # 设置窗体大小 14 | 15 | if not os.path.exists('./inception_model/classify_image_graph_def.pb'): # 如果没下载model,则下载model 16 | get_Inception_model.download_inception_model() # 下载model 17 | 18 | translator = Translator(to_lang="chinese") # 新建Translator对象 19 | 20 | 21 | def translator_prediction_result(pre_res): # 翻译模块 22 | res = pre_res.split("\n")[0] + '\n' 23 | for line in pre_res.split("\n")[1:-1]: 24 | s = translator.translate(line.split(',')[1]) 25 | res += line + " (机翻: " + s + ")\n" 26 | return res # 返回翻译结果 27 | 28 | 29 | img_label = Label(root, width='800', height='533') # 这是是显示预测图片的全局变量 30 | res_label = Label(root) # 这是是显示预测文字的全局变量 31 | pdt = TensorflowPredictor() # 新建预测类(自己写的) 32 | 33 | 34 | def selector_image(): # 选择图片按钮点击发生的事件 35 | img_path = filedialog.askopenfilename(initialdir='./images') # 弹窗选择图像文件返回图像地址 36 | pre_res = pdt.predict_image(image_path=img_path) # 利用地址调用预测函数返回结果字符串 37 | pre_res = translator_prediction_result(pre_res) # 机器翻译结果字符串 38 | photo = ImageTk.PhotoImage(file=img_path) 39 | img_label.config(imag=photo) # 更新图片 40 | img_label.pack() 41 | res_label.config(text=pre_res, justify=LEFT, font=("微软雅黑", 13)) # 更新文字 42 | res_label.pack() 43 | root.mainloop() # 进入消息循环 44 | return 45 | 46 | 47 | btn_sel = tkinter.Button(root, text='选择图片', command=selector_image) # 选择图片按钮 48 | btn_sel.pack() 49 | 50 | root.mainloop() # 进入消息循环(必需组件) 51 | -------------------------------------------------------------------------------- /image-20200401132142888.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nspyia/Image-recognizer/597571738f97b0140ef2e6079c0c2dd26c2dad20/image-20200401132142888.png -------------------------------------------------------------------------------- /image-20200401132154818.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nspyia/Image-recognizer/597571738f97b0140ef2e6079c0c2dd26c2dad20/image-20200401132154818.png -------------------------------------------------------------------------------- /image-20200401215811993.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nspyia/Image-recognizer/597571738f97b0140ef2e6079c0c2dd26c2dad20/image-20200401215811993.png -------------------------------------------------------------------------------- /nodelookup.py: -------------------------------------------------------------------------------- 1 | import tensorflow.compat.v1 as tf # 兼容性 2 | 3 | tf.disable_v2_behavior 4 | 5 | 6 | class NodeLookup(object): 7 | def __init__(self): 8 | labels_path = 'inception_model/imagenet_2012_challenge_label_map_proto.pbtxt' 9 | uids_path = 'inception_model/imagenet_synset_to_human_label_map.txt' 10 | self._node_lookup = self._load(labels_path, uids_path) 11 | 12 | @staticmethod 13 | def _load(labels_path, uids_path): 14 | uid_to_human = {} 15 | for line in tf.gfile.GFile(uids_path).readlines(): 16 | items = line.strip('\n').split('\t') 17 | uid_to_human[items[0]] = items[1] 18 | node_id_to_uid = {} 19 | for line in tf.gfile.GFile(labels_path).readlines(): 20 | if line.startswith(' target_class:'): 21 | target_class = int(line.split(': ')[1]) 22 | if line.startswith(' target_class_string:'): 23 | target_class_string = line.split(': ')[1] 24 | node_id_to_uid[target_class] = target_class_string[1:-2] 25 | node_id_to_name = {} 26 | for key, val in node_id_to_uid.items(): 27 | name = uid_to_human[val] 28 | node_id_to_name[key] = name 29 | return node_id_to_name 30 | 31 | def id_to_string(self, node_id): 32 | if node_id not in self._node_lookup: 33 | return '' 34 | return self._node_lookup[node_id] 35 | -------------------------------------------------------------------------------- /tensorflow_predictor.py: -------------------------------------------------------------------------------- 1 | import tensorflow.compat.v1 as tf 2 | 3 | tf.disable_v2_behavior 4 | import numpy as np 5 | import nodelookup 6 | 7 | 8 | class TensorflowPredictor(): 9 | def __init__(self): 10 | self.sess = tf.Session() 11 | with tf.gfile.FastGFile('./inception_model/classify_image_graph_def.pb', 'rb') as f: 12 | graph_def = tf.GraphDef() # 定义一个计算图 13 | graph_def.ParseFromString(f.read()) # 14 | tf.import_graph_def(graph_def, name='') 15 | self.softmax_tensor = self.sess.graph.get_tensor_by_name('softmax:0') 16 | 17 | def predict_image(self, image_path): 18 | # 载入图片 19 | image_data = tf.gfile.FastGFile(image_path, 'rb').read() 20 | predictions = self.sess.run(self.softmax_tensor, {'DecodeJpeg/contents:0': image_data}) # 图片格式是jpg格式 21 | predictions = np.squeeze(predictions) # 把结果转为1维 22 | # 打印图片路径及名称 23 | res_str = '' 24 | res_str += '图片路径: ...' + image_path[int(len(image_path)/2):] + '\n' 25 | # 排序 26 | top_k = predictions.argsort()[-5:][::-1] 27 | node_lookup = nodelookup.NodeLookup() 28 | for node_id in top_k: 29 | # 获取分类名称 30 | name_str = node_lookup.id_to_string(node_id) 31 | # 获取该分类的置信度 32 | score = predictions[node_id] * 100 33 | res_str += '(%.2f' % (score) + '%), ' + name_str + '\n' 34 | return res_str 35 | --------------------------------------------------------------------------------