tensorflow开源Tensorflow Object Detection API安装运行测试

来源:互联网 发布:淘宝宝贝改价格会影响排名吗 编辑:程序博客网 时间:2024/06/04 19:52

TensorFlow对象检测API是一个建立在TensorFlow之上的开源框架,可以轻松构建,训练和部署对象检测模型。
安装:
Tensorflow对象检测API依赖于以下项:
Protobuf 2.6
Pillow 1.0
lxml
tf Slim (which is included in the “tensorflow/models” checkout)
Jupyter notebook
Matplotlib
Tensorflow
以下是安装步骤:
我是pip安装的tensorflow1.2版本,1以下版本好像不兼容该API,命令如下:
sudo pip install –upgrade
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.2.1-cp27-cp27mu-manylinux1_x86_64.whl

pip安装的tensorflow里面不知道怎么没有model文件夹,这儿Tensorflow Object Detection API的model我是在guthub上下载下来https://github.com/tensorflow/models ,并解压在home目录(不用放在tensorflow目录),以下操作大多在解压后的model目录下操作

其余的库可以通过apt-get安装:
sudo apt-get install protobuf-compiler python-pil python-lxml
sudo pip install jupyter
sudo pip install matplotlib
以上命令也可以使用以下四条pip命令代替:
sudo pip install pillow
sudo pip install lxml
sudo pip install jupyter
sudo pip install matplotlib
注:安装jupyter时可能遇到错误,更新一下pip再安装,sudo -H pip install –upgrade pip

Tensorflow Object Detection API使用Protobufs来配置模型和训练参数。在使用框架之前,必须编译Protobuf库。这应该通过从下载解压的models/目录运行以下命令来完成:
protoc object_detection/protos/*.proto –python_out=.

当在本地运行时,models /和slim目录应该附加到PYTHONPATH。这可以通过从models /运行以下来完成:
export PYTHONPATH=$PYTHONPATH:pwd:pwd/slim
注意:此命令需要从您启动的每个新终端运行。如果您想避免手动运行,可以将其作为新行添加到〜/ .bashrc文件的末尾。

至此安装完毕,可以通过运行以下命令来测试是否正确安装了Tensorflow Object Detection API:
python object_detection / builders / model_builder_test.py

这儿我测试了SSDmobilenet和SSDinception等模型,提前下载好模型,修改里面的代码,将图片测试改为视频读取测试,具体如下:
下载模型(先下载一个ssd_mobilenet_v1_coco,也是速度最快的):
https://github.com/tensorflow/models/blob/master/object_detection/g3doc/detection_model_zoo.md
下载的ssd_mobilenet_v1_coco_11_06_2017.tar.gz放在models/object_detection目录

修改代码,测试SSDmobilenet代码位于
https://github.com/tensorflow/models/blob/master/object_detection/object_detection_tutorial.ipynb
修改如下:

import numpy as npimport osimport six.moves.urllib as urllibimport sysimport tarfileimport tensorflow as tfimport zipfileimport cv2import time  from 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 folder.sys.path.append("..")from utils import label_map_utilfrom utils import visualization_utils as vis_util# 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'# 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('/home/hmw/tensorflow/models/object_detection/data', 'mscoco_label_map.pbtxt')#extract the ssd_mobilenetstart = time.clock()NUM_CLASSES = 90opener = 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())end= time.clock()print 'load the model',(end-start)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='')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)cap = cv2.VideoCapture(0)with detection_graph.as_default():  with tf.Session(graph=detection_graph) as sess:      writer = tf.summary.FileWriter("logs/", sess.graph)        sess.run(tf.global_variables_initializer())        while(1):    start = time.clock()        ret, frame = cap.read()        if cv2.waitKey(1) & 0xFF == ord('q'):            break        image_np=frame        # the array based representation of the image will be used later in order to prepare the        # result image with boxes and labels on it.        # Expand dimensions since the model expects images to have shape: [1, None, None, 3]        image_np_expanded = np.expand_dims(image_np, axis=0)        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')        # Each box represents a part of the image where a particular object was detected.        boxes = detection_graph.get_tensor_by_name('detection_boxes:0')        # Each score represent how level of confidence for each of the objects.        # Score is shown on the result image, together with the class label.        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=6)    end = time.clock()    print 'frame:',1.0/(end - start)    #print 'frame:',time.time() - start    cv2.imshow("capture", image_np)    cv2.waitKey(1)cap.release()cv2.destroyAllWindows() 

修改后在models/object_detection目录下运行(否则会报错),先添加root权限(否则会报错),此测试需要提前安装好opencv2,(若没有安装运行命令:sudo apt-get install python-opencv 即可安装)接上摄像头然后python运行上述代码webcamtest.py,得到结果。测试时如果出现错误HIGHGUI ERROR: libv4l unable to ioctl VIDIOCSPICT…,将命令窗口关闭重新开启即可解决