TencentCloud / tencentcloud-sdk-python

Tencent Cloud API 3.0 SDK for Python
Apache License 2.0
613 stars 251 forks source link

I want use the DetectDisgust api. can you supply the example? #65

Closed Usernamezhx closed 4 years ago

Usernamezhx commented 4 years ago

thanks very much. I can not find any example about the DetectDisgust.

Usernamezhx commented 4 years ago
import os
import base64

# from PIL import Image
# from io import BytesIO
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.tiia.v20190529 import tiia_client, models

def convert_base64(img_path):
    with open(img_path, 'rb') as f:
        base64_img = base64.b64encode(f.read()).decode('utf-8') 
    return base64_img
try:
    cred = credential.Credential(
        'xxxxxxxxxxx',
        'xxxxxxxxxxx')

    httpProfile = HttpProfile()
    httpProfile.endpoint = "tiia.tencentcloudapi.com"

    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    client = tiia_client.TiiaClient(cred, "ap-beijing", clientProfile)

    req = models.DetectDisgustRequest()
    # req.ImageUrl = "xxxxxxxxxxx.jpg"
    req.ImageBase64 = img
    resp = client.DetectDisgust(req)
    print(resp.to_json_string())

except TencentCloudSDKException as err:
    print(err)
Usernamezhx commented 4 years ago

Besides that. I want to know where to get the meaning of these word aa aai af afc ame asr batch bda I find the DetectDisgust function at https://github.com/TencentCloud/tencentcloud-sdk-python/blob/74cec17a22a67cf55c2d30dcf13946d00a2d3e29/tencentcloud/tiia/v20190529/tiia_client.py#L122

zqfan commented 4 years ago

aa aai af afc ame asr batch bda These are product codes. It always will be the first part of domain name. For example, you've read a API document in official site, it says this API domain name is xyz.tencentcloudapi.com, then it will be xyz module in sdk. https://intl.cloud.tencent.com/document/product/213/33276 image We have a document here (Chinese Simplified version), but it is not the latest (edit manually, not auto generated as sdk), might be a bit helpful: https://cloud.tencent.com/document/sdk/Description

Usernamezhx commented 4 years ago

thanks for your reply. when I want to test the detectDisgust api for about 1w local image. It will show me an error ouble free or corruption (out) .and that can not be catch by the try except.this is my code snippet:

@retry
def init():
    cred = credential.Credential(
        'xxxxxxxxxxxxxxxxx',
        'xxxxxxxxxxxxxx')

    httpProfile = HttpProfile()
    httpProfile.endpoint = "tiia.tencentcloudapi.com"

    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    client = tiia_client.TiiaClient(cred, "ap-beijing", clientProfile)
    # req = models.DetectDisgustRequest()
    return client

def process(content):
    print('process {}'.format(len(res)))
    feed_id = content[0]
    url = content[-1]
    local_path = os.path.join(base_path, os.path.basename(url))
    if os.path.exists(local_path):
        #req.ImageBase64 = convert_base64(local_path)
        try:
            req = models.DetectDisgustRequest()
            req.ImageBase64 = convert_base64(local_path)
            resp = client.DetectDisgust(req)
            res.append([feed_id, resp.to_json_string()])
        except Exception as e:
            print(e)
            return

if __name__ == "__main__":
    res = []
    client = init()
    pool = ThreadPool(6)
    pool.map(process, feed_list)
    pool.close()
    pool.join()
Usernamezhx commented 4 years ago

it can be solved by :

def process(content):
    print('process {}'.format(len(res)))
    feed_id = content[0]
    url = content[-1]
    local_path = os.path.join(base_path, os.path.basename(url))
    if os.path.exists(local_path):
        #req.ImageBase64 = convert_base64(local_path)
        try:
            cred = credential.Credential(
        'xxxxxxxxxxxxxxxxx',
        'xxxxxxxxxxxxxx')

            httpProfile = HttpProfile()
            httpProfile.endpoint = "tiia.tencentcloudapi.com"

            clientProfile = ClientProfile()
            clientProfile.httpProfile = httpProfile
            client = tiia_client.TiiaClient(cred, "ap-beijing", clientProfile)
            req = models.DetectDisgustRequest()
            req.ImageBase64 = convert_base64(local_path)
            resp = client.DetectDisgust(req)
            res.append([feed_id, resp.to_json_string()])
        except Exception as e:
            print(e)
            return