-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_missing_images.go
212 lines (171 loc) · 4.52 KB
/
generate_missing_images.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"html/template"
"net/http"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"time"
)
var apiHost = flag.String("api-host", "", "AUTOMATIC1111 webui origin name")
var seeds = [...]int{
563560, 563561, 563562, 563563, 563564,
563565, 563566, 563567, 563568, 563569,
}
func main() {
flag.Parse()
if *apiHost == "" {
panic("-api-host flag is required")
}
adjectives := readLines("www/adjective.txt")
nouns := readLines("www/noun.txt")
combined := make([]string, 1, len(adjectives)+len(nouns)+1)
combined = append(combined, nouns...)
for _, adjective := range adjectives {
seen := false
for _, noun := range nouns {
if adjective == noun {
seen = true
break
}
}
if !seen {
combined = append(combined, adjective)
}
}
sort.Strings(combined)
err := os.MkdirAll("www/images", 0755)
if err != nil {
panic(err)
}
total := (len(adjectives)*len(nouns) + len(combined)) * len(seeds)
writeIndexFile(adjectives, nouns)
fmt.Printf("%d adjectives; %d nouns; %d overlap; %d variants\n%d total images\n", len(adjectives), len(nouns), len(adjectives)+len(nouns)-len(combined)+1, len(seeds), total)
generated := 0
for _, word := range combined {
generated += generateMissingImages(word, "")
}
for _, noun := range nouns {
for _, adjective := range adjectives {
generated += generateMissingImages(adjective, noun)
}
}
fmt.Printf("%d images generated / %d images already present\n", generated, total-generated)
}
func readLines(filename string) []string {
b, err := os.ReadFile(filename)
if err != nil {
panic(err)
}
lines := strings.Split(strings.ReplaceAll(string(b), "\r", ""), "\n")
if len(lines) > 0 && lines[len(lines)-1] == "" {
return lines[:len(lines)-1]
}
return lines
}
func writeIndexFile(adjectives, nouns []string) {
tmpl := template.Must(template.ParseFiles("www/images/index.tmpl"))
f, err := os.Create("www/images/index.html")
if err != nil {
panic(err)
}
defer f.Close()
variants := make([]int, len(seeds))
for i := range variants {
variants[i] = i
}
data := struct {
Adjectives []string
Nouns []string
Variants []int
}{
Adjectives: adjectives,
Nouns: nouns,
Variants: variants,
}
err = tmpl.Execute(f, &data)
if err != nil {
panic(err)
}
}
func generateMissingImages(adjective, noun string) int {
generated := 0
for i, seed := range seeds {
filename := fmt.Sprintf("www/images/%s-%s-%04d.avif", adjective, noun, i)
_, err := os.Stat(filename)
if err == nil {
continue
}
if !os.IsNotExist(err) {
panic(err)
}
fmt.Printf("Generating image %d/%d for %s %s... ", i, len(seeds), adjective, noun)
start := time.Now()
png := generateImage(adjective, noun, seed)
err = os.WriteFile(filename+".png", png, 0644)
if err != nil {
panic(err)
}
err = exec.Command("avifenc", "--speed", "0", "--jobs", "all", filename+".png", filename).Run()
if err != nil {
panic(err)
}
err = os.Remove(filename + ".png")
if err != nil {
panic(err)
}
fmt.Printf("%v\n", time.Since(start))
generated++
}
return generated
}
func generateImage(adjective, noun string, seed int) []byte {
query, err := json.Marshal(map[string]interface{}{
"sampler_name": "Euler a",
"steps": 50,
"cfg_scale": 7,
"width": 640,
"height": 360,
"enable_hr": true,
"hr_scale": 2,
"hr_upscaler": "Latent",
"denoising_strength": 0.7,
"seed": seed,
"prompt": adjective + " " + noun + ", level design render, wide view, dim volumetric lighting, retrofuturism",
"negative_prompt": "text, 2d, screenshot, watermark",
})
if err != nil {
panic(err)
}
req, err := http.NewRequest(http.MethodPost, *apiHost+"/sdapi/v1/txt2img", bytes.NewReader(query))
if err != nil {
panic(err)
}
req.Header.Set("User-Agent", "ReactiveDropMapThemeScript/1.0")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
panic("unexpected response status " + resp.Status + " for A:" + adjective + " N:" + noun + " S:" + strconv.Itoa(seed))
}
var data struct {
Images [][]byte `json:"images"`
Parameters json.RawMessage `json:"parameters"`
Info string `json:"info"`
}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
panic(err)
}
return data.Images[0]
}