Skip to content

Instantly share code, notes, and snippets.

@aishwarya-singh25
Created July 31, 2020 12:29
Show Gist options
  • Save aishwarya-singh25/f21079c78768dcfe82704028ed7db7b8 to your computer and use it in GitHub Desktop.
Save aishwarya-singh25/f21079c78768dcfe82704028ed7db7b8 to your computer and use it in GitHub Desktop.
Non max suppression implementation
import matplotlib.pyplot as plt
import matplotlib.patches as patches
image = plt.imread('index.jpg')
# draw emtpy figure
fig = plt.figure()
# define axis
ax = fig.add_axes([0, 0, 1, 1])
# plot image
plt.imshow(image)
# create rectangular patch
#dog
rect_0 = patches.Rectangle((190, 380), 300, 150, edgecolor='red', facecolor='none', linewidth=2)
rect_1 = patches.Rectangle((300, 420), 150, 210, edgecolor='green', facecolor='none', linewidth=2)
rect_2 = patches.Rectangle((320, 360), 200, 230, edgecolor='blue', facecolor='none', linewidth=2)
#person
rect_3 = patches.Rectangle((390, 50), 300, 330, edgecolor='red', facecolor='none', linewidth=2)
rect_4 = patches.Rectangle((490, 45), 200, 500, edgecolor='green', facecolor='none', linewidth=2)
rect_5 = patches.Rectangle((480, 130),150,400, edgecolor='blue', facecolor='none', linewidth=2)
# add patch
#dog
ax.add_patch(rect_0)
ax.text(188, 376, 'Box0:90%', color='red')
ax.add_patch(rect_1)
ax.text(328, 416, 'Box1:98%', color='green')
ax.add_patch(rect_2)
ax.text(318, 356, 'Box2:82%', color='blue')
#person
ax.add_patch(rect_3)
ax.text(388, 44, 'Box3:87%', color='red')
ax.add_patch(rect_4)
ax.text(492, 33, 'Box4:98%', color='green')
ax.add_patch(rect_5)
ax.text(478, 126, 'Box5:82%', color='blue')
# show figure
plt.show()
import matplotlib.pyplot as plt
import matplotlib.patches as patches
image = plt.imread('index.jpg')
# draw emtpy figure
fig = plt.figure()
# define axis
ax = fig.add_axes([0, 0, 1, 1])
# plot image
plt.imshow(image)
# create rectangular patch
rect_1 = patches.Rectangle((300, 420), 150, 210, edgecolor='green', facecolor='none', linewidth=2)
rect_4 = patches.Rectangle((490, 45), 200, 500, edgecolor='green', facecolor='none', linewidth=2)
# add patch
ax.add_patch(rect_1)
ax.text(328, 416, 'Box1:98%', color='green')
ax.add_patch(rect_4)
ax.text(492, 33, 'Box4:98%', color='green')
# show figure
plt.show()
import torch
from torchvision.ops import nms
boxes = torch.tensor([[190,380,(190+300),(380+150)],
[300,420,(300+150),(420+210)],
[320,360,(320+200),(360+230)],
[390,50,(390+300),(50+330)],
[490,45,(490+200),(45+500)],
[480,130,(480+150),(130+400)]], dtype=torch.float32)
scores = torch.tensor([[0.90],[0.98],[0.82], [0.87],[0.98],[0.82]], dtype=torch.float32)
nms(boxes = boxes, scores = scores, iou_threshold=0.2)
@aishwarya-singh25
Copy link
Author

'bounding_boxes.py' fails! It uses an inexistent image file: 'index.jpg'
If this file exists, where it is and why don't you tell?

Replace that with whatever image you're working on!

@AlkisPis
Copy link

Yes, this is what I did. But I see the phenomenon of missing images too often and that's why I mentioned it. (Usually I don't have that luxury ... You wer unlucky! :))

Sometimes it plays a role what image one uses. And the name 'index.jpg' raises such a suspicion! But since this is not the case for your code, it would be wise to use something like 'any_image' or 'your_image', to make this clear. This is what I do when I post a code in stackoverflow.com and elsewhere.

Anyway, thanks for sharing this code! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment