135人参与 • 2024-07-31 • Photoshop
完整代码下载地址:https://download.csdn.net/download/weixin_43703390/89179785 打不开就是还在审核中…
1.打开百度智能云官网:[https://cloud.baidu.com/?from=console],在【产品】中找到【语言与知识】下的【ai作画】
2.打开界面后点击【立即使用】,然后创建应用,得到api key和secret key
3.点击左侧【api在线调试】选择对应的版本
6.如果调试成功的画,就可在unity项目中开始使用了
代码步骤1:首先根据api key和secret key获取token
private string api_key = "111111"; // 替换为你的api key
private string secret_key = "2222222"; // 替换为你的secret key
public string m_token = string.empty;
ienumerator gettoken()
{
//获取token的api地址
string _token_url = string.format("https://aip.baidubce.com/oauth/2.0/token" + "?client_id={0}&client_secret={1}&grant_type=client_credentials"
, api_key, secret_key);
using (unitywebrequest request = new unitywebrequest(_token_url, "get"))
{
request.downloadhandler = (downloadhandler)new downloadhandlerbuffer();
yield return request.sendwebrequest();
if (request.isdone)
{
string _msg = request.downloadhandler.text;
tokeninfo _textback = jsonutility.fromjson<tokeninfo>(_msg);
m_token = _textback.access_token;
}
}
}
/// <summary>
/// 返回的token
/// </summary>
[system.serializable]
public class tokeninfo
{
public string access_token = string.empty;
}
代码步骤2:使用网络上传图片生成描述数据
taskdata taskdata=new taskdata();
string url = $"https://aip.baidubce.com/rpc/2.0/ernievilg/v1/txt2img?access_token={m_token}";
dictionary<string, object> dic = new dictionary<string, object>();
dic.add("text", "图片描述");
dic.add("resolution", "1024*1024");//图片尺寸
dic.add("style", "二次元");//图片风格
dic.add("num", 1);//生成数量
using (unitywebrequest www = unitywebrequest.post(url, "")) {
www.setrequestheader("content-type", "application/json");
byte[] bodyraw = system.text.encoding.utf8.getbytes(jsonconvert.serializeobject(dic));
www.uploadhandler = new uploadhandlerraw(bodyraw);
www.downloadhandler = new downloadhandlerbuffer();
yield return www.sendwebrequest();
if (www.result == unitywebrequest.result.success)
{
debug.log("结果1:"+ www.downloadhandler.text);
}
else
{
debug.log("结果2:" + www.error);
}
}
代码步骤3:百度服务器图片生成之后,获取图片的信息
rootobject rootobject = new rootobject();
string urls = $"https://aip.baidubce.com/rpc/2.0/ernievilg/v1/getimg?access_token={m_token}";
using (unitywebrequest www = unitywebrequest.post(urls, ""))
{
www.setrequestheader("content-type", "application/json");
dictionary<string, object> dica = new dictionary<string, object>();
dica.add("taskid", "返回的任务id");
byte[] bodyraw = encoding.utf8.getbytes(jsonconvert.serializeobject(dica));
www.uploadhandler = new uploadhandlerraw(bodyraw);
www.downloadhandler = new downloadhandlerbuffer();
yield return www.sendwebrequest();
if (www.result == unitywebrequest.result.success)
{
debug.log("结果2:" + www.downloadhandler.text);
}
else
{
debug.log("结果2:" + www.error);
}
}
代码步骤4:根据返回信息中的网址下载图片 返回信息中的【status】0或1。"1"表示已生成完成,"0"表示任务排队中或正在处理.
using (unitywebrequest www = unitywebrequesttexture.gettexture(rootobject.data.img)) {
yield return www.sendwebrequest();
if (www.result == unitywebrequest.result.success)
{
// 成功下载图片
texture2d texture = downloadhandlertexture.getcontent(www);
if (texture != null)
{
// 获取成功
debug.log("获取成功");
}
}
else
{
// 下载失败
debug.logerror("failed to download image: " + www.error);
}
}
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论