Win10 Tensorflow object_detection目標(biāo)檢測框架使用

首先我們說下前期準(zhǔn)備工作

1.肯定要有能運行Tensorflow環(huán)境win10環(huán)境搭建

2.object_detection框架的安裝??框架地址

3.protobuf?編譯器(我用3.4版本,3.4+的發(fā)現(xiàn)無法找到文件不知道為什么)

大概就這么多我們一步一步開始。

第一步就不多說了,沒搭建好的先自己搭建好。

我們先去下載框架代碼下來·········

下載完成后解壓出來會有一個models-master的文件,文件應(yīng)該比較大在2G左右。


文件結(jié)構(gòu)

里面包含很多最新的網(wǎng)絡(luò)模型和很多demo有興趣的童鞋可以看下github上面的介紹,說的很詳細。不過都是基于Linux。

我們的目標(biāo)檢測就在research/object_detection??

官方也有對該文件目錄的簡介和使用方法(Linux)。

接下來我們第三步下載的編譯器就是用來編譯object_detection里的porto文件的


在bin下有個exe文件,我們將這個bin目錄直接添加到環(huán)境變量里面方便使用。

在cmd輸入protoc

這就說明proto工具可以使用,然后我們開始編譯object_detection下的protos文件下的所有proto文件。

我們在cmd切換到research目錄下后輸入protoc ./object_detection/protos/*.proto --python_out=.

編譯完成后會生成很多.py文件

接下來在我們python環(huán)境下加入引用

我的目錄為 D:\ProgramData\Anaconda3\envs\py36\Lib\site-packages

在這個目錄下創(chuàng)建一個.pth的文件指明slim框架的位置,因為object_detection里面代碼也調(diào)用了slim框架。

現(xiàn)在我們就可以跑下測試代碼看看是否環(huán)境搭建完成,在我們的py36環(huán)境下運行python object_detection/builders/model_builder_test.py


出現(xiàn)OK就說明我們環(huán)境部署是沒問題的,那我們在跑一個demo試試?

我們打開官方demo稍稍修改下就可以使用了,修改結(jié)果。

import numpy as np

import os

import six.moves.urllib as urllib

import sys

import tarfile

import tensorflow as tf

import zipfile

import matplotlib

import cv2

# Matplotlib chooses Xwindows backend by default.

matplotlib.use('Agg')

from collections import defaultdict

from io import StringIO

from matplotlib import pyplot as plt

from PIL import Image

from utils import label_map_util

from utils import visualization_utils as vis_util

##################### Download Model,如果本地已下載也可修改成本地路徑

# What model to download.

MODEL_NAME = 'ssd_mobilenet_v1_coco_11_06_2017'

MODEL_FILE = MODEL_NAME + '.tar.gz'

DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'

# Path to frozen detection graph. This is the actual model that is used for the object detection.

PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'

# List of the strings that is used to add correct label for each box.

PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')

NUM_CLASSES = 90

# Download model if not already downloaded

if not os.path.exists(PATH_TO_CKPT):

? ? print('Downloading model... (This may take over 5 minutes)')

? ? opener = urllib.request.URLopener()

? ? opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)

? ? print('Extracting...')

? ? tar_file = tarfile.open(MODEL_FILE)

? ? for file in tar_file.getmembers():

? ? ? ? file_name = os.path.basename(file.name)

? ? ? ? if 'frozen_inference_graph.pb' in file_name:

? ? ? ? ? ? tar_file.extract(file, os.getcwd())

else:

? ? print('Model already downloaded.')

##################### Load a (frozen) Tensorflow model into memory.

detection_graph = tf.Graph()

with detection_graph.as_default():

? ? od_graph_def = tf.GraphDef()

? ? with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:

? ? ? ? serialized_graph = fid.read()

? ? ? ? od_graph_def.ParseFromString(serialized_graph)

? ? ? ? tf.import_graph_def(od_graph_def, name='')

##################### Loading label map

print('Loading label map...')

label_map = label_map_util.load_labelmap(PATH_TO_LABELS)

categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)

category_index = label_map_util.create_category_index(categories)

def load_image_into_numpy_array(image):

? (im_width, im_height) = image.size

? return np.array(image.getdata()).reshape(

? ? ? (im_height, im_width, 3)).astype(np.uint8)

# 測試圖片的路徑

TEST_IMAGE_PATH = 'test_images/test1.jpg'

# Size, in inches, of the output images.

IMAGE_SIZE = (12, 8)

with detection_graph.as_default():

? with tf.Session(graph=detection_graph) as sess:

? ? image = Image.open(TEST_IMAGE_PATH)

? ? image_np = load_image_into_numpy_array(image)

? ? image_np_expanded = np.expand_dims(image_np, axis=0)

? ? image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

? ? boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

? ? scores = detection_graph.get_tensor_by_name('detection_scores:0')

? ? classes = detection_graph.get_tensor_by_name('detection_classes:0')

? ? num_detections = detection_graph.get_tensor_by_name('num_detections:0')

? ? # Actual detection.

? ? (boxes, scores, classes, num_detections) = sess.run(

? ? ? ? [boxes, scores, classes, num_detections],

? ? ? ? feed_dict={image_tensor: image_np_expanded})

? ? # Visualization of the results of a detection.

? ? vis_util.visualize_boxes_and_labels_on_image_array(

? ? ? ? image_np,

? ? ? ? np.squeeze(boxes),

? ? ? ? np.squeeze(classes).astype(np.int32),

? ? ? ? np.squeeze(scores),

? ? ? ? category_index,

? ? ? ? use_normalized_coordinates=True,

? ? ? ? line_thickness=8)

? ? print(TEST_IMAGE_PATH.split('.')[0]+'_labeled.jpg')

? ? plt.figure(figsize=IMAGE_SIZE, dpi=300)

? ? plt.imshow(image_np)

? ? plt.savefig(TEST_IMAGE_PATH.split('.')[0] + '_labeled.jpg')

上面就修改了下圖片路徑,和改變了圖片的保存方式。

我們從網(wǎng)上隨便找些圖片放在我們測試文件夾下運行該代碼,就會出現(xiàn)所預(yù)測的結(jié)果。


不過從結(jié)果上我們環(huán)境運行是沒問題的,那我們就可以自己準(zhǔn)備數(shù)據(jù)學(xué)習(xí)自己針對的目標(biāo)檢測。


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.恐懼變化是人性 百變的人生,變幻莫測的時代。我們總是害怕變化和恐懼被改變。接受這一人性的弱點,你就開始改變的第...
    清不負此生閱讀 291評論 0 7
  • 初高中的時候,我最怕別人談?wù)摾硐耄瑒e人的理想都是科學(xué)家、工程師、文學(xué)家甚至是改變社會,發(fā)展國家,拯救地球之類,如此...
    花生zfh閱讀 481評論 0 0
  • 近年來,抗衰老領(lǐng)域出現(xiàn)了一個新的研究發(fā)現(xiàn)。人類體內(nèi)的老化,疾病的重大原因就是身體被糖化了。可以說,這是所有人類老化...
    ibeautyforlady閱讀 1,864評論 0 0
  • 目錄 UI組件 開發(fā)框架 實用庫 服務(wù)端 輔助工具 應(yīng)用實例 Demo示例 UI組件 element ★31142...
    吳佳浩閱讀 11,079評論 1 61
  • 學(xué)習(xí)完健康管理師的課程,對自己減肥的偉大事業(yè)一直都未啟動。還好,辦了健身卡,從每周的四五次,到現(xiàn)在每周一次兩...
    隨風(fēng)飄蕩的羽毛閱讀 354評論 0 0

友情鏈接更多精彩內(nèi)容