MayankTamakuwala commited on
Commit
408deaa
1 Parent(s): d1cf19d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -18
app.py CHANGED
@@ -8,32 +8,45 @@ from PIL import Image
8
  import json
9
  import numpy as np
10
  import rsa
11
- import insightface
12
- from insightface.app import FaceAnalysis
13
 
14
  def FaceSwap(source_img, dest_img):
15
-
16
- swapper = insightface.model_zoo.get_model('./inswapper_128.onnx', download=False, download_zip=False)
17
 
18
- app = FaceAnalysis(name="buffalo_l")
19
- app.prepare(ctx_id=0, det_size=(640, 640))
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- s_faces = app.get(source_img)
22
- if len(s_faces) == 0:
23
- raise gr.Error("No Face Detected in Source Image")
24
 
25
- d_faces = app.get(dest_img)
26
- if len(d_faces) == 0:
27
- raise gr.Error("No Face Detected in Destination Image")
28
 
29
- source_face = s_faces[0]
30
- dest_face = d_faces[0]
 
31
 
32
- res = dest_img.copy()
33
- res = swapper.get(res, dest_face, source_face, paste_back=True)
34
- return res
 
 
 
 
35
 
36
  if __name__ == "__main__":
37
  with gr.Blocks(title="Face Swap | Meet With Your new Personality") as demo:
38
- gr.Interface(fn=FaceSwap, inputs=["image","image"], outputs=["image"], allow_flagging="never")
39
  demo.launch(show_api=False, favicon_path="./favicon.ico")
 
8
  import json
9
  import numpy as np
10
  import rsa
 
 
11
 
12
  def FaceSwap(source_img, dest_img):
 
 
13
 
14
+ secret = os.environ['API_KEY']
15
+ privateKey = os.environ["PRIVATE_KEY"]
16
+ publicKey = os.environ["PUBLIC_KEY"]
17
+ baseurl = os.environ["BASE_URL"]
18
+
19
+ n,e,d,p,q = privateKey.split(",")
20
+ n1,e1 = publicKey.split(",")
21
+ privateKey = rsa.PrivateKey(int(n),int(e),int(d),int(p),int(q))
22
+ publicKey = rsa.PublicKey(int(n1), int(e1))
23
+
24
+ encMessage = rsa.encrypt(secret.encode('utf-8'), publicKey)
25
+ encMessage = base64.b64encode(encMessage).decode("utf-8")
26
+
27
+ src_size = source_img.size
28
+ dest_size = dest_img.size
29
 
30
+ src_base64 = base64.b64encode(bytearray(source_img.tobytes())).decode('utf-8')
31
+ dest_base64 = base64.b64encode(bytearray(dest_img.tobytes())).decode('utf-8')
 
32
 
33
+ #Sending request to a FaceSwap API I deployed it on AWS ec2.
34
+ #Contact [email protected] to get access to the API.
35
+ api_url = f"{baseurl}/api/faceswap"
36
 
37
+ payload = {"source_img": str(src_base64), "dest_img": str(dest_base64), "source_size": src_size, "dest_size": dest_size}
38
+ headers = {'Content-Type': 'multipart/form-data', "Authorization": encMessage}
39
+ res = requests.post(api_url, json=payload, headers=headers)
40
 
41
+ if (res.status_code >= 200 and res.status_code <= 299):
42
+ res = np.array(res.json())
43
+ return res
44
+ elif (res.status_code >= 400 and res.status_code <= 499):
45
+ raise gr.Error(f"Code {res.status_code}: Erron on client")
46
+ else:
47
+ raise gr.Error(f"Code {res.status_code}: Erron on server. Try after some time")
48
 
49
  if __name__ == "__main__":
50
  with gr.Blocks(title="Face Swap | Meet With Your new Personality") as demo:
51
+ gr.Interface(fn=FaceSwap, inputs=[gr.Image(type="pil"), gr.Image(type="pil")], outputs=["image"], allow_flagging="never")
52
  demo.launch(show_api=False, favicon_path="./favicon.ico")