|
| 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