【问题标题】:Rectangles don't show in the image when using cv2.rectangle() in streamlit web app在 streamlit Web 应用程序中使用 cv2.rectangle() 时,矩形不会显示在图像中
【发布时间】:2020-10-18 02:49:11
【问题描述】:

我正在尝试使用 streamlit 展示我的深度学习工作,其中涉及在图像中检测到的对象上绘制矩形。但是,在其上看不到矩形。那么它有什么问题呢?

这里是sn-p的代码:

fig, ax = plt.subplots(1, 1, figsize=(32, 16))
    for box in boxes:
        x1, y1, x2, y2 = box
        cv2.rectangle(img=sample,
                      pt1=(y1, x1),
                      pt2=(y2, x2),
                      color=(0, 0, 255), thickness=3)
    ax.set_axis_off()
    im = ax.imshow(sample)
    st.pyplot()
    st.write("# Results")
    st.dataframe(pd.DataFrame(results))

【问题讨论】:

  • 您可以尝试打印x1, y1, x2, y2 吗?
  • 我试过了,还是不行。

标签: python opencv drawrectangle streamlit


【解决方案1】:

cv2.rectangle 返回一个带有矩形的图像(它不会原地执行)。所以应该是:

fig, ax = plt.subplots(1, 1, figsize=(32, 16))
for box in boxes:
    x1, y1, x2, y2 = box
    sample = cv2.rectangle(img=sample,
                  pt1=(y1, x1),
                  pt2=(y2, x2),
                  color=(0, 0, 255), thickness=3)
ax.set_axis_off()
im = ax.imshow(sample)
st.pyplot()
st.write("# Results")
st.dataframe(pd.DataFrame(results))

【讨论】:

  • 我也试过了,但是出现另一个错误,说“TypeError:dtype对象的图像数据无法转换为float”
  • sample 中有什么内容? (在cv2.rectangle 之前)
  • 已解决,请参考我在你的代码中的修改
猜你喜欢
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 2021-08-30
  • 2012-04-03
  • 2013-11-29
  • 1970-01-01
相关资源
最近更新 更多