swap_face.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import os
  2. import time
  3. import requests
  4. from PIL import Image
  5. import threading
  6. class swap_face():
  7. def __init__(self, imgurl):
  8. self.imgurl = imgurl
  9. def run(self):
  10. name_hash = str(time.time()).replace('.','')
  11. src_img = 'FaceSwap/src_img/'+name_hash+'.jpg'
  12. sv_path = '/var/www/html/swap_save/'+name_hash+'.avi'
  13. print(name_hash)
  14. try:
  15. im = Image.open(requests.get(self.imgurl, stream=True).raw)
  16. im= im.convert("RGB")
  17. im.save(src_img)
  18. except:
  19. return {'msg':'圖片錯誤'}
  20. x = threading.Thread(target=self.runthreadswap, args=(src_img,sv_path))
  21. x.start()
  22. time.sleep(20)
  23. load_time = 0
  24. while True:
  25. print('waiting...')
  26. if os.path.exists(sv_path):
  27. break
  28. time.sleep(10)
  29. load_time+=10
  30. print('waiting...')
  31. self.notify_group('人物生成成功,在:www.choozmo.com:8168/swap_save/'+name_hash+'.avi')
  32. return {'msg':'生成中,成果會在line中展示'}
  33. def runthreadswap(self,src_img,sv_path):
  34. os.system('python3 FaceSwap/main_video.py --src_img '+src_img+' --video_path ./FaceSwap/b1.mp4 --correct_color --save_path '+sv_path)
  35. def notify_group(self,msg):
  36. headers = {
  37. "Authorization": "Bearer " + "WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD",
  38. "Content-Type": "application/x-www-form-urlencoded"
  39. }
  40. params = {"message": msg}
  41. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
  42. #print(r)