【问题标题】:How can I use the gluon-cv model_zoo and output to an OpenCV window with Python?如何使用 gluon-cv model_zoo 并通过 Python 输出到 OpenCV 窗口?
【发布时间】:2019-06-17 05:16:51
【问题描述】:

我的代码是:

import gluoncv as gcv

net = gcv.model_zoo.get_model('ssd_512_mobilenet1.0_voc', pretrained=True)

windowName = "ssdObject"
cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
cv2.resizeWindow(windowName, 1280, 720)
cv2.moveWindow(windowName, 0, 0)
cv2.setWindowTitle(windowName, "SSD Object Detection")
while True:
    # Check to see if the user closed the window
    if cv2.getWindowProperty(windowName, 0) < 0:
        # This will fail if the user closed the window; Nasties get printed to the console
        break
    ret_val, frame = video_capture.read()

    frame = mx.nd.array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)).astype('uint8')
    rgb_nd, frame = gcv.data.transforms.presets.ssd.transform_test(frame, short=512, max_size=700)

    # # Run frame through network
    class_IDs, scores, bounding_boxes = net(rgb_nd)

    displayBuf = frame
    cv2.imshow(windowName, displayBuf)
    cv2.waitKey(0)

不知何故,我需要将bounding_codesclass_IDsscores 绘制到图像上并通过imshow 输出。

我怎样才能做到这一点?

【问题讨论】:

  • 嗯。不熟悉该库,但我猜想class_IDs, scores, bounding_boxes 是 3 个长度相同的数组,由索引 ID 捆绑在一起吗?即每个边界框都有一个关联的 class_ID 和分数? |如果是这样(除非在 gluoncv 中有一些预制的渲染功能)......也许只是循环数组并使用 primitive drawing functions 来绘制矩形和两个文本,可能是随机颜色......
  • 嗯,也许this是预制的?
  • 这看起来像是使用 matlab 绘图而不是 OpenCV 窗口 @DanMašek
  • 对...所以我想要么自己制作简单的渲染器,要么将 matplotlib 输出到内存中的图像中,然后用imshow 显示出来...虽然这似乎有点过头了.自己画应该很糟糕......我能看到的最糟糕的事情是摆弄文本的定位/大小,以使其在各种大小的边界框下看起来合理。
  • 您能否将其简化为在单个输入图像上运行(并将其提供为 PNG)和示例 class_IDs, scores, bounding_boxes 值?然后我可以在不安装 mxnet 和查找/确定要使用的视频的情况下制定解决方案。 |顺便说一句,如果您想将原始 frame 用于可视化,您可能应该将原始 frame 存储为 BGR 格式。 (或者,gcv.data.transforms.presets.ssd.transform_test 是如何修改框架的?)

标签: python opencv object-detection mxnet


【解决方案1】:

我们可以使用ssd|yolo(作者mxnet|keras|pytorch)来检测图像中的物体。然后我们将以classids/scores/bboxes的形式获得结果。迭代结果,做一些变换,然后在 OpenCV 中绘图就可以了。

(英语很差,但我想你可以在下面的代码中找到我)。


这是源图像:

这是OpenCV中显示的结果:


#!/usr/bin/python3
# 2019/01/24 09:05
# 2019/01/24 10:25

import gluoncv as gcv
import mxnet as mx
import cv2
import numpy as np
# https://github.com/pjreddie/darknet/blob/master/data/dog.jpg

## (1) Create network 
net = gcv.model_zoo.get_model('ssd_512_mobilenet1.0_voc', pretrained=True)

## (2) Read the image and preprocess 
img = cv2.imread("dog.jpg")
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

xrgb = mx.nd.array(rgb).astype('uint8')
rgb_nd, xrgb = gcv.data.transforms.presets.ssd.transform_test(xrgb, short=512, max_size=700)

## (3) Interface 
class_IDs, scores, bounding_boxes = net(rgb_nd)

## (4) Display 
for i in range(len(scores[0])):
    #print(class_IDs.reshape(-1))
    #print(scores.reshape(-1))
    cid = int(class_IDs[0][i].asnumpy())
    cname = net.classes[cid]
    score = float(scores[0][i].asnumpy())
    if score < 0.5:
        break
    x,y,w,h = bbox =  bounding_boxes[0][i].astype(int).asnumpy()
    print(cid, score, bbox)
    tag = "{}; {:.4f}".format(cname, score)
    cv2.rectangle(img, (x,y), (w, h), (0, 255, 0), 2)
    cv2.putText(img, tag, (x, y-20),  cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,255), 1)

cv2.imshow("ssd", img);
cv2.waitKey()

【讨论】:

    【解决方案2】:

    GluonCV 最近在 OpenCV 中包含了可视化功能。

    要调用这些函数,您只需在已使用的函数中添加cv_ 前缀即可。例如使用cv_plot_bbox 而不是plot_bbox

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 2016-03-14
      相关资源
      最近更新 更多