import os 
import time
import requests
from PIL import Image
class swap_face():
    def __init__(self, imgurl):
        self.imgurl = imgurl
    def run(self):
        name_hash = str(time.time()).replace('.','')
        src_img = 'FaceSwap/src_img/'+name_hash+'.jpg'
        sv_path = '../html/swap_save'+name_hash+'.avi'
        
        try:
            im = Image.open(requests.get(self.imgurl, stream=True).raw)
            im= im.convert("RGB")
            im.save(src_img)
        except:
            return '圖片錯誤'
        

        os.system('python3 FaceSwap/main_video.py --src_img '+src_img+' --video_path FaceSwap/nina_noGesture_adj.mp4 --correct_color --save_path '+sv_path)    
        while True:
            print('waiting...')
            if os.path.exists(sv_path):
                break
            time.sleep(5)
            print('waiting...')

        return "人物生成完成 at : www.choozmo.com:8168/swap_save/"+name_hash+".avi"