def handler(context, event): context.logger.info("Run yolo-v8 model") data = event.body buf = io.BytesIO(base64.b64decode(data["image"])) threshold = float(data.get("threshold", 0.35)) context.user_data.model.conf = threshold image = Image.open(buf) yolo_results = context.user_data.model(image, conf=threshold)[0] labels = yolo_results.names detections = sv.Detections.from_yolov8(yolo_results) detections = detections[detections.confidence > threshold] masks = detections.mask conf = detections.confidence class_ids = detections.class_id results = [] if masks.shape[0] > 0: for label, score, seg in zip(class_ids, conf, masks): results.append({ "confidence": str(score), "label": labels.get(label, "unknown"), "points": , "type": "rectangle",})根据上面的改这个代码处理yolov8推理出来的结果会带有检测框和掩码 提取掩码
时间: 2024-04-17 13:24:23 浏览: 105
要提取分割掩码,你可以按照以下方式修改代码:
```python
results = []
if masks.shape[0] > 0:
for label, score, seg in zip(class_ids, conf, masks):
# 解析分割掩码
mask = seg.argmax(axis=0) # 取最大值的索引,将二维掩码还原为一维
mask = mask.astype(np.uint8) # 将掩码转换为无符号整数类型
mask_binary = np.where(mask > 0, 255, 0) # 将掩码转换为二值图像
# 获取掩码边界框
contours, _ = cv2.findContours(mask_binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
x, y, w, h = cv2.boundingRect(contours[0]) if len(contours) > 0 else (0, 0, 0, 0)
xtl = x
ytl = y
xbr = x + w
ybr = y + h
# 添加结果到列表中
results.append({
"confidence": str(score),
"label": labels.get(label, "unknown"),
"points": [xtl, ytl, xbr, ybr],
"type": "rectangle",
"mask": mask.tolist() # 将分割掩码转换为列表形式
})
```
在这个修改后的代码中,我们首先对分割掩码进行解析处理,将其转换为二值图像,并计算出掩码的边界框。然后,将边界框的坐标添加到结果字典中,并将分割掩码转换为列表形式,并添加到结果字典中。
请注意,这里假设`masks`是一个包含分割掩码的形状数组(n,W,H)。如果`masks`不是这种形式,请根据实际情况进行修改。
阅读全文