pythonopencvdrawrectanglestreamlit

Rectangles don't show in the image when using cv2.rectangle() in streamlit web app


I am trying to showcase my deep learning work with streamlit, which involves drawing rectangles over the objects detected in the image. However no rectangles can be seen on it. So what's wrong with it?enter image description here

Here's the code snippet:

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))

Solution

  • cv2.rectangle returns an image with the rectangle on it (it doesn`t do it in place). So it should be:

    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))