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()
0 commit comments