华为升腾平台支持运行 YOLOv5 目标检测模型,以下是一个简单的 YOLOv5 案例代码示例:
```python
import numpy as np
from PIL import Image
from atlasutil import ai
from atlasutil import camera
# 加载模型
model = ai.Model("yolov5s.om")
# 打开摄像头
camera.open()
while True:
# 获取摄像头采集的图像
image = camera.capture()
# 图像预处理
image = Image.fromarray(np.uint8(image))
image = image.resize((640, 640))
image_data = np.array(image)
# 图像推理
output = model.infer([image_data])
# 解析推理结果
output = output[0]
boxes, confidences, class_ids = output['boxes'], output['confidences'], output['class_ids']
# 在图像上绘制检测框
for i in range(len(boxes)):
box = boxes[i]
confidence = confidences[i]
class_id = class_ids[i]
if confidence > 0.5:
x, y, w, h = box
left, top, right, bottom = int(x - w / 2), int(y - h / 2), int(x + w / 2), int(y + h / 2)
cv2.rectangle(image_data, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(image_data, f"{class_id}", (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# 显示图像
cv2.imshow('Object Detection', image_data)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 关闭摄像头
camera.close()
```
这段代码使用华为升腾平台的 YOLOv5 模型进行实时目标检测,并在图像上绘制检测框和类别标签。请确保已安装好华为升腾平台的开发套件和相关依赖库,并参考华为开发者社区的文档进行模型部署和推理。