├── .github └── FUNDING.yml ├── .gitattributes ├── images ├── turtle.jpg └── kadinsky.jpg ├── LICENSE ├── README.md ├── js ├── jquery.keyframes.min.js └── style-transfer.js ├── style └── style-transfer.css └── index.html /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | ko_fi: bensonruan 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /images/turtle.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bensonruan/Neural-Style-Transfer/HEAD/images/turtle.jpg -------------------------------------------------------------------------------- /images/kadinsky.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bensonruan/Neural-Style-Transfer/HEAD/images/kadinsky.jpg -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Benson Ruan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Artistic Neural Style Transfer 2 | Artistic neural style transfer with magenta.js 3 | 4 | Artistic neural style transfer is an optimization technique used to take two images—a content image and a style reference image (such as an artwork by a famous painter)—and blend them together so the output image looks like the content image, but "painted" in the style of the style reference image. 5 | 6 | ## Live Demo 7 | **[https://bensonruan.com/when-ai-meets-art-neural-style-transfer-with-magenta-js/](https://bensonruan.com/when-ai-meets-art-neural-style-transfer-with-magenta-js/)** 8 | 9 | ![style-transfer](https://bensonruan.com/wp-content/uploads/2019/09/art_style_transfer.jpg) 10 | 11 | 12 | ## Installing 13 | Clone this repository to your local computer 14 | ``` bash 15 | git https://github.com/bensonruan/Neural-Style-Transfer.git 16 | ``` 17 | Point your localhost to the cloned root directory 18 | 19 | Browse to http://localhost/index.html 20 | 21 | 22 | ## Library 23 | * [jquery](https://code.jquery.com/jquery-3.3.1.min.js) - JQuery 24 | * [jQuery.Keyframes](https://github.com/Keyframes/jQuery.Keyframes) - This library allows dynamic generation of CSS keyframes with callback events and other niceness. 25 | * [magenta.js](https://github.com/tensorflow/magenta-js/tree/master/image) - JavaScript implementation of Magenta's image models uses TensorFlow.js for GPU-accelerated inference 26 | 27 | ## Support me 28 | [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/W7W6METMY) 29 | -------------------------------------------------------------------------------- /js/jquery.keyframes.min.js: -------------------------------------------------------------------------------- 1 | !function a(o,u,s){function f(t,e){if(!u[t]){if(!o[t]){var n="function"==typeof require&&require;if(!e&&n)return n(t,!0);if(l)return l(t,!0);var r=new Error("Cannot find module '"+t+"'");throw r.code="MODULE_NOT_FOUND",r}var i=u[t]={exports:{}};o[t][0].call(i.exports,function(e){return f(o[t][1][e]||e)},i,i.exports,a,o,u,s)}return u[t].exports}for(var l="function"==typeof require&&require,e=0;e 1 ) 20 | fileName = ( this.getAttribute( 'data-multiple-caption' ) || '' ).replace( '{count}', this.files.length ); 21 | else 22 | fileName = e.target.value.split( '\\' ).pop(); 23 | 24 | if( fileName ){ 25 | label.querySelector( 'span' ).innerHTML = fileName; 26 | if(this.attributes['target'].value=='content'){ 27 | loadImage(e, contentImg); 28 | }else if(this.attributes['target'].value=='style'){ 29 | loadImage(e,styleImg); 30 | } 31 | } 32 | else{ 33 | label.innerHTML = labelVal; 34 | } 35 | }); 36 | 37 | // Firefox bug fix 38 | input.addEventListener( 'focus', function(){ input.classList.add( 'has-focus' ); }); 39 | input.addEventListener( 'blur', function(){ input.classList.remove( 'has-focus' ); }); 40 | }); 41 | }); 42 | 43 | $('#contentImg').on('load', function () { 44 | contentHeight = $(this).height(); 45 | $('.canvasContainer').height(contentHeight); 46 | }).each(function() { 47 | if (this.complete) $(this).trigger('load'); 48 | }); 49 | 50 | $('#styleImg').on('load', function () { 51 | styleHeight = $(this).height(); 52 | }).each(function() { 53 | if (this.complete) $(this).trigger('load'); 54 | }); 55 | 56 | $(".btn-transfer").click(function () { 57 | startTransfering(); 58 | model.initialize().then(() => { 59 | stylize(); 60 | }); 61 | }); 62 | 63 | async function clearCanvas() { 64 | // Don't block painting until we've reset the state. 65 | await mi.tf.nextFrame(); 66 | ctx.clearRect(0, 0, canvas.width, canvas.height); 67 | await mi.tf.nextFrame(); 68 | } 69 | 70 | async function stylize() { 71 | await clearCanvas(); 72 | 73 | // Resize the canvas to be the same size as the source image. 74 | canvas.width = contentImg.width; 75 | canvas.height = contentImg.height; 76 | 77 | // This does all the work! 78 | model.stylize(contentImg, styleImg).then((imageData) => { 79 | finishTransfering(); 80 | ctx.putImageData(imageData, 0, 0); 81 | }); 82 | } 83 | 84 | function loadImage(event, imgElement) { 85 | const reader = new FileReader(); 86 | reader.onload = (e) => { 87 | imgElement.src = e.target.result; 88 | }; 89 | reader.readAsDataURL(event.target.files[0]); 90 | } 91 | 92 | function startTransfering() { 93 | $('.imageContainer').addClass('scanning'); 94 | playScan(); 95 | $('#transfering').removeClass('d-none'); 96 | $('#stylized').addClass('d-none'); 97 | } 98 | 99 | function finishTransfering() { 100 | $('.imageContainer').removeClass('scanning'); 101 | $('#transfering').addClass('d-none'); 102 | $('#stylized').removeClass('d-none'); 103 | } 104 | 105 | function playScan(){ 106 | $.keyframe.define([{ 107 | name: 'scan-content', 108 | '0%': {'top': '0px'}, 109 | '100%': {'top': contentHeight-15} 110 | }]); 111 | $.keyframe.define([{ 112 | name: 'scan-style', 113 | '0%': {'top': styleHeight-15}, 114 | '100%': {'top': '0px'} 115 | }]); 116 | 117 | $('.scan-content').resetKeyframe(function() { 118 | $(".scan-content").playKeyframe({ 119 | name: 'scan-content', 120 | duration: '3s', 121 | timingFunction: 'linear', 122 | delay: '0s', 123 | iterationCount: 'infinite', 124 | direction: 'alternate-reverse' 125 | }); 126 | }); 127 | 128 | $('.scan-style').resetKeyframe(function() { 129 | $(".scan-style").playKeyframe({ 130 | name: 'scan-style', 131 | duration: '3s', 132 | timingFunction: 'linear', 133 | delay: '0s', 134 | iterationCount: 'infinite', 135 | direction: 'alternate-reverse' 136 | }); 137 | }); 138 | } -------------------------------------------------------------------------------- /style/style-transfer.css: -------------------------------------------------------------------------------- 1 | .scanning{ 2 | margin:auto; 3 | position:relative; 4 | width:100%; 5 | } 6 | .scanning .screen 7 | { 8 | margin:auto; 9 | width:100%; 10 | } 11 | .scanning em:before, .scanning em:after, .scanning .screen:before, .scanning .screen:after { 12 | border-color: rgba(0, 0, 0, 0.8); 13 | content: ""; 14 | position: absolute; 15 | width: 19px; 16 | height: 16px; 17 | border-style: solid; 18 | border-width: 0px; 19 | z-index: 9999; 20 | } 21 | .scanning .screen:before { 22 | left: 0; 23 | top: 0; 24 | border-left-width: 3px; 25 | border-top-width: 3px; 26 | } 27 | .scanning .screen:after { 28 | right: 0; 29 | top: 0; 30 | border-right-width: 3px; 31 | border-top-width: 3px; 32 | } 33 | .scanning em:before { 34 | left: 0; 35 | bottom: 0; 36 | border-left-width: 3px; 37 | border-bottom-width: 3px; 38 | } 39 | .scanning em:after { 40 | right: 0; 41 | bottom: 0; 42 | border-right-width: 3px; 43 | border-bottom-width: 3px; 44 | } 45 | 46 | .scanning .scan 47 | { 48 | width: 100%; 49 | height: 15px; 50 | background-color: rgba(20, 206, 97, 0.5); 51 | position:absolute; 52 | z-index:9999; 53 | box-shadow:0px 0px 30px rgba(255,204,102,.5); 54 | top: 0; 55 | } 56 | 57 | .inputfile { 58 | width: 0.1px; 59 | height: 0.1px; 60 | opacity: 0; 61 | overflow: hidden; 62 | position: absolute; 63 | z-index: -1; 64 | } 65 | 66 | .inputfile + label { 67 | width: 100%; 68 | font-size: 1.25rem; 69 | /* 20px */ 70 | font-weight: 700; 71 | text-overflow: ellipsis; 72 | white-space: nowrap; 73 | cursor: pointer; 74 | display: inline-block; 75 | overflow: hidden; 76 | padding: 0.625rem 1.25rem; 77 | text-align: center; 78 | margin: 0; 79 | /* 10px 20px */ 80 | } 81 | 82 | .no-js .inputfile + label { 83 | display: none; 84 | } 85 | 86 | .inputfile:focus + label, 87 | .inputfile.has-focus + label { 88 | outline: 1px dotted #000; 89 | outline: -webkit-focus-ring-color auto 5px; 90 | } 91 | 92 | .inputfile + label * { 93 | /* pointer-events: none; */ 94 | /* in case of FastClick lib use */ 95 | } 96 | 97 | .inputfile + label svg { 98 | width: 1em; 99 | height: 1em; 100 | vertical-align: middle; 101 | fill: currentColor; 102 | margin-top: -0.25em; 103 | /* 4px */ 104 | margin-right: 0.25em; 105 | /* 4px */ 106 | } 107 | 108 | 109 | /* style red */ 110 | 111 | .inputfile-red + label { 112 | color: #f1e5e6; 113 | background-color: #d3394c; 114 | } 115 | 116 | .inputfile-red:focus + label, 117 | .inputfile-red.has-focus + label, 118 | .inputfile-red + label:hover { 119 | background-color: #722040; 120 | } 121 | 122 | /* style blue */ 123 | 124 | .inputfile-blue + label { 125 | color: #f1e5e6; 126 | background-color: #064ead; 127 | } 128 | 129 | .inputfile-blue:focus + label, 130 | .inputfile-blue.has-focus + label, 131 | .inputfile-blue + label:hover { 132 | background-color: #073168; 133 | } 134 | 135 | .fa-plus { 136 | font-size: 2em; 137 | color: green; 138 | } 139 | 140 | .btn-transfer { 141 | background: #a0047e; 142 | color: #f1e5e6; 143 | font-weight: 700; 144 | font-size: 1.25rem; 145 | padding: 10px 30px; 146 | } 147 | 148 | .btn-transfer:hover { 149 | color: #f1e5e6; 150 | background: #460237; 151 | } 152 | 153 | #transfering { 154 | font-size: 5em; 155 | color: #a0047e; 156 | margin-top: 10%; 157 | } 158 | 159 | .canvasContainer{ 160 | border: 1px dotted black; 161 | } 162 | 163 | @media screen and (min-width: 768px) and (max-width: 992px) { 164 | .fa-plus{ 165 | font-size: 2em; 166 | } 167 | .btn-transfer { 168 | padding: 10px 10px; 169 | } 170 | } 171 | @media screen and (min-width: 992px) { 172 | .fa-plus{ 173 | font-size: 3em; 174 | } 175 | } 176 | 177 | .arrowAnim { 178 | width: 100%; 179 | display: flex; 180 | justify-content: center; 181 | align-items: center; 182 | } 183 | .arrowAnim.top-to-bottom { 184 | height : 50px; 185 | } 186 | 187 | .arrow { 188 | width: 10px; 189 | height: 10px; 190 | border: 2.5px solid; 191 | 192 | } 193 | .arrowAnim.right-to-left .arrow { 194 | transform: rotate(-45deg); 195 | border-color: #064ead transparent transparent #064ead; 196 | } 197 | .arrowAnim.rightTop-to-leftBottom .arrow { 198 | transform: rotate(-90deg); 199 | border-color: #064ead transparent transparent #064ead; 200 | } 201 | .arrowAnim.left-to-right .arrow { 202 | transform: rotate(135deg); 203 | border-color: #d3394c transparent transparent #d3394c; 204 | } 205 | .arrowAnim.leftTop-to-rightBottom .arrow { 206 | transform: rotate(180deg); 207 | border-color: #d3394c transparent transparent #d3394c; 208 | } 209 | .arrowAnim.top-to-bottom .arrow { 210 | transform: rotate(-135deg); 211 | border-color: #d3394c transparent transparent #a0047e; 212 | } 213 | 214 | 215 | .arrowAnim.left-to-right .arrowSliding { 216 | position: absolute; 217 | -webkit-animation: slide-left-to-right 4s linear infinite; 218 | animation: slide-left-to-right 4s linear infinite; 219 | } 220 | .arrowAnim.leftTop-to-rightBottom .arrowSliding { 221 | position: absolute; 222 | -webkit-animation: slide-leftTop-to-rightBottom 4s linear infinite; 223 | animation: slide-leftTop-to-rightBottom 4s linear infinite; 224 | } 225 | .arrowAnim.right-to-left .arrowSliding { 226 | position: absolute; 227 | -webkit-animation: slide-right-to-left 4s linear infinite; 228 | animation: slide-right-to-left 4s linear infinite; 229 | } 230 | .arrowAnim.rightTop-to-leftBottom .arrowSliding { 231 | position: absolute; 232 | -webkit-animation: slide-rightTop-to-leftBottom 4s linear infinite; 233 | animation: slide-rightTop-to-leftBottom 4s linear infinite; 234 | } 235 | .arrowAnim.top-to-bottom .arrowSliding { 236 | position: absolute; 237 | -webkit-animation: slide-top-to-bottom 4s linear infinite; 238 | animation: slide-top-to-bottom 4s linear infinite; 239 | } 240 | 241 | .arrowAnim.right-to-left .arrowSliding.delay1, 242 | .arrowAnim.left-to-right .arrowSliding.delay1, 243 | .arrowAnim.leftTop-to-rightBottom .arrowSliding.delay1, 244 | .arrowAnim.rightTop-to-leftBottom .arrowSliding.delay1, 245 | .arrowAnim.top-to-bottom .arrowSliding.delay1 { 246 | -webkit-animation-delay: 1s; 247 | animation-delay: 1s; 248 | } 249 | .arrowAnim.right-to-left .arrowSliding.delay2, 250 | .arrowAnim.left-to-right .arrowSliding.delay2, 251 | .arrowAnim.leftTop-to-rightBottom .arrowSliding.delay2, 252 | .arrowAnim.rightTop-to-leftBottom .arrowSliding.delay2, 253 | .arrowAnim.top-to-bottom .arrowSliding.delay2 { 254 | -webkit-animation-delay: 2s; 255 | animation-delay: 2s; 256 | } 257 | .arrowAnim.right-to-left .arrowSliding.delay3, 258 | .arrowAnim.left-to-right .arrowSliding.delay3, 259 | .arrowAnim.leftTop-to-rightBottom .arrowSliding.delay3, 260 | .arrowAnim.rightTop-to-leftBottom .arrowSliding.delay3, 261 | .arrowAnim.top-to-bottom .arrowSliding.delay3 { 262 | -webkit-animation-delay: 3s; 263 | animation-delay: 3s; 264 | } 265 | 266 | @-webkit-keyframes slide-right-to-left { 267 | 0% { opacity:0; transform: translateX(5vw); } 268 | 20% { opacity:1; transform: translateX(3vw); } 269 | 80% { opacity:1; transform: translateX(-3vw); } 270 | 100% { opacity:0; transform: translateX(-5vw); } 271 | } 272 | @keyframes slide-right-to-left { 273 | 0% { opacity:0; transform: translateX(5vw); } 274 | 20% { opacity:1; transform: translateX(3vw); } 275 | 80% { opacity:1; transform: translateX(-3vw); } 276 | 100% { opacity:0; transform: translateX(-5vw); } 277 | } 278 | @-webkit-keyframes slide-rightTop-to-leftBottom { 279 | 0% { opacity:0; transform: translateX(5vw) translateY(5vw); } 280 | 20% { opacity:1; transform: translateX(3vw) translateY(3vw); } 281 | 80% { opacity:1; transform: translateX(-3vw) translateY(-3vw); } 282 | 100% { opacity:0; transform: translateX(-5vw) translateY(-5vw); } 283 | } 284 | @keyframes slide-rightTop-to-leftBottom { 285 | 0% { opacity:0; transform: translateX(5vw) translateY(-5vw); } 286 | 20% { opacity:1; transform: translateX(3vw) translateY(-3vw); } 287 | 80% { opacity:1; transform: translateX(-3vw) translateY(3vw); } 288 | 100% { opacity:0; transform: translateX(-5vw) translateY(5vw); } 289 | } 290 | @-webkit-keyframes slide-left-to-right { 291 | 0% { opacity:0; transform: translateX(-5vw); } 292 | 20% { opacity:1; transform: translateX(-3vw); } 293 | 80% { opacity:1; transform: translateX(3vw); } 294 | 100% { opacity:0; transform: translateX(5vw); } 295 | } 296 | @keyframes slide-left-to-right { 297 | 0% { opacity:0; transform: translateX(-5vw); } 298 | 20% { opacity:1; transform: translateX(-3vw); } 299 | 80% { opacity:1; transform: translateX(3vw); } 300 | 100% { opacity:0; transform: translateX(5vw); } 301 | } 302 | @-webkit-keyframes slide-leftTop-to-rightBottom { 303 | 0% { opacity:0; transform: translateX(-5vw) translateY(-5vw); } 304 | 20% { opacity:1; transform: translateX(-3vw) translateY(-3vw); } 305 | 80% { opacity:1; transform: translateX(3vw) translateY(3vw); } 306 | 100% { opacity:0; transform: translateX(5vw) translateY(5vw); } 307 | } 308 | @keyframes slide-leftTop-to-rightBottom { 309 | 0% { opacity:0; transform: translateX(-5vw) translateY(-5vw); } 310 | 20% { opacity:1; transform: translateX(-3vw) translateY(-3vw); } 311 | 80% { opacity:1; transform: translateX(3vw) translateY(3vw); } 312 | 100% { opacity:0; transform: translateX(5vw) translateY(5vw); } 313 | } 314 | @-webkit-keyframes slide-top-to-bottom { 315 | 0% { opacity:0; transform: translateY(-40px); } 316 | 20% { opacity:1; transform: translateY(-30px); } 317 | 80% { opacity:1; transform: translateY(10px); } 318 | 100% { opacity:0; transform: translateY(20px); } 319 | } 320 | @keyframes slide-top-to-bottom { 321 | 0% { opacity:0; transform: translateY(-40px); } 322 | 20% { opacity:1; transform: translateY(-30px); } 323 | 80% { opacity:1; transform: translateY(10px); } 324 | 100% { opacity:0; transform: translateY(20px); } 325 | } -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Arbitrary Style Transfer 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 |
17 |
18 |
19 |
20 | 21 | 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 | 77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 | 96 | 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 | 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 | --------------------------------------------------------------------------------