物体检测与描述
模力方舟上通过 Florence-2-large 模型提供了图像标记的能力。
Florence-2-large 是微软出品的开源多功能图像标记模型,可以辅助标记图像内容、生成图像描述、识别目标等。得益于大模型架构,Florence-2 还支持使用提示词定向标记图中特定对象。
该模型仅支持英文输入输出,因此无论是输入的提示词还是识别的内容和输出的结果都将是英文的,任何其它语言的信息都会导致模型出现无法预计的输出结果。
下面介绍如何在代码中使用 Florence-2-large 模型进行图像标记。
准备工作
首先获取您的 访问令牌 ,然后可定义请求函数如下。
接口的使用以 Python 代码为例,下面将分别演示“图像描述”与“目标识别”两个功能项的使用方法。在开始前先定义请求函数如下:
import requests
headers = {
"Authorization": "Bearer <your api token>",
}
#用于请求图像描述的url
url_caption = "https://api.moark.com/v1/images/caption"
#用于请求目标识别的url
url_object_detection = "https://api.moark.com/v1/images/object-detection"
def query(url, payload):
files = {
"image": (payload["image"], open(payload["image"], "rb"))
}
data = {key: payload[key] for key in payload if key not in files}
response = requests.post(url, headers=headers, files=files, data=data)
return response.json()
下文中使用的图片示例为:

1. 图像描述生成
该功能可以获取用户输入的图片的自然语言描述。 使用上文的 query 函数,发起请求如下:
output = query(url_caption, {
"model": "Florence-2-large",
"image": "path/to/image.jpg",
"caption_level": 0
})
请求参数说明:
image:需要进行描述的图片,仅支持输入一张图片。caption_level:需要描述图片的详细程度,支持0、1、2三个等级,等级越高则描述得越详细,输出的字数越多。等级可根据需求进行调整,若不填写则采用默认等级 0 。
设置 caption_level=0 时输出如下:
A woman and a little girl walking down a dirt road.
设置 caption_level=1 时输出如下:
The image shows a woman and a little girl walking down a dirt road, hand in hand, with a horse in the background. The sky is filled with clouds and the ground is covered with lush green grass. The image is animated, giving it a whimsical feel.
设置 caption_level=2 时输出如下:
The image is an illustration of a mother and daughter walking hand in hand in a field. The mother is wearing a long white dress with pink flowers on it and has long blonde hair. She is holding the hand of the daughter, who is also wearing a purple dress. They are both smiling and appear to be enjoying each other's company. In the background, there is a fence with wooden posts and a horse grazing on the grass. The sky is filled with fluffy white clouds and the sun is shining brightly, creating a warm glow. The field is covered in yellow flowers and there are hills in the distance. The overall mood of the image is peaceful and serene.
从结果中可以直观的感受到 caption_level 级别越高,描述结果越详细,您可根据需求选择不同的等级。