Browse Source

new face swap proto finish

ming 3 years ago
parent
commit
ab16673be7
1 changed files with 21 additions and 6 deletions
  1. 21 6
      api/main.py

+ 21 - 6
api/main.py

@@ -52,6 +52,7 @@ app.mount("/static/img", StaticFiles(directory="static/img"), name="static/img")
 
 tmp_video_dir = '../OpenshotService/tmp_video/'
 tmp_avatar_dir = '../../face_swap/tmp_avatar/'  #change source face path here
+
 video_sub_folder = 'ai_anchor_video/'
 avatar_sub_folder = 'swap_save/'
 tmp_img_sub_folder = 'tmp_img/'
@@ -77,14 +78,28 @@ async def index2():
 @app.get("/gen_avatar")
 async def avatar():
     return FileResponse('static/gen_avatar.html')
-'''
+
 @app.post("/swapFace")
-async def swapFace(req:swap_req):
+async def swapFace(imgurl):
+    '''
     sf = swap_face(req.imgurl)
     result = sf.run()
     #notify_group(result)hi
-    return result
-'''
+    '''
+    im = Image.open(requests.get(imgu, stream=True).raw)
+    im.save(dir_photo+name_hash+"/"+str(img_num)+".jpg")
+    try:
+        if get_url_type(imgurl) =='video/mp4':
+            r=requests.get(imgurl)
+        else:
+            im = Image.open(requests.get(imgurl, stream=True).raw)
+            im= im.convert("RGB")
+    except:
+        return {'msg':"無法辨別圖片網址"+imgurl}
+    x = threading.Thread(target=gen_avatar, args=(name_hash,imgurl))
+    x.start()
+    return '人物生成中,請稍候'
+
 @app.post("/uploadfile/")
 async def create_upload_file(file: UploadFile = File(...)):
     img_name = str(time.time()).replace('.','')
@@ -163,11 +178,11 @@ def gen_video(name_hash,name,text_content, image_urls,avatar,client_id):
     os.remove(tmp_video_dir+name_hash+'.mp4')
 
 
-def gen_avatar():
+def gen_avatar(name_hash, imgurl):
     c = rpyc.connect("localhost", 8868)
     c._config['sync_request_timeout'] = None
     remote_svc = c.root
-    my_answer = remote_svc.call_avatar(name_hash) # method call
+    my_answer = remote_svc.call_avatar(name_hash,imgurl) # method call
    
     shutil.copy(tmp_avatar_dir+name_hash+'.mp4',avatar_dest+name_hash+'.mp4')
     os.remove(tmp_avatar_dir+name_hash+'.mp4')