Python實時物件檢測入門指南

zhongpeijiaoyu發表於2020-08-05

  多年來,研究人員一直在研究賦予機器以視覺識別和識別物體的能力的可能性。這個稱為計算機視覺或CV的特定領域具有廣泛的現代應用程式。從被自動駕駛汽車用於道路目標檢測到複雜的面部和肢體語言識別(可以識別可能的犯罪或犯罪活動),CV在當今世界中有許多用途。不可否認,物件檢測還是Computer Vision最酷的應用之一。當今的CV工具可以輕鬆地在影像甚至是實時流視訊上實現物件檢測。在本文中,我們將看一下使用TensorFlow進行實時物件檢測的簡單演示。

  設定簡單的物件檢測器

  先決條件:

  Tensorflow> = 1.15.0

  通過執行pip install tensorflow安裝最新版本

  我們現在出發了!

  搭建環境

  步驟1.從Github下載或克隆TensorFlow物件檢測程式碼到本地計算機中

  在終端中執行以下命令:git clone 如果您的計算機上未安裝git,則可以選擇從此處下載zip檔案。

  步驟2.安裝依賴項

  下一步是確保我們擁有在計算機上執行物件檢測器所需的所有庫和模組。

  這是專案依賴的庫的列表。(預設情況下,大多數依賴項都隨Tensorflow一起提供)

  · 賽頓

  · contextlib2

  · 枕頭

  · xml檔案

  · matplotlib

  如果您發現缺少任何模組,只需在您的環境中執行pip install即可安裝。

  步驟3.安裝Protobuf編譯器

  Protobuf或Protocol緩衝區是Google的語言無關,平臺無關的可擴充套件機制,用於序列化結構化資料。它可以幫助我們定義我們希望資料的結構方式,一旦結構化,就可以輕鬆地使用各種語言在各種資料流之間讀寫結構化資料。

  這也是該專案的依賴項。您可以在此處瞭解有關Protobufs的更多資訊。現在,選擇適合您的作業系統的版本,然後複製下載連結。

  開啟終端或命令提示符,將目錄更改為克隆的儲存庫,然後在終端中執行以下命令。

  cd models/research

  wget -O protobuf.zip

  unzip protobuf.zip

  注意:請確保在models / research目錄中解壓縮protobuf.zip檔案

  步驟4.編譯Protobuf編譯器

  從research /目錄執行以下命令以編譯協議緩衝區。

  在Python中實現物件檢測

  現在,我們已經安裝了所有依賴項,讓我們使用Python來實現物件檢測。

  在下載的儲存庫中,將目錄更改為 models/research/object_detection。在此目錄中,您將找到一個名為object_detection_tutorial.ipynb的ipython筆記本。該檔案是用於物件檢測的演示,執行時將使用指定的“ssd_mobilenet_v1_coco_2017_11_17模型對儲存庫中提供的兩個測試影像進行分類。

  以下是測試輸出之一:

  引入了一些小的更改以從實時流視訊中檢測物件。在相同的資料夾中製作一個新的Jupyter筆記本,並遵循以下程式碼。

  在[1]中:

  import numpy as npimport osimport six.moves.urllib as urllibimport sysimport tarfileimport tensorflow as tfimport zipfilefrom distutils.version import StrictVersionfrom collections import defaultdictfrom io import StringIOfrom matplotlib import pyplot as pltfrom PIL import Image# This is needed since the notebook is stored in the object_detection

  sys.path.append(“..”)from utils import ops as utils_opsif StrictVersion(tf.version) < StrictVersion(‘1.12.0’):

  raise ImportError(‘Please upgrade your TensorFlow installation to v1.12.*.’)

  在[2]中:

  # This is needed to display the images.

  get_ipython().run_line_magic(‘matplotlib’, ‘inline’)

  在[3]中:

  # Object detection imports# Here are the imports from the object detection module.from utils import label_map_utilfrom utils import visualization_utils as vis_util

  在[4]中:

  # Model preparation # Any model exported using the export_inference_graph.py tool can be loaded here simply by changing PATH_TO_FROZEN_GRAPH to point to a new .pb file.# By default we use an “SSD with Mobilenet” model here. #See

  MODEL_NAME = ‘ssd_mobilenet_v1_coco_2017_11_17’

  MODEL_FILE = MODEL_NAME + ‘.tar.gz’

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

  PATH_TO_FROZEN_GRAPH = 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’)

  在[5]中:

  #Download Model

  opener = urllib.request.URLopener()

  opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)

  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())

  在[6]中:

  # 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_FROZEN_GRAPH, ‘rb’) as fid:

  serialized_graph = fid.read()

  od_graph_def.ParseFromString(serialized_graph)

  tf.import_graph_def(od_graph_def, name=’’)

  在[7]中:

  # Loading label map# Label maps map indices to category names, so that when our convolution network predicts 5,#we know that this corresponds to airplane. Here we use internal utility functions, #but anything that returns a dictionary mapping integers to appropriate string labels would be fine

  category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)

  在[8]中:

  def run_inference_for_single_image(image, graph):

  with graph.as_default():

  with tf.Session() as sess:

  # Get handles to input and output tensors

  ops = tf.get_default_graph().get_operations()

  all_tensor_names = {output.name for op in ops for output in op.outputs}

  tensor_dict = {}

  for key in [

  ‘num_detections’, ‘detection_boxes’, ‘detection_scores’,

  ‘detection_classes’, ‘detection_masks’]:

  tensor_name = key + ‘:0’

  if tensor_name in all_tensor_names:

  tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(tensor_name)

  if ‘detection_masks’ in tensor_dict:

  # The following processing is only for single image

  detection_boxes = tf.squeeze(tensor_dict[‘detection_boxes’], [0])

  detection_masks = tf.squeeze(tensor_dict[‘detection_masks’], [0])

  # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.

  real_num_detection = tf.cast(tensor_dict[0], tf.int32)

  detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])

  detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])

  detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(

  detection_masks, detection_boxes, image.shape[1], image.shape[2])

  detection_masks_reframed = tf.cast(

  tf.greater(detection_masks_reframed, 0.5), tf.uint8)

  # Follow the convention by adding back the batch dimension

  tensor_dict[‘detection_masks’] = tf.expand_dims(

  detection_masks_reframed, 0)

  image_tensor = tf.get_default_graph().get_tensor_by_name(‘image_tensor:0’)

  # Run inference

  output_dict = sess.run(tensor_dict, feed_dict={image_tensor: image})

  # all outputs are float32 numpy arrays, so convert types as appropriate

  output_dict[‘num_detections’] = int(output_dict[‘num_detections’][0])

  output_dict[‘detection_classes’] = output_dict[

  ‘detection_classes’][0].astype(np.int64)

  output_dict[0]

  output_dict[0]

  if ‘detection_masks’ in output_dict:

  output_dict[0]

  return output_dict

  在[8]中:

  import cv2

  cam = cv2.cv2.VideoCapture(0)

  rolling = Truewhile (rolling):

  ret, image_np = cam.read()

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

  # Actual detection.

  output_dict = run_inference_for_single_image(image_np_expanded, detection_graph)

  # Visualization of the results of a detection.

  vis_util.visualize_boxes_and_labels_on_image_array(

  image_np,

  output_dict[‘detection_boxes’],

  output_dict[‘detection_classes’],

  output_dict[‘detection_scores’],

  category_index,

  instance_masks=output_dict.get(‘detection_masks’),

  use_normalized_coordinates=True,

  line_thickness=8)

  cv2.imshow(‘image’, cv2.resize(image_np,(1000,800)))

  if cv2.waitKey(25) & 0xFF == ord(‘q’):

  break

  cv2.destroyAllWindows()

  cam.release()

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章