/
faceswap.go
126 lines (107 loc) · 2.96 KB
/
faceswap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package api
import (
"bytes"
"encoding/base64"
"fmt"
"html/template"
"image"
"image/draw"
"image/gif"
"image/jpeg"
"net/http"
"strconv"
"strings"
"time"
gildasai "github.com/gildasch/gildas-ai"
"github.com/gildasch/gildas-ai/imageutils"
"github.com/gildasch/gildas-ai/imageutils/gifutils"
"github.com/gin-gonic/gin"
)
func FaceSwapHandler(extractor *gildasai.Extractor, detector gildasai.Landmark) gin.HandlerFunc {
return func(c *gin.Context) {
srcURL := strings.TrimPrefix(c.Query("src"), "/")
dstURL := strings.TrimPrefix(c.Query("dst"), "/")
blur, _ := strconv.ParseFloat(c.Query("blur"), 64)
if srcURL == "" || dstURL == "" {
c.HTML(http.StatusOK, "faceswap.html", gin.H{
"src": srcURL,
"dst": dstURL,
})
return
}
src, err := imageutils.FromURL(srcURL)
if err != nil {
c.AbortWithStatusJSON(
http.StatusBadRequest,
fmt.Sprintf("cannot read remote image %q: %v\n", srcURL, err))
return
}
if strings.Contains(strings.ToLower(dstURL), ".gif") {
dstGIF, err := imageutils.GIFFromURL(dstURL)
if err != nil {
c.AbortWithStatusJSON(
http.StatusBadRequest,
fmt.Sprintf("cannot read remote image %q: %v\n", dstURL, err))
return
}
if len(dstGIF.Image) == 0 {
c.AbortWithStatusJSON(
http.StatusBadRequest,
fmt.Sprintf("gif %q has zero frame\n", dstURL))
return
}
dst := image.NewRGBA(dstGIF.Image[0].Bounds())
var outImages []image.Image
for i := 0; i < len(dstGIF.Image); i++ {
draw.Draw(dst, dstGIF.Image[i].Bounds(), dstGIF.Image[i], dstGIF.Image[i].Bounds().Min, draw.Over)
if blur == 0 {
blur = 0.7
}
out, err := gildasai.FaceSwap(extractor, detector, dst, src, blur)
if err != nil {
continue
}
outImages = append(outImages, out)
}
outGIF, err := gifutils.MakeGIFFromImages(
outImages, time.Duration(dstGIF.Delay[0])*10*time.Millisecond, gifutils.StandardQuantizer{})
c.HTML(http.StatusOK, "faceswap.html", gin.H{
"src": srcURL,
"dst": dstURL,
"out": template.URL(toHTMLBase64GIF(outGIF)),
})
return
}
dst, err := imageutils.FromURL(dstURL)
if err != nil {
c.AbortWithStatusJSON(
http.StatusBadRequest,
fmt.Sprintf("cannot read remote image %q: %v\n", dstURL, err))
return
}
out, err := gildasai.FaceSwap(extractor, detector, dst, src, blur)
if err != nil {
c.AbortWithStatusJSON(
http.StatusBadRequest,
fmt.Sprintf("error faceswapping: %v\n", err))
return
}
c.HTML(http.StatusOK, "faceswap.html", gin.H{
"src": srcURL,
"dst": dstURL,
"out": template.URL(toHTMLBase64(out)),
})
}
}
func toHTMLBase64(img image.Image) string {
var buf bytes.Buffer
_ = jpeg.Encode(&buf, img, nil)
b64 := base64.StdEncoding.EncodeToString(buf.Bytes())
return "data:image/jpeg;base64," + b64
}
func toHTMLBase64GIF(g *gif.GIF) string {
var buf bytes.Buffer
_ = gif.EncodeAll(&buf, g)
b64 := base64.StdEncoding.EncodeToString(buf.Bytes())
return "data:image/gif;base64," + b64
}