GluonCV对象检测器-批处理图像

时间:2020-04-22 10:52:14

标签: python object-detection mxnet

我正在Ubuntu18.04上使用Python在网络摄像头流或视频文件上运行对象检测。到目前为止,我一直在使用以下代码逐帧运行推理:

Private Sub OptionButton1_Change()
If OptionButton1.Value = True Then
    Sheet1.Range("B1").Value = 1
Else
    Sheet1.Range("B1").Value = 0
End If
End Sub

Private Sub OptionButton2_Change()
If OptionButton2.Value = True Then
    Sheet1.Range("B2").Value = 1
Else
    Sheet1.Range("B2").Value = 0
End If
End Sub

Private Sub OptionButton3_Change()
If OptionButton3.Value = True Then
    Sheet1.Range("B3").Value = 1
Else
    Sheet1.Range("B3").Value = 0
End If
End Sub

我想尝试一个替代版本,我不逐帧执行检测,而是对成批帧进行检测。
我尝试过这种方式:

  • 在开头创建一个空列表;
  • (在图像预处理之后)将每帧追加到列表中;
  • 经过N帧(例如N = 50)后,将列表转换为mx.nd.array;
  • 将所述数组输入模型;

因此,使用以下代码:

def main():

    ctx = mx.gpu(0)

    # Load pretrained model
    net = gcv.model_zoo.get_model('ssd_512_mobilenet1.0_coco', pretrained=True)
    net.hybridize()

    # Load the webcam handler
    cap = cv2.VideoCapture(0)


    count_frame = 0


    while(True):
        print(f"Frame: {count_frame}")

        # Load frame from the camera
        ret, frame = cap.read()
        # print(type(frame))

        if (cv2.waitKey(25) & 0xFF == ord('q')) or (ret == False):
            cv2.destroyAllWindows()
            cap.release()
            print("Done!!!")
            break

        # Image pre-processing
        frame = mx.nd.array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)).astype('uint8')
        frame_nd, frame_np = gcv.data.transforms.presets.ssd.transform_test(frame, short=512, max_size=700)

        # Run frame through network
        frame_nd = frame_nd.as_in_context(ctx)
        class_IDs, scores, bounding_boxes = net(frame_nd)


        # Display result with cv
        img = gcv.utils.viz.cv_plot_bbox(frame_np, bounding_boxes[0], scores[0], class_IDs[0], thresh=0.3, class_names=net.classes)
        gcv.utils.viz.cv_plot_image(img)

        count_frame += 1



    cv2.destroyAllWindows()
    cap.release()



if __name__ == "__main__":
    main()


问题在于,当我运行它时,执行到行时完全卡住了:

def main():

    ctx = mx.gpu(0)

    # Load a pretrained model
    net = gcv.model_zoo.get_model('ssd_512_mobilenet1.0_coco', pretrained=True)
    net.hybridize()

    # Load the webcam handler
    cap = cv2.VideoCapture(0)

    count_frame = 0

    batch = []

    while(True):
        print(f"Frame: {count_frame}")

        # Load frame from the camera
        ret, frame = cap.read()

        if (cv2.waitKey(25) & 0xFF == ord('q')) or (ret == False):
            cv2.destroyAllWindows()
            cap.release()
            print("Done!!!")
            break

        # Image pre-processing
        frame = mx.nd.array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)).astype('uint8')
        frame_nd, frame_np = gcv.data.transforms.presets.ssd.transform_test(frame, short=512, max_size=700)
        batch.append(frame_nd)


        if (count_frame>0) and (count_frame%50 == 0):
            print("\tStarting detection.")
            batch_nd = mx.nd.array(batch)
            batch_nd = batch_nd.as_in_context(ctx)
            class_IDs, scores, bounding_boxes = net(batch)
            print("\tDetection performed.")
        count_frame += 1



    cv2.destroyAllWindows()
    cap.release()




if __name__ == "__main__":
    main()

供参考,这是输出:

batch_nd = mx.nd.array(batch)

任何提示我在做什么错?有没有更好的方法可以将成批的帧发送到模型?

0 个答案:

没有答案
相关问题