【问题标题】:Having trouble when implementing CNN+LSTM model that i made into a video实现我制作成视频的 CNN+LSTM 模型时遇到问题
【发布时间】:2021-10-12 11:35:39
【问题描述】:

我正在尝试在视频上测试我的模型以进行预测。

我想使用我的cnn(alexnet)+lstm 模型对我拥有的视频进行预测,但是当它运行时,视频中什么都没有出现。

这是我的代码

vid = cv2.VideoCapture("Data Fix/Data16_133.mp4")

while(vid.isOpened()):
    ret, frame = vid.read()
    vid.set(3, 480)
    vid.set(4, 240)
    start = time.time()
    if ret == True:
        total_frame += 1
        draw = frame.copy()
        draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
        scale_percent = 20 # percent of original size
        width = 224
        height = width
        dim = (width, height)
        frame_set = cv2.resize(draw, dim, interpolation = cv2.INTER_AREA)
        frame_set=np.arange(10*width*height*3).reshape(10,width, height, 3)
        frame_set.reshape(10, width, height, 3).shape
        frame_set = np.expand_dims(frame_set, axis=0)
        result=model.predict_on_batch(frame_set)

        cv2.imshow('Result', result)
        print(result)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break            

vid.release()
cv2.destroyAllWindows()

当我打印结果时,它会一遍又一遍地打印这个值,而cv2.imshow 没有显示任何内容

[[0.0602112  0.01403825 0.3782384  0.5362205  0.01129159]]

有人知道吗? 任何答案将不胜感激

目前我正在尝试this tutorial 制作模型,我放置数据集的方式也相同,不同的是我没有使用MobileNet 迁移学习,我使用AlexNet 模型对其进行了修改。

【问题讨论】:

    标签: python tensorflow keras deep-learning cv2


    【解决方案1】:

    问题是您的结果不是您可以使用OpenCV 显示的图像。您的结果是您的模型的输出,根据共享笔记本,该模型是一个分类模型,代表类概率。我假设您正在尝试预测与视频相对应的某些类别。如果你想看到框架,那么你必须像这样使用它:

    cv2.imshow('frame', frame) # to see the frame
    # below to see the draw
    cv2.imshow('draw', draw)
    

    编辑:如果你想在图像上显示预测的类,那么 执行以下操作

    # Get the predicted class from the result using argmax
    pred_class = np.argmax(result)
    # Here I assume that the index is the desired class like most cases
    
    # Now we will write the class label on the image
    # Set the font and place
    font = cv2.FONT_HERSHEY_SIMPLEX
    org = (50, 50)
    cv2.putText(frame, str(pred_class), org, font, .5, (255,255,255),2,cv2.LINE_AA)
    
    # now just show the frame
    cv2.imshow('frame', frame)
    
    

    【讨论】:

    • 但是如果我只显示框架并只绘制它不会在视频上显示预测吗?
    • @JuniorPython 你必须清楚你想要什么?你想在框架上显示结果类吗?
    • 我的意思是它在帖子中明确表示我想使用我的模型显示预测结果,而不仅仅是显示视频.....
    • 它确实有效,它显示了预测,遗憾的是我尝试其他视频预测没有改变并且总是有相同的类,我认为它来自我的模型谢谢你的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多