import json import base64 from PIL import Image import io import cv2 import numpy as np from ultralytics import YOLO import supervision as sv def init_context(context): context.logger.info("Init context... 0%") model_path = "yolov8m-seg.pt" # YOLOV8模型放在nuclio目录下构建 model = YOLO(model_path) # Read the DL model context.user_data.model = model context.logger.info("Init context...100%") def handler(context, event): context.logger.info("Run yolo-v8-seg 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.xy conf = detections.confidence class_ids = detections.class_id results = [] if masks.shape[0] > 0: for label, score, mask in zip(class_ids, conf, masks): # 将mask转换为轮廓 contours, _ = cv2.findContours(mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for contour in contours: points = [] for point in contour: x = point[0][0] y = point[0][1] points.append([x, y]) results.append({ "confidence": str(score), "label": labels.get(label, "unknown"), "points": points, "type": "polygon",}) return context.Response(body=json.dumps(results), headers={}, content_type='application/json', status_code=200)不用supervision 包 用别的方式解析
时间: 2024-02-14 10:29:51 浏览: 153
python PIL/cv2/base64相互转换实例
如果你不想使用 `supervision` 包,可以使用其他方式解析 `masks`。
以下是修改后的代码示例,使用 OpenCV 库中的 `cv2.findContours()` 函数和 `cv2.drawContours 函数来解析 `masks` 并结果列表:
```python
import json
import base64
from PIL import Image
import io
import cv2
import numpy as np
from ultralytics import YOLO
def init_context(context):
context.logger.info("Init context... 0%")
model_path = "yolov8m-seg.pt" # YOLOV8模型放在nuclio目录下构建
model = YOLO(model_path)
# Read the DL model
context.user_data.model = model
context.logger.info("Init context...100%")
def handler(context, event):
context.logger.info("Run yolo-v8-seg 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 = yolo_results.pred
results = []
for detection in detections:
class_id = int(detection[5])
score = detection[4]
mask = detection[6]
# 将mask转换为轮廓
_, contours, _ = cv2.findContours(mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
points = []
for point in contour:
x = point[0][0]
y = point[0][1]
points.append([x, y])
results.append({
"confidence": str(score),
"label": labels[class_id],
"points": points,
"type": "polygon"
})
return context.Response(body=json.dumps(results), headers={},
content_type='application/json', status_code=200)
```
在这个修改后的代码中,我假设你已经正确导入了所需的库和模块,并且使用了 `YOLO` 类来获取预测结果。
我修改了 `handler` 函数中的代码,将 `detections` 对象中的每个检测结果进行遍历。对于每个检测结果,我提取了类别ID、置信度和掩码。然后,使用 `cv2.findContours()` 函数将掩码转换为轮廓,并使用 `cv2.drawContours()` 函数绘制轮廓。
最后,我将每个检测结果的类别、置信度和轮廓点添加到结果列表中。
请注意,这只是一个示例代码,可能需要根据你的实际需求进行调整和优化。
希望这能帮助到你!如果还有其他问题,请随时提问。
阅读全文