无法摆脱困境:无法使用学习前模型的输出

我使用opencv在树莓派4上进行对象检测。 https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb 并尝试转换为opencv以在本地运行它,以从网络摄像头拍摄图像。

我将网络摄像头设置为640x480分辨率,然后应用一些变换以将图像调整为300x300x3,因为这应该是输入模型的正确输入。

#crop the image to a square
image = image[0:480,84:564]
#now the image is 480x480
#scales the image to 300x300  
image = cv2.resize(image,(300,300),interpolation = cv2.INTER_AREA)

之后,我调用该函数     show_inference(detection_model,conversion_image)

def run_inference_for_single_image(model,image):
  image = np.asarray(image)
  # The input needs to be a tensor,convert it using `tf.convert_to_tensor`.
  input_tensor = tf.convert_to_tensor(image)
  # The model expects a batch of images,so add an axis with `tf.newaxis`.
  input_tensor = input_tensor[tf.newaxis,...]

  # Run inference
  output_dict = model(input_tensor)

  print('\noutputdict:\n',output_dict,'\n')
  # All outputs are batches tensors.
  # Convert to numpy arrays,and take index [0] to remove the batch dimension.
  # We're only interested in the first num_detections.
  num_detections = int(output_dict.pop('num_detections'))
  print('\nnum_detections:\n',num_detections,'\n')
  output_dict = {key:value[0,:num_detections].numpy() 
                 for key,value in output_dict.items()}
  output_dict['num_detections'] = num_detections

  # detection_classes should be ints.
  output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)

"""
  # Handle models with masks:
  if 'detection_masks' in output_dict:
    # Reframe the the bbox mask to the image size.
    detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
              output_dict['detection_masks'],output_dict['detection_boxes'],image.shape[0],image.shape[1])      
detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5,tf.uint8)
output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()
"""
  return output_dict

"""Run it on each test image and show the results:"""

def show_inference(model,image):
  # the array based representation of the image will be used later in order to prepare the
  # result image with boxes and labels on it.
  image_np = np.array(image)
  # actual detection.
  output_dict = run_inference_for_single_image(model,image_np)
  # Visualization of the results of a detection.
  vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,output_dict['detection_classes'],output_dict['detection_scores'],category_index,instance_masks=output_dict.get('detection_masks_reframed',None),use_normalized_coordinates=True,line_thickness=8)

  display(Image.fromarray(image_np))

在这一行(在run_inference_for_single_image(模型,图片)中):

num_detections = int(output_dict.pop('num_detections'))

我收到此错误:

Traceback (most recent call last):
  File "object_detection_webcam_opencv.py",line 223,in <module>
show_inference(detection_model,converted_image)
  File "object_detection_webcam_opencv.py",line 145,in show_inference
output_dict = run_inference_for_single_image(model,image_np)
  File "object_detection_webcam_opencv.py",line 116,in run_inference_for_single_image
  num_detections = int(output_dict.pop('num_detections'))
TypeError: int() argument must be a string,a bytes-like object or a number,not 'Tensor'

已经有3天了!我的树莓有问题吗?

模型所需的输入:

[<tf.Tensor 'image_tensor:0' shape=(?,?,3) dtype=uint8>] 

预期输出:

 {'detection_classes': TensorShape([Dimension(None),Dimension(100)]),'num_detections': TensorShape([Dimension(None)]),'detection_boxes': TensorShape([Dimension(None),Dimension(100),Dimension(4)]),'detection_scores': TensorShape([Dimension(None),Dimension(100)])} 

这就是我得到的:outputdict:

 {'detection_classes': <tf.Tensor 'StatefulPartitionedCall:1' shape=(?,100) dtype=float32>,'num_detections': <tf.Tensor 'StatefulPartitionedCall:3' shape=(?,) dtype=float32>,'detection_boxes': <tf.Tensor 'StatefulPartitionedCall:0' shape=(?,100,4) dtype=float32>,'detection_scores': <tf.Tensor 'StatefulPartitionedCall:2' shape=(?,100) dtype=float32>} 

这是整个脚本。py

# -*- coding: utf-8 -*-

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 pathlib
import cv2

"""Import the object detection module."""

from object_detection.utils import ops as utils_ops
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util

"""Patches:"""

# patch tf1 into `utils.ops`
utils_ops.tf = tf.compat.v1

# Patch the location of gfile
tf.gfile = tf.io.gfile



"""# Model preparation

## Variables

Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing the path.

By default we use an "SSD with Mobilenet" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/research/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.

## Loader
"""

def load_model(model_name):
  #per 'coco_ssd_mobilenet_v1_1.0_quant_2018_06_29'
  #base_url = 'https://storage.googleapis.com/download.tensorflow.org/models/tflite/'
  #model_file = model_name + '.zip'

  #per 'ssd_mobilenet_v1_coco_2017_11_17' e 'ssd_mobilenet_v1_coco_2018_01_28'
  base_url = 'http://download.tensorflow.org/models/object_detection/'
  model_file = model_name + '.tar.gz'

  model_dir = tf.compat.v1.keras.utils.get_file(
    fname=model_name,origin=base_url + model_file,untar=True)

  model_dir = pathlib.Path(model_dir)/"saved_model"

  model = tf.compat.v1.keras.models.load_model(str(model_dir))
  model = model.signatures['serving_default']

  return model

"""## Loading label map
Label maps map indices to category names,so that when our convolution network predicts `5`,we know that this corresponds to `airplane`.  Here we use internal utility functions,but anything that returns a dictionary mapping integers to appropriate string labels would be fine
"""

# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = '/home/pi/venv/models/research/object_detection/data/mscoco_label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS,use_display_name=True)

"""For the sake of simplicity we will test on 2 images:"""

# If you want to test the code with your images,just add path to the images to the TEST_IMAGE_PATHS.
PATH_TO_TEST_IMAGES_DIR = pathlib.Path('models/research/object_detection/test_images')
TEST_IMAGE_PATHS = sorted(list(PATH_TO_TEST_IMAGES_DIR.glob("*.jpg")))
TEST_IMAGE_PATHS

"""# Detection

Load an object detection model:
"""
#model_name = 'ssd_mobilenet_v1_coco_2017_11_17'
model_name = 'ssd_mobilenet_v1_coco_2018_01_28'
#model_name = 'coco_ssd_mobilenet_v1_1.0_quant_2018_06_29'
detection_model = load_model(model_name)

"""Check the model's input signature,it expects a batch of 3-color images of type uint8:"""

print('\nInput:\n',detection_model.inputs,'\n')

"""And retuns several outputs:"""

detection_model.output_dtypes

print('\nOutput:\n',detection_model.output_shapes,'\n')

"""Add a wrapper function to call the model,and cleanup the outputs:"""

def run_inference_for_single_image(model,:num_detections].numpy() 
             for key,value in output_dict.items()}
  output_dict['num_detections'] = num_detections

  # detection_classes should be ints.
  output_dict['detection_classes'] =  output_dict['detection_classes'].astype(np.int64)

  """ 
  # Handle models with masks:
  if 'detection_masks' in output_dict:
    # Reframe the the bbox mask to the image size.
    detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
          output_dict['detection_masks'],tf.uint8)
output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()
  """  
  return output_dict

"""Run it on each test image and show the results:"""

def show_inference(model,line_thickness=8)

  display(Image.fromarray(image_np))


#accedo alla webcam
cap = cv2.VideoCapture(0)

#setto un framerate sufficientemente basso
cap.set(5,5)

#setto larghezza e poi altezza dello stream
cap.set(3,640)
cap.set(4,480)

def convert_Image(image):

  #Riduco l'immagine ad un formato 1:1 senza deformarla
  image = image[0:480,84:564]

  #scalo l'immagine a 28x28  
  image = cv2.resize(image,interpolation = cv2.INTER_AREA)
  print('\nLa risoluzione scalata è',image.shape,'\n')


  return image

#3) Crea un oggetto immagine
if cap.isOpened():
  check,image = cap.read()
  print('\nLa risoluzione è','\n')
else:
    check = False

while check:
  #print('Original: ',image)
  #print('Shape: ',image.shape)
  check,image = cap.read()
  converted_image = convert_Image(image)

  #mostra l'mmagine
  cv2.imshow('Object detection',image)
  cv2.imshow("Converted",converted_image)

  show_inference(detection_model,converted_image)

  #5) Per interrompere lo streaming premere un tasto
  key = cv2.waitKey(20)

  if key == 27: #per uscire premere ESC
    cv2.destroyAllWindows()
    cap.release
    break

  #to break the cycle after 1 run just for troubleshoot purpose
  check = False
leecomezhou 回答:无法摆脱困境:无法使用学习前模型的输出

在调用show_inference之前可以尝试转换为numpy数组吗? 或在返回之前在转换图像功能的末尾添加此行-


def convert_Image(image):
    image = np.asarray(image) 
    return image

如果这不起作用,请尝试调整图像大小,然后再将图像转换为numpy数组。该模型要求图像为numpy数组格式。

以下代码适用于tf 2.0和cv2-


#!/usr/bin/env python
# coding: utf-8
"""
Object detection with live camera using cv2 and tf2.0
"""
import pathlib
import cv2
import numpy as np
import tensorflow as tf
import sys
import time
# Import the object detection module.
from object_detection.utils import ops as utils_ops
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util

# patch tf1 into `utils.ops`
utils_ops.tf = tf.compat.v1

# Patch the location of gfile
tf.gfile = tf.io.gfile

def load_model(model_name):
    """Loading the model from the url"""
    base_url = 'http://download.tensorflow.org/models/object_detection/'
    model_file = model_name + '.tar.gz'
    model_dir = tf.keras.utils.get_file(
      fname=model_name,origin=base_url + model_file,untar=True)

    model_dir = pathlib.Path(model_dir)/"saved_model"

    model = tf.saved_model.load(str(model_dir))
    model = model.signatures['serving_default']

    return model

def run_inference_for_single_image(model,image):
    """ Add a wrapper function to call the model,and cleanup the outputs:"""
    image = np.asarray(image)
    # The input needs to be a tensor,convert it using `tf.convert_to_tensor`.
    input_tensor = tf.convert_to_tensor(image)
    # The model expects a batch of images,so add an axis with `tf.newaxis`.
    input_tensor = input_tensor[tf.newaxis,...]

    # Run inference
    output_dict = model(input_tensor)

    # We're only interested in the first num_detections.
    num_detections = int(output_dict.pop('num_detections'))
    output_dict = {key:value[0,:num_detections].numpy()
                   for key,value in output_dict.items()}
    output_dict['num_detections'] = num_detections

    # detection_classes should be ints.
    output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)

    # Handle models with masks:
    if 'detection_masks' in output_dict:
      # Reframe the the bbox mask to the image size.
      detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
              output_dict['detection_masks'],output_dict['detection_boxes'],image.shape[0],image.shape[1])      
      detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5,tf.uint8)
      output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()

    return output_dict


def show_inference(model,image):
    """# Run it on each test image and show the results:
    # the array based representation of the image will be used later in order to prepare the
    # result image with boxes and labels on it.
    """
    image_np = np.array(image)
    # Actual detection.
    output_dict = run_inference_for_single_image(model,image_np)
    # Visualization of the results of a detection.
    vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,output_dict['detection_classes'],output_dict['detection_scores'],category_index,instance_masks=output_dict.get('detection_masks_reframed',None),use_normalized_coordinates=True,line_thickness=8)

    return image_np


def main():
    """
    load the model and run the logic
    """
    #  Detection Load an object detection model:
    model_name = 'ssd_mobilenet_v1_coco_2017_11_17'
    detection_model = load_model(model_name)

    try:
        cap = cv2.VideoCapture(0)  # video capture source camera (Here webcam of laptop)
        start = end = time.time()
        while (True):
            ret,frame = cap.read()  # return a single frame in variable `frame`
            image = np.asarray(frame)
            image_inf = show_inference(detection_model,image)
            end = time.time()
            cv2.imshow('Live web camera',image_inf)
            if cv2.waitKey(1) == ord('q'):
                cv2.destroyAllWindows()
                break
        cap.release()
    finally:
        print("Could not open video source,exiting the program !!")
        cap.release()
        sys.exit(1)


# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = '/home/sumanh/github/tf_models/models/research/object_detection/data/mscoco_label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS,use_display_name=True)
if __name__ == '__main__':
    main()

本文链接:https://www.f2er.com/3145933.html

大家都在问