欧美麻豆久久久久久中文_成年免费观看_男人天堂亚洲成人_中国一级片_动漫黄网站免费永久在线观看_国产精品自产av一区二区三区

中培偉業IT資訊頻道
您現在的位置:首頁 > IT資訊 > 軟件研發 > Python實時對象檢測入門指南

Python實時對象檢測入門指南

2020-08-05 18:12:35 | 來源:中培企業IT培訓網

  稱為計算機視覺或CV的特定領域具有廣泛的現代應用程序。從被自動駕駛汽車用作道路目標檢測到復雜的面部和肢體語言識別(可以識別可能的犯罪或犯罪活動),CV在當今世界中有很多用途。不可否認,對象檢測還是Computer Vision最酷的應用之一。當今的CV工具可以輕松地在圖像甚至是實時流視頻上實現對象檢測。在此處,我們將看一下使用TensorFlow進行實時對象檢測的簡單演示。

  設置簡單的對象檢測器

  先決條件:

  Tensorflow> = 1.15.0

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

  營造環境

  步驟1.下載或克隆TensorFlow對象檢測代碼到本地計算機中

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

  步驟2.安裝依賴項

  下一步是確保我們擁有在計算機上運行對象檢測器所需的所有庫和模塊。

 ?。ㄊ乔闆r下,大多數依賴項都隨隨Tensorflow一起提供)

  ·賽頓

  ·contextlib2

  ·枕頭

  ·xml文件

  ·matplotlib

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

步驟3.安裝Protobuf編譯器

  Protobuf或Protocol示例是Google的語言無關,平臺無關的可擴展機制,用于序列化結構化數據。它可以幫助我們定義我們希望數據的結構方式,一旦結構化,就可以輕松地使用各種語言在各種數據流之間讀寫結構化數據。

  這也是該項目的依賴項。您可以在此處了解有關Protobufs的更多信息?,F在,選擇適合您的操作系統的版本,然后復制下載鏈接。

  :終端或命令向導,將目錄更改為克隆的存儲庫,然后在終端中執行以下命令。

  cd模型/研究

  wget -O protobuf.zip 

  解壓縮protobuf.zip

  注意:請確保在模型/研究目錄中解壓縮protobuf.zip文件

  步驟4.編譯Protobuf編譯器

  從研究/目錄執行以下命令以編譯協議捆綁。

  在Python中實現對象檢測

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

  在此目錄中,您將找到一個名為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 tarimport import tensorflow as tfimport zipfile from distutils.version import strictVersionfrom collections import defaultdictfrom io import matplotlib import pyplot as plt from PIL import Image#這是必需的,因為筆記本存儲在object_中。

  utils中的sys.path.append(“ ..”)作為utils_opsif StrictVersion(tf .__ version__)<StrictVersion('1.12.0'):

  提高ImportError('請將您的TensorFlow安裝升級到v1.12。*。')

  在[2]中:

  #這是顯示圖像所必需的。

  get_ipython()。run_line_magic('matplotlib','inline')

  在[3]中:

 ?。ο髾z測導入#這是來自對象檢測模塊的導入。from utils import label_map_utilfrom utils import visualization_utils as vis_util

  在[4]中:

 ?。DP蜏蕚洌J褂胉export_inference_graph.py`工具導出的任何模型都可以通過更改`PATH_TO_FROZEN_GRAPH`指向新的.pb文件來加載。#默認情況下,我們在此處使用“帶有Mobilenet的SSD”模型。??吹?/span>

  MODEL_NAME ='ssd_mobilenet_v1_coco_2017_11_17'

  MODEL_FILE = MODEL_NAME +'.tar.gz'

  DOWNLOAD_BASE =#凍結檢測圖的路徑。這是用于對象檢測的實際模型。

  PATH_TO_FROZEN_GRAPH = MODEL_NAME +'/frozen_inference_graph.pb'#用于為每個框添加正確標簽的字符串列表。

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

  在[5]中:

 ?。O螺d模型

  開瓶器= urllib.request.URLopener()

  opener.retrieve(DOWNLOAD_BASE + MODEL_FILE,MODEL_FILE)

  tar_file = tarfile.open(MODEL_FILE)用于tar_file.getmembers()中的文件:

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

  如果文件名中的“ frozen_inference_graph.pb”:

  tar_file.extract(文件,os.getcwd())

  在[6]中:

 ?。⒁粋€(凍結的)Tensorflow模型加載到內存中。

  detection_graph = tf.Graph()和detection_graph.as_default():

  od_graph_def = tf.GraphDef()

  使用tf.gfile.GFile(PATH_TO_FROZEN_GRAPH,'rb')作為fid:

  serialized_graph = fid.read()

  od_graph_def.ParseFromString(serialized_graph)

  tf.import_graph_def(od_graph_def,name ='')

  在[7]中:

  #加載標簽地圖#標簽將地圖索引映射到類別名稱,這樣,當我們的卷積網絡預測“ 5”時,#我們知道它對應于“飛機”。在這里,我們使用內部實用程序函數,但是任何返回將整數映射到適當的字符串標簽的字典的方法都可以

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

  在[8]中:

  def run_inference_for_single_image(圖像,圖形):

  與graph.as_default():

  與tf.Session()作為sess:

 ?。+@取輸入和輸出張量的句柄

  ops = tf.get_default_graph()。get_operations()

  all_tensor_names = {ops中op的output.name在op.outputs中的輸出}

  tensor_dict = {}

  用于輸入[

  'num_detections','detection_boxes','detection_scores',

  'detection_classes','detection_masks']:

  張量名稱=鍵+':0'

  如果tensor_name在all_tensor_names中:

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

  如果tensor_dict中的'detection_masks':

 ?。R韵绿幚韮H適用于單個圖像

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

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

  #需要重新框架以將蒙版從框坐標轉換為圖像坐標并適合圖像尺寸。

  real_num_detection = tf.cast(tensor_dict ['num_detections'] [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)

 ?。W裱瓚T例,重新添加批次尺寸

  tensor_dict ['detection_masks'] = tf.expand_dims(

  detection_masks_reframed,0)

  image_tensor = tf.get_default_graph()。get_tensor_by_name('image_tensor:0')

 ?。_\行推斷

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

  #所有輸出均為float32 numpy數組,因此請適當轉換類型

  output_dict ['num_detections'] = int(output_dict ['num_detections'] [0])

  output_dict ['detection_classes'] = output_dict [

  'detection_classes'] [0] .astype(np.int64)

  output_dict ['detection_boxes'] = output_dict ['detection_boxes'] [0]

  output_dict ['detection_scores'] = output_dict ['detection_scores'] [0]

  如果output_dict中的“ detection_masks”:

  output_dict ['detection_masks'] = output_dict ['detection_masks'] [0]

  返回output_dict

  在[8]中:

  導入cv2

  cam = cv2.cv2.VideoCapture(0)

  滾動= Truewhile(滾動):

  ret,image_np = cam.read()

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

 ?。嶋H檢測。

  output_dict = run_inference_for_single_image(image_np_expanded,detection_graph)

 ?。?梢暬瘷z測結果。

  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 =是,

  line_thickness = 8)

  cv2.imshow('image',cv2.resize(image_np,(1000,800)))

  如果cv2.waitKey(25)和0xFF == ord('q'):

  打破

  cv2.destroyAllWindows()

  cam.release()

  以上即是關于Python實時對象檢測入門指南的全部內容,想了解更多更多關于Python的信息,請繼續關注中培偉業。

標簽: Python 軟件研發
主站蜘蛛池模板: 亚洲成AV人片在线观看ww | 亚洲欧洲日产国码久在线 | 在线看一区 | 亚洲男人的天堂在线aⅴ视频 | 加勒比色老久久爱综合网 | 超碰97人人做人人爱2020 | 久久免费毛片大全 | hd女人奶水授乳milk | 精品无人区无码乱码大片国产 | 美脚丝袜一区二区三区在线观看 | 久久男人高潮女人高潮 | 国产999精品成人网站 | 国产国拍亚洲精品MV在线观看" | 欧美黑人又粗又大一级A片 动漫AV网站免费观看 | 黑人玩弄人妻中文在线 | 成人片黄网站色大片免费观看CN | 丝袜足控一区二区三区 | 精品一线二线三线区别在哪欧美 | 成人欧美一区二区 | 亚洲另类激情专区小说 | 成人性做爰aaa片免费看 | 特级毛片www| 亚洲爱爱网站 | 亚洲AV永久无码精品一区二区 | 精精国产xxxx视频在线中文版 | 国产免费a∨片在线软件 | 国产亚洲精品久久777777美腿 | 免费高清毛片无遮挡 | 少妇在宾馆高潮不断狂叫床 | 强被迫伦姧惨叫在线视频 | 人妻少妇精品专区性色av | 91精品久久久久久9s密挑 | 强伦人妻一区二区三区视频18 | 久久激情戏 | 亚洲一区二区三区波多野结衣 | 狠狠操狠狠搞 | 韩国毛片免费看 | 亚洲aⅴ无码成人网站国产 婷婷亚洲精品久久无码一区二区三区 | 久久国产日韩精华液的功效 | chinese激烈高潮hd | 精品无人区卡卡卡卡卡二卡三乱码 |