python - 如何在python中将azure自定义视觉注释格式更改为yolo v8格式

标签 python azure computer-vision yolov8 microsoft-custom-vision

我已编写脚本,使用以下 API 从 azure 自定义视觉下载带注释的图像。 API 给出检测到车牌的区域及其相应边界框的概率分数。由于图像中可能有多辆车,因此会有多种概率。

# Code to get bounding box details and corresponding probability for single image
url="url to your custom vision model"
headers={'content-type':'header value'}
r =requests.post(url,data=open(r"path to image.jpg","rb"),headers=headers)
print(r.content)
{"id":"someID","project":"project ID","iteration":"iteration ID","created":"2023-08-03T13:03:12.831Z","predictions":[{"probability":0.7684734,"tagId":"tage_ID","tagName":"License_plate","boundingBox":{"left":0.4307156,"top":0.5326757,"width":0.15810284,"height":0.129749}},{"probability":0.026557693,"tagId":"tag_ID","tagName":"License_plate","boundingBox":{"left":0.47290865,"top":0.5626349,"width":0.07031235,"height":0.066358685}}

上面是输出 在这里,我尝试使用以下代码可视化图像中的边界框。

path = r"path to image.jpg"
image = cv2.imread(path)
image_height, image_width,channel = image.shape
pred = json.loads(r.content)
for i in pred["predictions"]:
    if i["probability"] > 0.5:
        print(i['boundingBox'])
        left = i['boundingBox']['left']* image_width
        top = i['boundingBox']['top'] * image_height
        width = i['boundingBox']['width'] * image_width
        height = i['boundingBox']['height'] * image_height
color = (255, 0, 0)
#drawing bounding box
cv2.rectangle(image,(int(left), int(top)), (int(left + width), int(top + height)) ,(0, 0, 255), 5)
license_plate_crop = image[int(left):int(top), int(left + width):int(top + height), :]
cv2.imshow('image', license_plate_crop)
cv2.waitKey(0)

当我可视化裁剪后的图像时,我得到的是车牌以外的不同区域。

在自定义视觉提供的 UI 上测试相同图像时,它可以正确显示车牌。
我认为在将图像转换为 yolo 格式时我缺少一些东西。

在 yolo 格式中,我们得到 x1,y1,x2,y2,我们可以使用以下代码绘制边界框并仅裁剪该区域。

cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 5)
license_plate_crop = image[int(y1):int(y2), int(x1):int(x2), :]

所以在这里我试图获取仅车牌的裁剪图像,并且自定义视觉格式与 yolo 格式不同,其中自定义视觉格式为

{"left":0.4307156,"top":0.5326757,"width":0.15810284,"height":0.129749}

yolo 格式为x1,y1,x2,y2
请协助将 azure Vision 格式转换为 yolo 格式。
另外如果有任何API或方法请添加。

最佳答案

要裁剪 cv2 图像,您需要描述边界框左上角和右下角的坐标。这些坐标已经以绝对xyxy的格式包含在yolo结果框中。

对于 Azure 自定义视觉,我们的坐标略有不同:标准化形式的左侧、顶部、宽度和高度(除以图像宽度和高度)。 'left' 和 'top' 是边界框的左上角坐标,但 'width' 和 'height' 是边界框边的实际长度,而不是框右下角的坐标。要获取它们并裁剪图像,请执行以下操作:

# having left, top, width, and height values from your script:
'''
left = i['boundingBox']['left']* image_width
top = i['boundingBox']['top'] * image_height
width = i['boundingBox']['width'] * image_width
height = i['boundingBox']['height'] * image_height
'''
y1 = top 
x1 = left
y2 = top + height
x2 = left + width

license_plate_crop = image[int(y1):int(y2), int(x1):int(x2), :]

关于python - 如何在python中将azure自定义视觉注释格式更改为yolo v8格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76828858/

相关文章:

python - multiprocessing.pool.RemoteTraceback,TypeError : Incorrect type of self (must be 'Feature2D' or its derivative)

azure - 核心 WebJob 未观察到应用程序设置

Azure DevOps VSTest 任务 - appconfig 设置

python - key 错误 : 'IDENTITY_ENDPOINT' error in Azure environment

python - 使用图像的局部平均颜色来减少闪电差异

python - Python 中正则表达式的替代品

python - 如何使用模型/ View / Controller 方法制作 GUI?

python - 谷歌 API : getting Credentials from refresh token with oauth2client. 客户端

python - 使用 Python OpenCV,您将如何提取特定颜色边界框内的图像区域?

python - 创建相机图像的无失真俯 View