116 def report(self, outimg, helper, overlay, idle):
119 if outimg
is not None:
120 jevois.LFATAL(
"Sorry, legacy mode not supported by PyPostYOLOv8seg.py")
123 if helper
is not None and overlay:
125 x1, y1, x2, y2, score, cla = self.
boxes[i]
126 label, color = self.
getLabel(int(cla), score);
131 convexhull = cv2.convexHull(approx)
132 helper.drawPoly(convexhull, color,
True)
134 helper.drawPoly(approx, color,
False)
138 helper.drawRect(x1, y1, x2, y2, color & 0xffffffff,
False)
139 helper.drawText(x1 + 3, y1 + 3, label, color & 0xffffffff)
143 x, protos = preds[0], preds[1]
147 x = np.einsum(
"bcn->bnc", x)
150 x = x[np.amax(x[..., 4:-nm], axis=-1) > self.
conf.get()]
154 x = np.c_[x[..., :4], np.amax(x[..., 4:-nm], axis=-1), np.argmax(x[..., 4:-nm], axis=-1), x[..., -nm:]]
157 x = x[cv2.dnn.NMSBoxes(x[:, :4], x[:, 4], self.
conf.get(), self.
iou.get())]
162 x[..., [0, 1]] -= x[..., [2, 3]] / 2
163 x[..., [2, 3]] += x[..., [0, 1]]
166 x[..., [0, 2]] = x[:, [0, 2]].clip(0, blobsize[1])
167 x[..., [1, 3]] = x[:, [1, 3]].clip(0, blobsize[0])
170 masks = self.
process_mask(protos[0], x[:, 6:], x[:, :4], blobsize)
176 for i
in range(x.shape[0]):
177 x[i, [0, 1]] = preproc.b2i(x[i, 0], x[i, 1], 0)
178 x[i, [2, 3]] = preproc.b2i(x[i, 2], x[i, 3], 0)
181 for s
in range(len(segments)):
182 for i
in range(segments[s].shape[0]):
183 x1, y1 = segments[s][i, [0, 1]]
184 x1, y1 = preproc.b2i(float(x1), float(y1), 0)
185 segments[s][i, [0, 1]] = [int(x1), int(y1)]
187 return x[..., :6], segments, masks
218 It takes a mask and a bounding box, and returns a mask that is cropped to the bounding box. (Borrowed from
219 https://github.com/ultralytics/ultralytics/blob/465df3024f44fa97d4fad9986530d5a13cdabdca/ultralytics/utils/ops.py#L599)
222 masks (Numpy.ndarray): [n, h, w] tensor of masks.
223 boxes (Numpy.ndarray): [n, 4] tensor of bbox coordinates in relative point form.
226 (Numpy.ndarray): The masks are being cropped to the bounding box.
228 n, h, w = masks.shape
229 x1, y1, x2, y2 = np.split(boxes[:, :,
None], 4, 1)
230 r = np.arange(w, dtype=x1.dtype)[
None,
None, :]
231 c = np.arange(h, dtype=x1.dtype)[
None, :,
None]
232 return masks * ((r >= x1) * (r < x2) * (c >= y1) * (c < y2))
237 Takes the output of the mask head, and applies the mask to the bounding boxes.
238 This produces masks of higher quality but is slower.
239 (Borrowed from https://github.com/ultralytics/ultralytics/blob/465df3024f44fa97d4fad9986530d5a13cdabdca/
240 ultralytics/utils/ops.py#L618)
243 protos (numpy.ndarray): [mask_dim, mask_h, mask_w].
244 masks_in (numpy.ndarray): [n, mask_dim], n is number of masks after nms.
245 bboxes (numpy.ndarray): bboxes re-scaled to original image shape.
246 im0_shape (tuple): the size of the input image (h,w,c).
249 (numpy.ndarray): The upsampled masks.
251 c, mh, mw = protos.shape
252 masks = np.matmul(masks_in, protos.reshape((c, -1))).reshape((-1, mh, mw)).transpose(1, 2, 0)
253 masks = np.ascontiguousarray(masks)
255 masks = np.einsum(
"HWN -> NHW", masks)
257 return np.greater(masks, 0.5)
263 Takes a mask, and resizes it to the original image size. (Borrowed from
264 https://github.com/ultralytics/ultralytics/blob/465df3024f44fa97d4fad9986530d5a13cdabdca/ultralytics/utils/ops.py#L305)
267 masks (np.ndarray): resized and padded masks/images, [h, w, num]/[h, w, 3].
268 im0_shape (tuple): the original image shape.
269 ratio_pad (tuple): the ratio of the padding to the original image.
272 masks (np.ndarray): The masks that are being returned.
274 im1_shape = masks.shape[:2]
275 if ratio_pad
is None:
276 gain = min(im1_shape[0] / im0_shape[0], im1_shape[1] / im0_shape[1])
277 pad = (im1_shape[1] - im0_shape[1] * gain) / 2, (im1_shape[0] - im0_shape[0] * gain) / 2
282 top, left = int(round(pad[1] - 0.1)), int(round(pad[0] - 0.1))
283 bottom, right = int(round(im1_shape[0] - pad[1] + 0.1)), int(round(im1_shape[1] - pad[0] + 0.1))
284 if len(masks.shape) < 2:
285 raise ValueError(f
'"len of masks shape" should be 2 or 3, but got {len(masks.shape)}')
286 masks = masks[top:bottom, left:right]
288 masks, (im0_shape[1], im0_shape[0]), interpolation=cv2.INTER_LINEAR
290 if len(masks.shape) == 2:
291 masks = masks[:, :,
None]