Skip to content

Commit da11eb4

Browse files
committed
Add Readme.
1 parent 79571a6 commit da11eb4

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

README.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
2+
3+
4+
5+
6+
<p align="center">
7+
<img src="./assets/sd-java-txt2img-0.jpg" width="256x">
8+
<img src="./assets/sd-java-img2img-0.jpg" width="256x">
9+
10+
</p>
11+
12+
# stable-diffusion.java
13+
14+
Java binding project of [stable-diffusion.cpp](https://github.com/leejet/stable-diffusion.cpp)
15+
16+
17+
18+
## Usage
19+
20+
### Add Maven depends:
21+
22+
```
23+
<dependency>
24+
<groupId>org.javaai.stablediffusion</groupId>
25+
<artifactId>StableDiffusionApi</artifactId>
26+
<version>1.0.0</version>
27+
</dependency>
28+
```
29+
30+
### Download models:
31+
32+
- download original weights(.ckpt or .safetensors). For example
33+
- Stable Diffusion v1.4 from https://huggingface.co/CompVis/stable-diffusion-v-1-4-original
34+
- Stable Diffusion v1.5 from https://huggingface.co/runwayml/stable-diffusion-v1-5
35+
- Stable Diffuison v2.1 from https://huggingface.co/stabilityai/stable-diffusion-2-1
36+
37+
```shell
38+
curl -L -O https://huggingface.co/CompVis/stable-diffusion-v-1-4-original/resolve/main/sd-v1-4.ckpt
39+
# curl -L -O https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors
40+
# curl -L -O https://huggingface.co/stabilityai/stable-diffusion-2-1/resolve/main/v2-1_768-nonema-pruned.safetensors
41+
```
42+
43+
#### Load StableDiffusion dynamic link library:
44+
```Java
45+
StableDiffusionLoader.loadShared();
46+
```
47+
48+
#### Create StableDiffusion instance:
49+
```Java
50+
StableDiffusion stableDiffusion = new StableDiffusion();
51+
```
52+
53+
#### Load Model:
54+
```Java
55+
sd.loadFromFile(modelPath, null, null, null);
56+
```
57+
58+
#### txt2img Example:
59+
```Java
60+
StableResult<Txt2ImgParams, BufferedImage> imgs = sd.txt2img("a lovely cat",
61+
null, null, null, null, null, 20,
62+
(long)(Math.random() * Long.MAX_VALUE), null);
63+
```
64+
#### Write output images to disk:
65+
```Java
66+
for (int i = 0; i < imgs.getResultImages().size(); i ++) {
67+
BufferedImage img = imgs.getResultImages().get(i);
68+
ImageIO.write(img, "jpg", new File(outputDir, "sd-java-txt2img-" + i + ".jpg"));
69+
}
70+
```
71+
72+
### After that, you will got images like this:
73+
<p align="center">
74+
<img src="./assets/sd-java-txt2img-0.jpg" width="256x">
75+
</p>
76+
77+
### img2img Example:
78+
Input previous generated image, and give the prompt:
79+
#####a lovely cat with blue eyes
80+
Let's see what will happend:
81+
82+
### Java Code:
83+
```Java
84+
BufferedImage inputImg = ImageIO.read(new File(outputDir, "sd-java-txt2img-0.jpg"));
85+
StableResult<Img2ImgParams, BufferedImage> imgs = sd.img2img(inputImg,
86+
"a lovely cat with blue eyes",
87+
null, null, null, null, null, 20, null,
88+
(long)(Math.random() * Long.MAX_VALUE));
89+
for (int i = 0; i < imgs.getResultImages().size(); i ++) {
90+
BufferedImage img = imgs.getResultImages().get(i);
91+
92+
ImageIO.write(img, "jpg", new File(outputDir, "sd-java-img2img-" + i + ".jpg"));
93+
}
94+
```
95+
96+
97+
### Twice generated images comparison:
98+
99+
100+
<p align="center">
101+
<div>The second cat has blue eyes. </div>
102+
<div align="center">
103+
<img src="./assets/sd-java-txt2img-0.jpg" width="256x">
104+
<img src="./assets/sd-java-img2img-0.jpg" width="256x">
105+
</div>
106+
107+
</p>
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+

assets/sd-java-img2img-0.jpg

23.9 KB
Loading

assets/sd-java-txt2img-0.jpg

24.4 KB
Loading

0 commit comments

Comments
 (0)