当前位置:   article > 正文

Python调用api实现AI绘画_pygame api编写ai

pygame api编写ai
  1. #导入相关库
  2. import requests
  3. import json
  4. import base64
  5. import cv2
  6. import numpy as np
  7. from alive_progress import alive_bar
  8. import time
  9. while True:
  10. u="https://flagopen.baai.ac.cn/flagStudio/auth/getToken"
  11. param={"apikey":"aee3672220ec348b3b3dfaa7956a8282"}
  12. token_response=requests.get(url=u,params=param)
  13. token_json=json.loads(token_response.text)
  14. token=token_json['data']['token']
  15. #调用api
  16. u = "https://flagopen.baai.ac.cn/flagStudio/v1/text2img"
  17. prompt=input("请输入你的创意:")
  18. param={"prompt":prompt,"steps":80,"style":"写实主义","width":500,"height":300}
  19. header={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36",
  20. "Accept": "application/json",
  21. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
  22. "X-Requested-With": "XMLHttpRequest",
  23. "token":token}
  24. response=requests.post(url=u,json=param,headers=header)
  25. print('AI正在绘画中...')
  26. img_json=json.loads(response.text)
  27. img=img_json['data']
  28. img=base64.b64decode(img)
  29. imgstring = np.array(img).tobytes()
  30. imgstring = np.asarray(bytearray(imgstring), dtype="uint8")
  31. image = cv2.imdecode(imgstring, cv2.IMREAD_COLOR)
  32. mylist = range(0,30)
  33. with alive_bar(len(mylist)) as bar:
  34. for i in mylist:
  35. bar()
  36. time.sleep(0.5)
  37. print('绘制完成')
  38. cv2.imshow('result',image)
  39. cv2.waitKey(0)
  40. cv2.destroyAllWindows()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/244377
推荐阅读
相关标签
  

闽ICP备14008679号