【问题标题】:Return type of net.forward()net.forward() 的返回类型
【发布时间】:2020-04-12 01:10:37
【问题描述】:

您好,我正在努力通过使用更快的 rcnn 模型和 tensorflow 来检测此人。 在我所指的代码中提到了它

net = cv2.dnn.readNetFromTensorflow(args["inferencegraph"],args["graphpbtxt"])

然后:

detections = net.forward()

我不知道检测到底是什么以及它包含什么内容? 例如它是一个列表还是一个元组,它的元素是什么?

【问题讨论】:

    标签: python opencv tensorflow object-detection faster-rcnn


    【解决方案1】:

    cv2.dnn.readNetFromTensorflow 获取您的Protobuf 文件.pb 和模型的配置文件.pbtxt 来加载您保存的模型。

    net.forward() - 运行正向传递以计算净输出。

    您的检测,即net.forward() 将给出Numpy ndarray 作为输出,您可以使用它在给定的输入图像上绘制框。

    您可以考虑以下示例。

    import cv2
    
    # Load a model imported from Tensorflow
    tensorflowNet = cv2.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'graph.pbtxt')
    
    # Input image
    img = cv2.imread('img.jpg')
    rows, cols, channels = img.shape
    
    # Use the given image as input, which needs to be blob(s).
    tensorflowNet.setInput(cv2.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False))
    
    # Runs a forward pass to compute the net output
    networkOutput = tensorflowNet.forward()
    
    # Loop on the outputs
    for detection in networkOutput[0,0]:
    
        score = float(detection[2])
        if score > 0.2:
    
            left = detection[3] * cols
            top = detection[4] * rows
            right = detection[5] * cols
            bottom = detection[6] * rows
    
            #draw a red rectangle around detected objects
            cv2.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (0, 0, 255), thickness=2)
    
    # Show the image with a rectagle surrounding the detected objects 
    cv2.imshow('Image', img)
    cv2.waitKey()
    cv2.destroyAllWindows()  
    

    我考虑过Inception-SSD v2 的重量文件,可以从here 下载。 并从此link 配置文件。

    【讨论】:

    • @Pratik - 如果上述答案解决了您的问题,请接受并投票。
    【解决方案2】:

    net.forward()Numpy ndarray 作为输出。

    在您上面的示例中,detections = net.forward() detections 是一个数组输出。如果你计算它的形状,那么它将给出 4 个元素,例如(1,1,200,7)。

    其中 1,1 告诉我们当前正在处理的图像数量。

    200 是我上面假设的检测到的人脸数量。

    而 7 是 7 个值的向量 [图像编号、二进制(0 或 1)、置信度得分(0 到 1)、StartX、StartY、EndX、EndY]。

    for i in range(detections.shape[2]):    
        confidence = detections[0, 0, i, 2]
    

    在此示例中,循环将遍历所有检测到的人脸,并返回检测到的每个人脸的置信度。

    【讨论】:

      猜你喜欢
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 2019-12-14
      相关资源
      最近更新 更多