Skip to content

Commit 5d3ce13

Browse files
committed
Finalize stylization
1 parent 8cffe30 commit 5d3ce13

File tree

6 files changed

+166
-15
lines changed

6 files changed

+166
-15
lines changed

Readme.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,11 @@ If you have a usecase that's not listed above, please create an [issue](https://
2929

3030
This downloads tensorflow to `./tensorflow`.
3131

32-
#### 3. Download the required models
33-
Until we've figured out a solution for how to host the final models, the following step is a bit more difficult than we'd like it to be.
32+
#### 3. Download a model
3433

35-
You'll need to have python installed (locally), and some sort of way to host the model files yourself.
34+
php vendor/funcai/funcai-php/install-stylization.php
3635

37-
To generate the model, run:
38-
39-
pip3 install tensorflow
40-
python3 vendor/funcai/funcai-php/scripts/generate/efficientnet.py
41-
42-
The model will be placed in vendor/funcai/funcai-php/models.
36+
This downloads the stylization model to `./models`
4337

4438
#### 4. Configure the models folder
4539
You will need to move the models folder to a permanent location.
@@ -53,8 +47,14 @@ You can also just move the folder directly into your project and check them into
5347
After you've completed the installation steps you can run your first prediction:
5448

5549
\FuncAI\Config::setLibPath('./tensorflow/'); // This should point to the path from step 2
56-
$model = new \FuncAI\Models\EfficientNet();
57-
$output = $model->predict('./vendor/funcai/funcai-php/sample_data/butterfly.jpg');
50+
\FuncAI\Config::setModelBasePath('./models'); // This should point to the path from step 4
51+
$model = new \FuncAI\Models\Stylization();
52+
$model->predict([
53+
__DIR__ . '/sample_data/prince-akachi.jpg',
54+
__DIR__ . '/sample_data/style.jpg',
55+
]);
56+
57+
This will output the stylized image to `./out.jpg`.
5858

5959
## Requirements
6060
- PHP >= 7.4 on Linux

example-style.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
* of an image.
3232
*/
3333
$model = new \FuncAI\Models\Stylization();
34-
#$model->printGraph();
3534
$output = $model->predict([
36-
__DIR__ . '/sample_data/mona-lisa.jpg',
37-
__DIR__ . '/sample_data/style5.jpg',
35+
__DIR__ . '/sample_data/prince-akachi.jpg',
36+
__DIR__ . '/sample_data/style.jpg',
3837
]);
39-
# var_dump($output);
38+
39+
echo "Saved the stylized image to ./out.jpg";

install-stylization.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
require __DIR__.'/../../autoload.php';
3+
4+
// Specify where tensorflow should be downloaded to
5+
\FuncAI\Config::setLibPath('./tensorflow/');
6+
\FuncAI\Config::setModelBasePath('./models');
7+
8+
$stylizationInstaller = new \FuncAI\Install\StylizationInstaller();
9+
$stylizationInstaller->install();

sample_data/prince-akachi.jpg

6.57 KB
Loading

sample_data/style.jpg

17.2 KB
Loading

src/Install/StylizationInstaller.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace FuncAI\Install;
4+
5+
use FuncAI\Config;
6+
use FuncAI\Models\Stylization;
7+
use PharData;
8+
9+
class StylizationInstaller
10+
{
11+
public function isInstalled()
12+
{
13+
if(!is_dir(Config::getModelBasePath())) {
14+
return false;
15+
}
16+
if(!$this->modelIsInstalled()) {
17+
return false;
18+
}
19+
return true;
20+
}
21+
22+
public function install()
23+
{
24+
if($this->isInstalled()) {
25+
return;
26+
}
27+
$model = new Stylization();
28+
$installPath = $model->getModelPath();
29+
echo "Starting to install the Stylization model to '$installPath'\n";
30+
if($this->isInstalled()) {
31+
echo "The stylization model is already installed.\n";
32+
return;
33+
}
34+
if(!$this->modelIsInstalled()) {
35+
echo "Installing model...\n";
36+
$this->installModel();
37+
}
38+
39+
echo "\nDone!\n\n";
40+
}
41+
42+
private function modelIsInstalled()
43+
{
44+
$model = new Stylization();
45+
$requiredFiles = [
46+
'saved_model.pb',
47+
];
48+
foreach($requiredFiles as $requiredFile) {
49+
if(!file_exists($model->getModelPath() . '/' . $requiredFile)) {
50+
return false;
51+
}
52+
}
53+
return true;
54+
}
55+
56+
private function installModel()
57+
{
58+
$model = new Stylization();
59+
if(!is_dir($model->getModelPath())) {
60+
mkdir($model->getModelPath(), 0777, true);
61+
}
62+
$this->downloadModel();
63+
}
64+
65+
private function downloadModel()
66+
{
67+
echo "Downloading model...\n";
68+
$tensorflowLib = 'https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2?tf-hub-format=compressed';
69+
$tmpfilePath = sys_get_temp_dir() . '/arbitrary-image-stylization-v1-256.tar.gz';
70+
$decompressedPath = sys_get_temp_dir() . '/arbitrary-image-stylization-v1-256.tar';
71+
$extractionPath = sys_get_temp_dir().'/arbitrary-image-stylization';
72+
73+
if(!file_exists($tmpfilePath)) {
74+
$tmpfile = fopen($tmpfilePath, "w");
75+
$options = array(
76+
CURLOPT_FILE => $tmpfile,
77+
CURLOPT_URL => $tensorflowLib,
78+
CURLOPT_FOLLOWLOCATION => true,
79+
CURLOPT_FAILONERROR => true,
80+
);
81+
82+
$handle = curl_init();
83+
curl_setopt_array($handle, $options);
84+
$result = curl_exec($handle);
85+
fclose($tmpfile);
86+
87+
if ($result === false) {
88+
throw new \Exception(curl_error($handle));
89+
}
90+
}
91+
92+
if(!file_exists($decompressedPath)) {
93+
$phar = new PharData($tmpfilePath);
94+
$phar->decompress();
95+
}
96+
$phar = new PharData($decompressedPath);
97+
98+
$model = new Stylization();
99+
$modelPath = $model->getModelPath();
100+
101+
$files = [
102+
'./saved_model.pb' => $modelPath . '/saved_model.pb',
103+
'./variables/variables.data-00000-of-00002' => $modelPath . '/variables/variables.data-00000-of-00002',
104+
'./variables/variables.data-00001-of-00002' => $modelPath . '/variables/variables.data-00001-of-00002',
105+
'./variables/variables.index' => $modelPath . '/variables/variables.index',
106+
];
107+
foreach($files as $from => $to) {
108+
if(!is_dir(dirname($to))) {
109+
mkdir(dirname($to), 0777, true);
110+
}
111+
$phar->extractTo($extractionPath, $from);
112+
rename(realpath($extractionPath. '/' . $from), $to);
113+
}
114+
115+
#unlink($tmpfilePath);
116+
#unlink($decompressedPath);
117+
#$this->deleteDirectory($extractionPath);
118+
}
119+
120+
private function deleteDirectory($dir) {
121+
if (!file_exists($dir)) {
122+
return true;
123+
}
124+
125+
if (!is_dir($dir)) {
126+
return unlink($dir);
127+
}
128+
129+
foreach (scandir($dir) as $item) {
130+
if ($item == '.' || $item == '..') {
131+
continue;
132+
}
133+
134+
if (!$this->deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
135+
return false;
136+
}
137+
138+
}
139+
140+
return rmdir($dir);
141+
}
142+
}

0 commit comments

Comments
 (0)