Enrollments closing soon for Post Graduate Certificate Program in Applied Data Science & AI By IIT Roorkee | 3 Seats Left
Apply NowLogin using Social Account
     Continue with GoogleLogin using your credentials
Now that we have got an idea about mask processing from the previous slide, let us take a closer look at the code by understanding each small snippet:
1.
for i in range(0, boxes.shape[2]): #For each detection
classID = int(boxes[0, 0, i, 1]) #Class ID
confidence = boxes[0, 0, i, 2] #Confidence scores
if confidence > threshold:
(H, W) = img.shape[:2]
box = boxes[0, 0, i, 3:7] * np.array([W, H, W, H]) #Bounding box
(startX, startY, endX, endY) = box.astype("int")
boxW = endX - startX
boxH = endY - startY
for i in range(0, boxes.shape[2])
.classID
and confidence
score of the detection.If the confidence value of the detection is greater than the threshold, we consider it as a valid detection and further proceed to create the mask using the correspong mask polygon for this detection, as follows:
H
and width W
of the img
.boxes[0, 0, i, 3:7]
and normalize the bounding boxes using boxes[0, 0, i, 3:7] * np.array([W, H, W, H])
. Thus the normalized bounding boxes of the currect valid detection is stored in box
.boxW
and height boxH
of the bounding box.2.
mask = masks_polygons[i, classID]
plt.imshow(mask)
plt.show()
print("Shape of individual mask", mask.shape)
mask = masks_polygons[i, classID]
.3.
mask = cv2.resize(mask, (boxW, boxH), interpolation=cv2.INTER_CUBIC)
print ("Mask after resize", mask.shape)
mask = (mask > threshold)
(boxW, boxH)
.mask
after resizing it. mask = (mask > threshold)
.4.
roi = img[startY:endY, startX:endX][mask]
mask
in the img
.5.
color = COLORS[classID]
blended = ((0.4 * color) + (0.6 * roi)).astype("uint8")
img[startY:endY, startX:endX][mask] = blended
roi
and the random color color
we have generated for each classID.img[startY:endY, startX:endX][mask] = blended
, we impart this blended color on the ROI of the image, thus forming the view of the object being overlapped with the color of ClassID.6.
color = COLORS[classID]
color = [int(c) for c in color]
print (LABELS[classID], color)
cv2.rectangle(img, (startX, startY), (endX, endY), color, 2)
text = "{}: {:.4f}".format(LABELS[classID], confidence)
cv2.putText(img, text, (startX, startY - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
img
, mark the detect labels along with their corresponding confidence values:
ClassID
.cv2.rectangle
using the coordinate values startX, startY, endX, endY
of the bounding boxes of the valid detections.cv2.putText
.Set threshold
to 0.9
.
threshold = 0.9
Use the code below to do the same as described above:
for i in range(0, boxes.shape[2]): #For each detection
classID = int(boxes[0, 0, i, 1]) #Class ID
confidence = boxes[0, 0, i, 2] #Confidence scores
if confidence > threshold:
(H, W) = img.shape[:2]
box = boxes[0, 0, i, 3:7] * np.array([W, H, W, H]) #Bounding box
(startX, startY, endX, endY) = box.astype("int")
boxW = endX - startX
boxH = endY - startY
# extract the pixel-wise segmentation for the object, and visualize the mask
mask = masks_polygons[i, classID]
plt.imshow(mask)
plt.show()
print ("Shape of individual mask", mask.shape)
# resize the mask such that it's the same dimensions of
# the bounding box, and interpolation gives individual pixel positions
mask = cv2.resize(mask, (boxW, boxH), interpolation=cv2.INTER_CUBIC)
print ("Mask after resize", mask.shape)
# then finally threshold to create a *binary* mask
mask = (mask > threshold)
print ("Mask after threshold", mask.shape)
# extract the ROI of the image but *only* extracted the
# masked region of the ROI
roi = img[startY:endY, startX:endX][mask]
print ("ROI Shape", roi.shape)
# grab the color used to visualize this particular class,
# then create a transparent overlay by blending the color
# with the ROI
color = COLORS[classID]
blended = ((0.4 * color) + (0.6 * roi)).astype("uint8")
# Change the colors in the original to blended color
img[startY:endY, startX:endX][mask] = blended
color = COLORS[classID]
color = [int(c) for c in color]
print (LABELS[classID], color)
cv2.rectangle(img, (startX, startY), (endX, endY), color, 2)
text = "{}: {:.4f}".format(LABELS[classID], confidence)
cv2.putText(img, text, (startX, startY - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
Finally visualize the image img
with the detected objects being highlighted with their corresponding Masks, Bounding Boxes, Class Labels and Confidence Scores.
plt.imshow(fixColor(img))
Want to create exercises like this yourself? Click here.
Note - Having trouble with the assessment engine? Follow the steps listed here
Loading comments...