-
大小: 6KB文件類型: .py金幣: 1下載: 1 次發布日期: 2021-06-07
- 語言: Python
- 標簽: TensorFlow??視頻??目標檢測??
資源簡介
配置及運行文章見http://blog.csdn.net/asukasmallriver/article/details/78696260
代碼片段和文件信息
#?coding:?utf-8??
??
#?#?object?Detection?Demo??
#?Welcome?to?the?object?detection?inference?walkthrough!??This?notebook?will?walk?you?step?by?step?through?the?process?of?using?a?pre-trained?model?to?detect?objects?in?an?image.?Make?sure?to?follow?the?[installation?instructions](https://github.com/tensorflow/models/blob/master/object_detection/g3doc/installation.md)?before?you?start.??
??
#?#?Imports??
??
#?In[1]:??
??
import?numpy?as?np??
import?os??
import?six.moves.urllib?as?urllib??
import?sys??
import?tarfile??
import?tensorflow?as?tf??
import?zipfile??
??
from?collections?import?defaultdict??
from?io?import?StringIO??
from?matplotlib?import?pyplot?as?plt??
from?PIL?import?Image??
??
import?cv2??????????????????#add?20170825??
cap?=?cv2.VideoCapture(0)???#add?20170825??
??
#?##?Env?setup??
??
#?In[2]:??????????????????????????????????#delete?20170825??
#?This?is?needed?to?display?the?images.????#delete?20170825??
#get_ipython().magic(‘matplotlib?inline‘)???#delete?20170825??
??
#?This?is?needed?since?the?notebook?is?stored?in?the?object_detection?folder.????
sys.path.append(“..“)??
??
??
#?##?object?detection?imports??
#?Here?are?the?imports?from?the?object?detection?module.??
??
#?In[3]:??
??
from?utils?import?label_map_util??
??
from?utils?import?visualization_utils?as?vis_util??
??
??
#?#?Model?preparation???
??
#?##?Variables??
#???
#?Any?model?exported?using?the?‘export_inference_graph.py‘?tool?can?be?loaded?here?simply?by?changing?‘PATH_TO_CKPT‘?to?point?to?a?new?.pb?file.????
#???
#?By?default?we?use?an?“SSD?with?Mobilenet“?model?here.?See?the?[detection?model?zoo](https://github.com/tensorflow/models/blob/master/object_detection/g3doc/detection_model_zoo.md)?for?a?list?of?other?models?that?can?be?run?out-of-the-box?with?varying?speeds?and?accuracies.??
??
#?In[4]:??
??
#?What?model?to?download.??
MODEL_NAME?=?‘ssd_mobilenet_v1_coco_11_06_2017‘??
#MODEL_NAME?=?‘faster_rcnn_resnet101_coco_11_06_2017‘
#MODEL_NAME?=?‘ssd_inception_v2_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??
??
#?In[5]:??
??
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())??
??
??
#?##?Load?a?(frozen)?Tensorflow?model?into?memory.??
??
#?In[6]:??
??
detection_graph?=?tf.Graph()??
with?detection_gra
評論
共有 條評論