results = [] if polygon.shape[0] > 0: for label, score, seg in zip(class_ids, conf, polygon): # 将多边形的点坐标转换为列表形式 points = [[float(coord[0]), float(coord[1])] for coord in contour.squeeze().tolist()] if len(contour) > 0 else [] results.append({ "confidence": str(score), "label": labels.get(label, "unknown"), "points": points, "type": "polygon" 检查一下
时间: 2024-02-14 08:30:20 浏览: 27
代码中的 `points` 部分在使用 `contour` 变量时出现错误。根据之前的代码,应该使用 `seg` 变量来转换多边形的点坐标。以下是修改后的代码:
```python
results = []
if polygon.shape[0] > 0:
for label, score, seg in zip(class_ids, conf, polygon):
# 将多边形的点坐标转换为列表形式
points = [[float(coord[0]), float(coord[1])] for coord in seg]
results.append({
"confidence": str(score),
"label": labels.get(label, "unknown"),
"points": points,
"type": "polygon"
})
```
现在,代码将正确地将多边形的点坐标转换为浮点数类型,并将其作为 `points` 字段的值添加到结果中。
相关问题
detections = sv.Detections.from_yolov8(yolo_results) detections = detections[detections.confidence > threshold] boxes = detections.xyxy conf = detections.confidence class_ids = detections.class_id results = [] if polygon.shape[0] > 0: for label, score, seg in zip(class_ids, conf, polygon): # 将多边形的点坐标转换为列表形式 points = [[float(coord[0]), float(coord[1])] for coord in seg] results.append({ "confidence": str(score), "label": labels.get(label, "unknown"), "points": points, "type": "polygon",})帮我改
根据您提供的代码片段,以下是修改后的代码:
```python
detections = sv.Detections.from_yolov8(yolo_results)
detections = detections[detections.confidence > threshold]
boxes = detections.xyxy
conf = detections.confidence
class_ids = detections.class_id
results = []
if polygon.shape[0] > 0:
for label, score, seg in zip(class_ids, conf, polygon):
# 将多边形的点坐标转换为列表形式
points = [[float(coord[0]), float(coord[1])] for coord in seg]
results.append({
"confidence": str(score),
"label": labels.get(label, "unknown"),
"points": points,
"type": "polygon",
})
```
在这个修改后的代码中,将多边形的点坐标转换为列表形式,并将结果添加到 `results` 列表中。每个结果字典包含了置信度、标签、多边形点坐标和类型信息。
请注意,修改后的代码假设 `polygon`、`labels` 和 `threshold` 变量已经在之前的代码中定义或赋值。请确保这些变量的正确性,并根据需要进行调整。
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 segments = sv.PolygonZone.from_yolov8(yolo_results) segments = segments[segments.confidence > threshold] polygon = segments.xy conf = segments.confidence class_ids = segments.class_id results = [] if polygon.shape[0] > 0: for label, score, seg in zip(class_ids, conf, polygon): results.append({ "confidence": str(score), "label": labels.get(label, "unknown"), "points": , "type": "polygon",}) 改成分割的
如果你想将多边形的点坐标添加到结果中,可以将代码修改为以下形式:
```python
results = []
if polygon.shape[0] > 0:
for label, score, seg in zip(class_ids, conf, polygon):
# 将多边形的点坐标转换为列表形式
points = seg.tolist()
results.append({
"confidence": str(score),
"label": labels.get(label, "unknown"),
"points": points,
"type": "polygon"
})
```
这样修改后,每个多边形对象的点坐标将被添加到结果中作为 `points` 字段的值。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)