Skip to content

Commit d9cb553

Browse files
authored
Merge pull request #253 from bug-is-zhang/master
Add ci sdk and demo
2 parents e070641 + 5a1953b commit d9cb553

File tree

3 files changed

+251
-8
lines changed

3 files changed

+251
-8
lines changed

demo/ci_audit.py

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
# -*- coding=utf-8
2+
import base64
3+
import time
4+
5+
from qcloud_cos import CosConfig
6+
from qcloud_cos import CosS3Client
7+
8+
import os
9+
import sys
10+
import logging
11+
12+
# 腾讯云COSV5Python SDK, 目前可以支持Python2.6与Python2.7以及Python3.x
13+
14+
# https://cloud.tencent.com/document/product/436/46782
15+
16+
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
17+
18+
# 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在CosConfig中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成
19+
secret_id = os.environ["SECRETID"] # 替换为用户的 SecretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
20+
secret_key = os.environ["SECRETKEY"] # 替换为用户的 SecretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
21+
22+
region = 'ap-chongqing' # 替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
23+
# COS支持的所有region列表参见https://www.qcloud.com/document/product/436/6224
24+
token = None
25+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme='https') # 获取配置对象
26+
client = CosS3Client(config)
27+
28+
29+
bucket_name = 'test-1250000000'
30+
31+
32+
def ci_auditing_video_submit():
33+
response = client.ci_auditing_video_submit(Bucket=bucket_name,
34+
Key="test.mp4",
35+
Callback="http://www.demo.com",
36+
CallbackVersion='Simple',
37+
DetectContent=1,
38+
Mode='Interval',
39+
Count=1,
40+
TimeInterval=1)
41+
print(str(response))
42+
43+
44+
def ci_auditing_video_query():
45+
response = client.ci_auditing_video_query(Bucket=bucket_name, JobID="jobId")
46+
print(response['JobsDetail']['State'])
47+
48+
49+
def ci_auditing_image_batch():
50+
user_info = {
51+
'TokenId': '123456', # 一般用于表示账号信息,长度不超过128字节
52+
'Nickname': '测试', # 一般用于表示昵称信息,长度不超过128字节
53+
'DeviceId': '腾讯云', # 一般用于表示设备信息,长度不超过128字节
54+
'AppId': '12500000', # 一般用于表示 App 的唯一标识,长度不超过128字节
55+
'Room': '1', # 一般用于表示房间号信息,长度不超过128字节
56+
'IP': '127.0.0.1', # 一般用于表示 IP 地址信息,长度不超过128字节
57+
'Type': '测试', # 一般用于表示业务类型,长度不超过128字节
58+
'ReceiveTokenId': '789123', # 一般用于表示接收消息的用户账号,长度不超过128字节
59+
'Gender': '男', # 一般用于表示性别信息,长度不超过128字节
60+
'Level': '100', # 一般用于表示等级信息,长度不超过128字节
61+
'Role': '测试人员', # 一般用于表示角色信息,长度不超过128字节
62+
}
63+
input_info = []
64+
input_info.append({
65+
'Object': 'test.png', # 存储在 COS 存储桶中的图片文件名称
66+
# 'Url': 'http://a-1250000.cos.ap-shanghai.myqcloud.com/image.jpg', # 图片文件的链接地址
67+
# 'Content': base64.b64encode('我是测试'.encode("utf-8")).decode('utf-8'), # 图片文件的内容,需要先经过 base64 编码
68+
# 'Interval': '5', # 截帧频率,GIF 图检测专用,默认值为5,表示从第一帧(包含)开始每隔5帧截取一帧。
69+
# 'MaxFrames': '5', # 最大截帧数量,GIF 图检测专用,默认值为5,表示只截取 GIF 的5帧图片进行审核,必须大于0。
70+
'DataId': 'my data id', # 图片标识,该字段在结果中返回原始内容,长度限制为512字节。
71+
# 'LargeImageDetect': '0', # 对于超过大小限制的图片是否进行压缩后再审核,取值为: 0(不压缩),1(压缩)。默认为0。注:压缩最大支持32M的图片,且会收取压缩费用。
72+
'UserInfo': user_info, # 用户业务字段。
73+
})
74+
input_info.append({
75+
'Object': 'test.png', # 存储在 COS 存储桶中的图片文件名称
76+
# 'Url': 'http://a-1250000.cos.ap-shanghai.myqcloud.com/image.jpg', # 图片文件的链接地址
77+
# 'Content': base64.b64encode('我是测试1'.encode("utf-8")).decode('utf-8'), # 图片文件的内容,需要先经过 base64 编码
78+
# 'Interval': '5', # 截帧频率,GIF 图检测专用,默认值为5,表示从第一帧(包含)开始每隔5帧截取一帧。
79+
# 'MaxFrames': '5', # 最大截帧数量,GIF 图检测专用,默认值为5,表示只截取 GIF 的5帧图片进行审核,必须大于0。
80+
'DataId': 'my data id', # 图片标识,该字段在结果中返回原始内容,长度限制为512字节。
81+
# 'LargeImageDetect': '0', # 对于超过大小限制的图片是否进行压缩后再审核,取值为: 0(不压缩),1(压缩)。默认为0。注:压缩最大支持32M的图片,且会收取压缩费用。
82+
'UserInfo': user_info, # 用户业务字段。
83+
})
84+
85+
freeze = {
86+
'PornScore': '50', # 取值为[0,100],表示当色情审核结果大于或等于该分数时,自动进行冻结操作。不填写则表示不自动冻结,默认值为空。
87+
'AdsScore': '50' # 取值为[0,100],表示当广告审核结果大于或等于该分数时,自动进行冻结操作。不填写则表示不自动冻结,默认值为空。
88+
}
89+
response = client.ci_auditing_image_batch(Bucket=bucket_name,
90+
Input=input_info,
91+
BizType='', # 表示审核策略的唯一标识
92+
Async=0, # 是否异步进行审核
93+
Callback="http://www.demo.com",
94+
Freeze=freeze
95+
)
96+
print(str(response))
97+
98+
99+
def ci_live_video_auditing():
100+
# 提交视频流审核任务
101+
response = client.ci_auditing_live_video_submit(
102+
Bucket=bucket_name,
103+
Url='rtmp://example.com/live/123',
104+
Callback='http://callback.com/',
105+
DataId='testdataid-111111',
106+
UserInfo={
107+
'TokenId': 'token',
108+
'Nickname': 'test',
109+
'DeviceId': 'DeviceId-test',
110+
'AppId': 'AppId-test',
111+
'Room': 'Room-test',
112+
'IP': 'IP-test',
113+
'Type': 'Type-test',
114+
},
115+
BizType='d0292362d07428b4f6982a31bf97c246',
116+
CallbackType=1
117+
)
118+
assert (response['JobsDetail']['JobId'])
119+
jobId = response['JobsDetail']['JobId']
120+
time.sleep(5)
121+
kwargs = {"CacheControl": "no-cache", "ResponseCacheControl": "no-cache"}
122+
response = client.ci_auditing_live_video_cancle(
123+
Bucket=bucket_name,
124+
JobID=jobId,
125+
**kwargs
126+
)
127+
print(response)
128+
129+
130+
def ci_auditing_virus_submit_and_query():
131+
kwargs = {"CacheControl": "no-cache", "ResponseCacheControl": "no-cache"}
132+
response = client.ci_auditing_virus_submit(Bucket=bucket_name,
133+
Key="test.png",
134+
Callback="http://www.demo.com",
135+
**kwargs)
136+
jobId = response['JobsDetail']['JobId']
137+
while True:
138+
time.sleep(5)
139+
kwargs = {"CacheControl": "no-cache", "ResponseCacheControl": "no-cache"}
140+
response = client.ci_auditing_virus_query(Bucket=bucket_name, JobID=jobId, **kwargs)
141+
print(response['JobsDetail']['State'])
142+
if response['JobsDetail']['State'] == 'Success':
143+
print(str(response))
144+
break
145+
print(response)
146+
147+
148+
def ci_auditing_text_submit():
149+
user_info = {
150+
'TokenId': '123456', # 一般用于表示账号信息,长度不超过128字节
151+
'Nickname': '测试', # 一般用于表示昵称信息,长度不超过128字节
152+
'DeviceId': '腾讯云', # 一般用于表示设备信息,长度不超过128字节
153+
'AppId': '12500000', # 一般用于表示 App 的唯一标识,长度不超过128字节
154+
'Room': '1', # 一般用于表示房间号信息,长度不超过128字节
155+
'IP': '127.0.0.1', # 一般用于表示 IP 地址信息,长度不超过128字节
156+
'Type': '测试', # 一般用于表示业务类型,长度不超过128字节
157+
'ReceiveTokenId': '789123', # 一般用于表示接收消息的用户账号,长度不超过128字节
158+
'Gender': '男', # 一般用于表示性别信息,长度不超过128字节
159+
'Level': '100', # 一般用于表示等级信息,长度不超过128字节
160+
'Role': '测试人员', # 一般用于表示角色信息,长度不超过128字节
161+
}
162+
response = client.ci_auditing_text_submit(
163+
Bucket=bucket_name, # 桶名称
164+
Content='123456test'.encode("utf-8"), # 需要审核的文本内容
165+
BizType='', # 表示审核策略的唯一标识
166+
UserInfo=user_info, # 用户业务字段
167+
DataId='456456456', # 待审核的数据进行唯一业务标识
168+
)
169+
print(response)
170+
171+
172+
def ci_auditing_text_txt_submit():
173+
user_info = {
174+
'TokenId': '123456', # 一般用于表示账号信息,长度不超过128字节
175+
'Nickname': '测试', # 一般用于表示昵称信息,长度不超过128字节
176+
'DeviceId': '腾讯云', # 一般用于表示设备信息,长度不超过128字节
177+
'AppId': '12500000', # 一般用于表示 App 的唯一标识,长度不超过128字节
178+
'Room': '1', # 一般用于表示房间号信息,长度不超过128字节
179+
'IP': '127.0.0.1', # 一般用于表示 IP 地址信息,长度不超过128字节
180+
'Type': '测试', # 一般用于表示业务类型,长度不超过128字节
181+
'ReceiveTokenId': '789123', # 一般用于表示接收消息的用户账号,长度不超过128字节
182+
'Gender': '男', # 一般用于表示性别信息,长度不超过128字节
183+
'Level': '100', # 一般用于表示等级信息,长度不超过128字节
184+
'Role': '测试人员', # 一般用于表示角色信息,长度不超过128字节
185+
}
186+
response = client.ci_auditing_text_submit(
187+
Bucket=bucket_name, # 桶名称
188+
# Content='123456test'.encode("utf-8"), # 需要审核的文本内容
189+
Key='shenhe.txt',
190+
Url='https://test-1250000000.cos.ap-chongqing.myqcloud.com/shenhe.txt?q-sign-algorithm=sha1&q-ak=AKIDPdbIjuoRt40g5D4ex0nKaaJlvoRKzNVN&q-sign-time=1690968685;1690975885&q-key-time=1690968685;1690975885&q-header-list=&q-url-param-list=&q-signature=c93b2350e946ad1d5336286221edc66e53f18989',
191+
BizType='', # 表示审核策略的唯一标识
192+
UserInfo=user_info, # 用户业务字段
193+
DataId='456456456', # 待审核的数据进行唯一业务标识
194+
)
195+
print(response)
196+
197+
198+
def ci_auditing_text_txt_query():
199+
200+
response = client.ci_auditing_text_query(
201+
Bucket=bucket_name, # 桶名称
202+
JobID='st6a7d90fe311xxxxxxxxxxxxxxxxx', # 需要查询的文本文件审核任务ID
203+
)
204+
print(response)
205+
206+
207+
if __name__ == '__main__':
208+
# ci_auditing_video_submit()
209+
# ci_auditing_video_query()
210+
ci_auditing_image_batch()
211+
# ci_live_video_auditing()
212+
# ci_auditing_virus_submit_and_query()
213+
# ci_auditing_text_submit()
214+
# ci_auditing_text_txt_submit()
215+
# ci_auditing_text_txt_query()

demo/ci_media.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,16 +1908,33 @@ def ci_create_words_generalize_jobs():
19081908

19091909
def ci_get_presigned_download_url():
19101910
param = {
1911-
"object": "test.gif",
1912-
"format": "mp4"
1911+
# 想要处理的文件路径
1912+
# 支持cos对象的绝对路径
1913+
# 必选参数
1914+
"object": "test1026.gif",
1915+
# 转码后的视频流格式
1916+
# 当前仅支持mp4
1917+
# 必选参数
1918+
"format": "mp4",
1919+
# 转码后视频的宽
1920+
# 取值范围:(0,4096]。默认为0
1921+
# 非必传参数
1922+
# "width": "200",
1923+
# 转码后视频的高
1924+
# 取值范围:(0,4096]。默认为0
1925+
# 非必传参数
1926+
# 当 width 和 height 都为0时,表示使用视频的宽高。如果单个为0,则以另外一个值按视频宽高比例自动适应
1927+
# "heigth": "200"
19131928
}
19141929
url = client.get_presigned_download_url(
1915-
Bucket=bucket_name,
1916-
Key='/convert',
1917-
Expired=3600,
1918-
Params=param,
1919-
UseCiEndPoint=True,
1930+
Bucket=bucket_name, # 存储桶名称
1931+
Key="/convert", # 请求uri 同步转码固定为/convert
1932+
Expired=3600, # 预签名超时时间
1933+
Params=param, # 请求处理参数
1934+
UseCiEndPoint=True, # 是否使用数据万象的请求域名
19201935
)
1936+
if token is not None:
1937+
url = url + "&x-cos-security-token=" + token
19211938
print(url)
19221939

19231940

qcloud_cos/cos_client.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5965,7 +5965,7 @@ def ci_auditing_html_query(self, Bucket, JobID, **kwargs):
59655965

59665966
return data
59675967

5968-
def ci_auditing_image_batch(self, Bucket, Input, DetectType=None, BizType=None, **kwargs):
5968+
def ci_auditing_image_batch(self, Bucket, Input, DetectType=None, BizType=None, Async=0, Callback=None, Freeze=None, **kwargs):
59695969
"""图片同步批量审核接口 https://cloud.tencent.com/document/product/436/63593
59705970
59715971
:param Bucket(string): 存储桶名称.
@@ -5983,6 +5983,11 @@ def ci_auditing_image_batch(self, Bucket, Input, DetectType=None, BizType=None,
59835983
UserInfo: 用户业务字段。
59845984
:param DetectType(int): 内容识别标志,位计算 1:porn, 8:ads
59855985
:param BizType(string): 审核策略的唯一标识,由后台自动生成,在控制台中对应为Biztype值.
5986+
:param Async(string): 是否异步进行审核,0:同步返回结果,1:异步进行审核。默认值为 0。
5987+
:param Callback(string): 审核结果(Detail版本)以回调形式发送至您的回调地址,异步审核时生效,支持以 http:// 或者 https:// 开头的地址,例如:http://www.callback.com。
5988+
:param Freeze(dict): 可通过该字段,设置根据审核结果给出的不同分值,对图片进行自动冻结,仅当 input 中审核的图片为 object 时有效。
5989+
PornScore: 取值为[0,100],表示当色情审核结果大于或等于该分数时,自动进行冻结操作。不填写则表示不自动冻结,默认值为空。
5990+
AdsScore: 取值为[0,100],表示当广告审核结果大于或等于该分数时,自动进行冻结操作。不填写则表示不自动冻结,默认值为空。
59865991
:param kwargs(dict): 设置请求的headers.
59875992
:return(dict):任务提交成功返回的结果,dict类型.
59885993
@@ -6019,6 +6024,12 @@ def ci_auditing_image_batch(self, Bucket, Input, DetectType=None, BizType=None,
60196024
if DetectType is not None:
60206025
detect_type = CiDetectType.get_detect_type_str(DetectType)
60216026
conf['DetectType'] = detect_type
6027+
if Async == 0:
6028+
conf['Async'] = Async
6029+
if Callback is not None:
6030+
conf["Callback"] = Callback
6031+
if Freeze is not None:
6032+
conf["Freeze"] = Freeze
60226033
request = {
60236034
'Input': Input,
60246035
'Conf': conf

0 commit comments

Comments
 (0)