xxxxxxxxxx
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
# load image
img = plt.imread('image.jpg')
# create figure and axis
fig, ax = plt.subplots()
# display image
ax.imshow(img)
# create bounding box coordinates
xmin, ymin, width, height = 50, 100, 150, 200
# create rectangle patch
rect = Rectangle((xmin, ymin), width, height, linewidth=1, edgecolor='r', facecolor='none')
# add patch to axis
ax.add_patch(rect)
# display image with bounding box
plt.show()
xxxxxxxxxx
# cv2.rectangle(img, pt1, pt2, color, thickness, lineType, shift)
cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 2)
"""
x1,y1 ------
| |
| |
| |
--------x2,y2
"""
xxxxxxxxxx
## drawing b.box for given coutour
contours, _ = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
rect = cv2.boundingRect(c)
if rect[2] < 100 or rect[3] < 100: continue
print cv2.contourArea(c)
x,y,w,h = rect
cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
cv2.putText(im,'Moth Detected',(x+w+10,y+h),0,0.3,(0,255,0))
cv2.imshow("Show",im)
cv2.waitKey()
cv2.destroyAllWindows()
xxxxxxxxxx
from keras_cv import visualization
visualization.plot_bounding_box_gallery(
image_batch,
value_range=(0, 255),
rows=1,
cols=1,
y_pred=y_pred,
scale=5,
font_scale=0.7,
bounding_box_format="xywh",
class_mapping=class_mapping,
)
# check out source for more details
# also check https://aihints.com/how-to-draw-bounding-box-in-opencv-python/