tonyassi commited on
Commit
031e67d
Β·
verified Β·
1 Parent(s): bbe4686

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import insightface
3
+ from insightface.app import FaceAnalysis
4
+
5
+ wellcomingMessage = """
6
+ <h1>Face Swapping</h1>
7
+ <p>If you like this app, plase take a look at my <a href="https://www.meetup.com/tech-web3-enthusiasts-united-insightful-conversations/" target="_blank">Meetup Group</a>! There will be more interesting apps and events soon.</p>
8
+ <p>Happy <span style="font-size:500%;color:red;">&hearts;</span> coding!</p>
9
+ <div style="color: grey; font-size:small;">
10
+ <p>πŸš€ Love my Face-Swapping Fun? Support Me with Crypto</p>
11
+ <ul">
12
+ <li>BTC: bc1q2m92e4hrtpk3keh2dsq8whljz7mfquv46xetwj</li>
13
+ <li>ETH: 0x0459620D616C6D827603d43539519FA320B831c2</li>
14
+ </ul>
15
+ </div>
16
+ """
17
+
18
+ assert insightface.__version__>='0.7'
19
+
20
+ value = 0
21
+ app = FaceAnalysis(name='buffalo_l')
22
+ app.prepare(ctx_id=0, det_size=(640, 640))
23
+ swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=True, download_zip=True)
24
+
25
+ def swap_faces(faceSource, sourceFaceId, faceDestination, destFaceId):
26
+ faces = app.get(faceSource)
27
+ faces = sorted(faces, key = lambda x : x.bbox[0])
28
+ if len(faces) < sourceFaceId or sourceFaceId < 1:
29
+ raise gr.Error(f"Source image only contains {len(faces)} faces, but you requested face {sourceFaceId}")
30
+
31
+ source_face = faces[sourceFaceId-1]
32
+
33
+ res_faces = app.get(faceDestination)
34
+ res_faces = sorted(res_faces, key = lambda x : x.bbox[0])
35
+ if len(res_faces) < destFaceId or destFaceId < 1:
36
+ raise gr.Error(f"Destination image only contains {len(res_faces)} faces, but you requested face {destFaceId}")
37
+ res_face = res_faces[destFaceId-1]
38
+
39
+ result = swapper.get(faceDestination, res_face, source_face, paste_back=True)
40
+
41
+ global value
42
+ value = value + 1
43
+ print(f"processed: {value}...")
44
+
45
+ # for face in faces:
46
+ # res = swapper.get(res, face, source_face, paste_back=True)
47
+ # cv2.imwrite("./t1_swapped.jpg", res)
48
+ return result
49
+
50
+ gr.Interface(swap_faces,
51
+ [
52
+ gr.Image(),
53
+ gr.Number(precision=0, value=1, info='face position (from left, starting at 1)'),
54
+ gr.Image(),
55
+ gr.Number(precision=0, value=1, info='face position (from left, starting at 1)')
56
+ ],
57
+ gr.Image(),
58
+ description=wellcomingMessage,
59
+ examples=[
60
+ ['./Images/kim.jpg', 1, './Images/marilyn.jpg', 1],
61
+ ['./Images/friends.jpg', 2, './Images/friends.jpg', 1],
62
+ ],
63
+ ).launch()