python - 如何将参数传递给已加载的 tensorflow 图(在内存中)

标签 python tensorflow machine-learning deep-learning object-detection

我有一个使用 SSD-mobilenet 架构训练的对象检测模型。我使用网络摄像头根据该模型实时进行推理。输出是覆盖在网络摄像头图像上的边界框。

我正在按如下方式访问我的网络摄像头:

import cv2
cap = cv2.VideoCapture(0)

在视频源上实时运行推理的函数:

with detection_graph.as_default():
  with tf.Session(graph=detection_graph) as sess:
    while True:
      ret, image_np = cap.read()
      # 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=8)

      #print(boxes)


      for i, box in enumerate(np.squeeze(boxes)):
          if(np.squeeze(scores)[i] > 0.98):
              print("ymin={}, xmin={}, ymax={}, xmax{}".format(box[0]*height,box[1]*width,box[2]*height,box[3]*width))
      break

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

检测到对象时,我的终端会显示其标准化坐标。

这非常适合视频源,因为:

  • 模型已加载到内存中
  • 每当新对象出现在网络摄像头前时,加载的模型就会预测该对象并输出其坐标

我想要相同的图像功能,即我想要:

  • 模型已加载到内存中
  • 每当新参数提到图像位置时,加载的模型就会预测该对象并输出其坐标。

我应该如何通过修改上面的代码来做到这一点?我不希望有一个单独的服务器来执行此任务(如 tensorflow 服务中所述)。

如何在我的计算机上本地执行此操作?

最佳答案

您可以使用 os.listdir() 命令列出给定目录中的所有文件,然后遵循相同的管道。

import os
import cv2
path = "./path/to/image/folder"
images = os.listdir(path)

with detection_graph.as_default():
  with tf.Session(graph=detection_graph) as sess:
    for image in images:
      image_path = os.path.join(path, image)
      image_np = cv2.imread(image_path)
      # 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=8)

      #print(boxes)


      for i, box in enumerate(np.squeeze(boxes)):
          if(np.squeeze(scores)[i] > 0.98):
              print("ymin={}, xmin={}, ymax={}, xmax{}".format(box[0]*height,box[1]*width,box[2]*height,box[3]*width))
      break

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

关于python - 如何将参数传递给已加载的 tensorflow 图(在内存中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56748485/

相关文章:

python - 如何在模型中使用 "on delete restrict"?

python - 在机器学习中处理分类变量

algorithm - 我怎样才能找到下一个值?

python - Git 不提交文件

python - Django 默认缓存

python - 如何执行一个不为序列中的每个项目并行返回任何内容的操作?

matlab - 如何在神经网络Matlab中将训练数据制作为4D数组 - 输入数据的正确方法

python - 在colab上-class_weight导致ValueError : The truth value of an array with more than one element is ambiguous.使用a.any()或a.all()

python - LSTM模型的准确率很低

tensorflow - Google Colab - 训练时 session 崩溃错误