import json
import cv2
import requests
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.bda.v20200324 import bda_client
from tencentcloud.bda.v20200324 import models as bda_models
import numpy as np
import base64
import matplotlib.pyplot as plt
%matplotlib inline
import warnings
warnings.filterwarnings("ignore")
SecretId="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
SecretKey="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"""
视频人像分割
https://console.cloud.tencent.com/api
/explorer?Product=bda&Version=2020-03-24&Action=CreateSegmentationTask&SignVersion=
"""
try:
cred = credential.Credential(SecretId,SecretKey)
httpProfile = HttpProfile()
httpProfile.endpoint = "bda.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = bda_client.BdaClient(cred, "ap-guangzhou", clientProfile)
req = bda_models.CreateSegmentationTaskRequest()
"""
VideoUrl 是 String 需要分割的视频URL,可外网访问。
BackgroundImageUrl 否 String 背景图片URL。
可以将视频背景替换为输入的图片。
如果不输入背景图片,则输出人像区域mask。
Config 否 String 预留字段,后期用于展示更多识别信息。
"""
params = {
"VideoUrl": "xxxxxxxxxxxxxxxxxxxxxx/video/test_10s.mp4",
"BackgroundImageUrl": "",
"Config": ""
}
req.from_json_string(json.dumps(params))
resp = client.CreateSegmentationTask(req).to_json_string()
resp=json.loads(resp)
print(resp)
except TencentCloudSDKException as err:
print(err)
{'TaskID': '9YZqUUM2nzElnQxt', 'EstimatedProcessingTime': 30, 'RequestId': 'efba7af0-a341-40c5-800d-c35167a1fb9d'}
TaskID=resp["TaskID"]
TaskID
'xxxxxxxxxxxxxxxxxxxxxxxx'
try:
cred = credential.Credential(SecretId, SecretKey)
httpProfile = HttpProfile()
httpProfile.endpoint = "bda.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = bda_client.BdaClient(cred, "ap-guangzhou", clientProfile)
req = bda_models.DescribeSegmentationTaskRequest()
params = {
"TaskID": TaskID
}
req.from_json_string(json.dumps(params))
resp = client.DescribeSegmentationTask(req).to_json_string()
resp=json.loads(resp)
print(resp)
except TencentCloudSDKException as err:
print(err)
{'TaskStatus': 'FINISHED', 'ResultVideoUrl': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'ResultVideoMD5': 'xxxxxxxxxxxxxxxxxxx', 'VideoBasicInformation': {'FrameWidth': 1920, 'FrameHeight': 1080, 'FramesPerSecond': 25, 'Duration': 10, 'TotalFrames': 250}, 'ErrorMsg': '', 'RequestId': 'xxxxxxxxxxxxxxxxxxxxxxxxx'}
ResultVideoUrl=resp['ResultVideoUrl']
ResultVideoUrl
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.mp4'
with open("./video/视频人像分割.mp4",'wb') as file:
file.write(requests.get(ResultVideoUrl).contenttent)