调用百度智能图片处理的例子
By
☜李長新☞
at 2021-09-23 • 0人收藏 • 904人看过
import win.ui; import console; /*DSG{{*/ var mainForm = win.form(text="百度图像增强与特效";right=1014;bottom=703) mainForm.add( APIEdit={cls="edit";text="应用的API Key";left=355;top=19;right=1001;bottom=45;edge=1;multiline=1;password=1;z=1}; SecretEdit={cls="edit";text="应用的Secret Key";left=355;top=51;right=1001;bottom=77;edge=1;multiline=1;password=1;z=4}; listbox={cls="listbox";left=2;top=3;right=251;bottom=705;db=1;dl=1;dt=1;items={};ownerDraw=1;z=2}; picturebox={cls="picturebox";left=260;top=87;right=1008;bottom=663;border=1;notify=1;z=6}; static={cls="static";text="API Key:";left=261;top=16;right=348;bottom=42;align="center";center=1;transparent=1;z=3}; static2={cls="static";text="access_token:";left=259;top=49;right=351;bottom=75;align="center";center=1;transparent=1;z=5}; static3={cls="static";text="点击空白处选择图片";left=260;top=676;right=1008;bottom=696;transparent=1;z=7} ) /*}}*/ /*侧边菜单设计{{*/ var MenuList = { { "1|图像特效|\uF28A"; "1.1|黑白图像上色|\uF1FC"; "1.2|图像风格转换|\uF2DD"; "1.3|人像动漫化|\uF2AE"; 展开 = true; }, { "2|图像增强|\uF1E9"; "2.1|图像无损放大|\uF00E"; "2.2|图像去雾|\uF0A3"; "2.3|图像对比度增强|\uF247"; "2.4|拉伸图像恢复|\uF00E"; "2.5|图像修复|\uF0D0"; "2.6|图像清晰度增强|\uF06E"; 展开 = true; }, } /*}}*/ import exListbox; //var exx = exListbox(mainForm.listbox,MenuList,30,0x000000,0x272E2D,0x663202,0xFFD859,0x3E8427); var exx = exListbox(mainForm.listbox,MenuList,45,0x2E2E2E,0x3C3C3C,0x2E2E2E,0xFFFFFF,0x666666); exx.onClick = function(index,subindex,text){ select(index) {//判断一级菜单序号 case 1 {//执行一级菜单1命令开始 select(subindex){//判断二级菜单序号 case 1 {//执行二级菜单1.1命令开始 //黑白图像上色 var 上色url = "https://aip.baidubce.com/rest/2.0/image-process/v1/colourize"; access_token,expires_in = Access—_Token获取(mainForm) log_id,image = 图片处理(access_token,headers,上色url ,Oimage) string.save("F:\上色\image.jpg",image) };//执行二级菜单1命令结束 case 2 {//执行二级菜单1.2命令开始 //图像风格转换 var 风格转换url = "https://aip.baidubce.com/rest/2.0/image-process/v1/style_trans"; access_token,expires_in = Access—_Token获取(mainForm) log_id,image = 图片处理(access_token,headers,风格转换url,Oimage) string.save("F:\风格转换\image.jpg",image) };//执行二级菜单1.2命令结束 case 3 {//执行二级菜单1.3命令开始 //人像动漫化 var 动漫化url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"; access_token,expires_in = Access—_Token获取(mainForm) log_id,image = 图片处理(access_token,headers,动漫化url,Oimage) string.save("F:\动漫化\image.jpg",image) };//执行二级菜单1.3命令结束 }//判断二级菜单序号结束 };//执行一级菜单1命令结束 case 2 {//执行一级菜单2命令开始 select(subindex){//判断二级菜单序号 case 1 {//执行二级菜单2.1命令开始 //退单确认 };//执行二级菜单2.1命令结束 case 2 {//执行二级菜单2.2命令开始 //工单质检 };//执行二级菜单2.2命令结束 case 3 {//执行二级菜单2.3命令开始 //查看详情 };//执行二级菜单2.3命令结束 case 4 {//执行二级菜单3.1命令开始 repairOper =""//伽师县 };//执行二级菜单3.1命令结束 case 5 {//执行二级菜单3.1命令开始 repairOper =""//英吉沙 };//执行二级菜单3.1命令结束 case 6 {//执行二级菜单3.1命令开始 var 清晰增强url = "https://aip.baidubce.com/rest/2.0/image-process/v1/image_definition_enhance"; access_token,expires_in = Access—_Token获取(mainForm) log_id,image = 图片处理(access_token,headers,清晰增强url,Oimage) string.save("F:\清晰增强\image.jpg",image) };//执行二级菜单3.1命令结束 }//判断二级菜单序号结束 };//执行一级菜单2命令结束 }//判断一级菜单序号结束 } //Access Token获取 Access—_Token获取 = function(mainForm){ //grant_type: 必须参数,固定为client_credentials; //client_id: 必须参数,应用的API Key; //client_secret: 必须参数,应用的Secret Key; import inet.http; import inet.url; var PostDate = { grant_type = "client_credentials"; client_id = mainForm.APIEdit.text; client_secret = mainForm.SecretEdit.text; } PostDate = inet.url.stringifyParameters(PostDate) //console.log("PostDate是" ,PostDate); //console.pause(true); tokenurl = "https://aip.baidubce.com/oauth/2.0/token" http = inet.http(); response = http.post(tokenurl,PostDate) import web.json; console.varDump(web.json.parse(response)) responseTab = web.json.parse(response) access_token = responseTab.access_token expires_in = responseTab.expires_in console.varDump(access_token,expires_in) console.pause(true); return access_token,expires_in; } 图片处理 = function(access_token,headers,url,Oimage){ import inet.http; import inet.url; http = inet.http(); var url = url + "?access_token=" + access_token var params = {"image":Oimage} var headers = {['Content-Type'] = 'application/x-www-form-urlencoded';} response = http.post(url, params, headers); import web.json; responseTab = web.json.parse(response) log_id = responseTab.log_id image = responseTab.image import crypt.bin; image = crypt.bin.decodeBase64(image) return log_id,image; } mainForm.picturebox.oncommand = function(id,event){ import fsys.dlg; var file = fsys.dlg.open(); mainForm.picturebox.image = file import crypt.bin; f = string.load(file) Oimage = crypt.bin.encodeBase64(f); //io.open(); //io.print(Oimage) } mainForm.show(); win.loopMessage();
2 个回复 | 最后更新于 2021-09-24
登录后方可回帖
调用的 百度图片AI:图像增强与特效