You need to enable JavaScript to run this app.
文档中心
复制全文
下载 pdf
SDK 参考
图片检测
复制全文
下载 pdf
图片检测
您可以基于大模型应用防火墙 SDK 调用接口对图片内容进行检测。本文提供 Python、Java、Go、PHP 开发语言的图片内容检测示例代码,供您参考。​
注意事项​
  • 版本要求:​
无代理服务​
​
Python
Java
Go
PHP
​
​
from volcenginesdkllmshield import *​
import base64​
​
def load_image_content(image_file):​
with open(image_file, 'rb') as f:​
img_bytes = f.read()​
base64_bytes = base64.b64encode(img_bytes).decode('utf-8')​
return base64_bytes, len(base64_bytes)​
​
def main():​
# 配置信息(请替换为实际值)​
myUrl = "https://cn-beijing.sdk.access.llm-shield.omini-shield.com" # 根据大模型应用防火墙实例所在地域替换相应的地域信息​
ak = "YOUR AK" # 火山账号的 AK​
sk = "YOUR SK" # 火山账号的 SK​
appid = "app-d3r06khsqn9s73da****" # 资产的 AppId,可在控制台资产管理页面获取​
region = "cn-beijing" # 大模型应用防火墙实例所在地域​
​
TIMEOUT = 50 # 超时时间(秒)​
​
image_file = "/Users/Downloads/192750.jpeg" # 待检测图片的本地存储路径​
image_content, size = load_image_content(image_file)​
# 确保图片大小不超过 10 MB​
if size > 1010241024: ​
print(f"请求失败: {size/1024}KB")​
return​
try:​
# 发送请求并获取响应​
client = ClientV2(myUrl, ak, sk, region, TIMEOUT)​
# 构建请求(使用 pydantic 模型)​
request = ModerateV2Request(​
scene=appid, # 资产的 AppId​
message=MessageV2(​
role="user", # 检测的内容来源,user-用户提示词;assistant-模型响应内容​
content=image_content, # 需要检测的图片内容​
content_type=ContentTypeV2.IMAGE​
),​
# history=[​
# MessageV2(​
# role="system", # 上下文内容来源,user-用户提示词;assistant-模型响应内容;system-系统默认提示词​
# content="你是一个智能助手",​
# content_type=ContentTypeV2.TEXT​
# ),​
# MessageV2(​
# role="assistant", # 上下文内容来源,user-用户提示词;assistant-模型响应内容;system-系统默认提示词​
# content="Day 1:抵达昆明・滇池与石林初探...",​
# content_type=ContentTypeV2.TEXT​
# )​
# ]​
)​
​
response = client.Moderate(request)​
​
# 使用 pydantic 的内置序列化方法​
print("审核结果(JSON格式):")​
print(response.model_dump_json(by_alias=True, indent=2)) # pydantic 模型直接转 JSON​
​
except Exception as e:​
print(f"请求失败: {e}")​
​
if __name__ == "__main__":​
main()​
​
​
有代理服务​
​
Python
Java
Go
PHP
​
​
from volcenginesdkllmshield import *​
import base64​
​
def load_image_content(image_file):​
with open(image_file, 'rb') as f:​
img_bytes = f.read()​
base64_bytes = base64.b64encode(img_bytes).decode('utf-8')​
return base64_bytes, len(base64_bytes)​
​
def main():​
# 配置信息(请替换为实际值)​
myUrl = "https://cn-beijing.sdk.access.llm-shield.omini-shield.com" # 根据大模型应用防火墙实例所在地域替换相应的地域信息​
ak = "YOUR AK" # 火山账号的 AK​
sk = "YOUR SK" # 火山账号的 SK​
appid = "app-d3r06khsqn9s73da****" # 资产的 AppId,可在控制台资产管理页面获取​
region = "cn-beijing" # 大模型应用防火墙实例所在地域​
​
TIMEOUT = 50 # 超时时间(秒)​
​
# 设置代理服务字典,格式为 {"协议": "代理地址"}​
proxy_dict = {"http": "http://10.37.**.**:8888"}​
​
image_file = "/Users/Downloads/192750.jpeg" # 待检测图片的本地存储路径​
image_content, size = load_image_content(image_file)​
# 确保图片大小不超过 10 MB​
if size > 1010241024: ​
print(f"请求失败: {size/1024}KB")​
return​
try:​
# 发送请求并获取响应​
client = ClientV2(myUrl, ak, sk, region, TIMEOUT)​
​
# 设置代理地址​
client.SetProxy(proxy_dict)​
# 设置最大连接数,例如 100​
client.SetConnMax(100) ​
​
# 构建请求(使用 pydantic 模型)​
request = ModerateV2Request(​
scene=appid, # 资产的 AppId​
message=MessageV2(​
role="user", # 检测的内容来源,user-用户提示词;assistant-模型响应内容​
content=image_content, # 需要检测的图片内容​
content_type=ContentTypeV2.IMAGE​
),​
# history=[​
# MessageV2(​
# role="system", # 上下文内容来源,user-用户提示词;assistant-模型响应内容;system-系统默认提示词​
# content="你是一个智能助手",​
# content_type=ContentTypeV2.TEXT​
# ),​
# MessageV2(​
# role="assistant", # 上下文内容来源,user-用户提示词;assistant-模型响应内容;system-系统默认提示词​
# content="Day 1:抵达昆明・滇池与石林初探...",​
# content_type=ContentTypeV2.TEXT​
# )​
# ]​
)​
​
response = client.Moderate(request)​
​
# 使用 pydantic 的内置序列化方法​
print("审核结果(JSON格式):")​
print(response.model_dump_json(by_alias=True, indent=2)) # pydantic 模型直接转 JSON​
​
except Exception as e:​
print(f"请求失败: {e}")​
​
if __name__ == "__main__":​
main()​
​
​
​
最近更新时间:2026.04.27 19:47:53
这个页面对您有帮助吗?
有用
有用
无用
无用