【问题标题】:python - "from utils import label_map_util" ImportError: cannot import name 'label_map_util'python - “从实用程序导入 label_map_util” ImportError:无法导入名称'label_map_util'
【发布时间】:2019-01-05 06:28:40
【问题描述】:

当我运行这段代码时,我得到一个导入错误

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
cap = cv2.VideoCapture("ipr.mp4")

from utils import label_map_util
from utils import visualization_utils as vis_util

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_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'

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

NUM_CLASSES = 90



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



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)


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)


PATH_TO_TEST_IMAGES_DIR = 'test_images'
TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 3) ]

IMAGE_SIZE = (12, 8)



with detection_graph.as_default():
  with tf.Session(graph=detection_graph) as sess:
    while True:
      ret, image_np = cap.read()
      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')
      (boxes, scores, classes, num_detections) = sess.run(
          [boxes, scores, classes, num_detections],
          feed_dict={image_tensor: image_np_expanded})
      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)

      cv2.imshow('object detection', cv2.resize(image_np, (800,600)))
      if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

错误:

警告:打开文件时出错 (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:834)警告: ipr.mp4 (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:835) Traceback(最近一次调用最后一次):文件“test.py”,第 31 行,在 from utils import label_map_util ImportError: cannot import name 'label_map_util'

【问题讨论】:

  • 如果你删除除了“from utils import label_map_util”行之外的所有代码,你会得到相同的 ImportError 吗?
  • 是的,我得到了同样的错误
  • 那么您可以仅使用与您的问题相关的信息来简化您的帖子

标签: python tensorflow


【解决方案1】:

CD 到 object_detection 目录

import( os )
os.chdir( 'D:\\projects\\data core\\helmet detection\\models\\research\\object_detection' )

并更改这些行

from utils import label_map_util

from utils import visualization_utils as vis_util

到以下几行

from object_detection.utils import label_map_util

from object_detection.utils import visualization_utils as vis_util

它会起作用的。

来源:https://github.com/tensorflow/models/issues/1990

【讨论】:

  • 运行代码的工作目录是什么?
  • D:\Pythonic\My-Work
  • 克隆这个github.com/tensorflow/models,你会在那里找到一个名为..\\models\\research\\object_detection的文件夹——然后运行上面的代码。它会起作用的。
  • 代码 label_map_util 位于 object_detection 目录中名为 utils 的文件夹中
  • @RoxasZohbi,如果此答案满足/满足您的需求,请接受。人们会更快地注意到它。
【解决方案2】:

这对我有用。但是,为了使其正常工作,我必须执行以下操作:

  1. 使用以下内容创建虚拟环境: i) OpenCV 4.0.1 ii) Python 3.6 iii) tensorflow v1.12

  2. 单独安装和/或更新各种依赖项: conda 安装 scipy pip install --upgrade sklearn pip install --upgrade pandas pip install --upgrade pandas-datareader pip install --upgrade matplotlib pip install --upgrade 枕头 pip install --upgrade 请求 pip install --upgrade h5py pip install --upgrade pyyaml pip install --upgrade psutil pip install --upgrade tensorflow==1.12.0 pip install -- 升级 lxml pip install opencv-contrib-python

  3. 编译所有协议缓冲区定义文件: i)cd 到模型/研究文件夹 ii)protoc object_detection/protos/*.proto --python_out=.

4.导出正确的 PYTHONPATH 变量路径: i) 导出 PYTHONPATH=$PYTHONPATH:pwd:pwd/slim ii) 回显 $PYTHONPATH

  1. 进入文件所在的文件夹(object_dectection)并在python中运行 i) cd object_detection/ ii) python test_pyprog.py

【讨论】:

    【解决方案3】:
    pip install tensorflow-object-detection-api
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 2014-09-27
      • 2020-04-09
      • 2016-12-24
      相关资源
      最近更新 更多