@@ -19,6 +19,10 @@ class InstallCommand extends Command
19
19
20
20
protected $ installInfo = [];
21
21
22
+ protected $ isRoot = false ;
23
+ protected $ systemUsername ;
24
+
25
+
22
26
/**
23
27
* Configure the command options.
24
28
*
@@ -33,12 +37,28 @@ protected function configure()
33
37
// ->addArgument('version', InputArgument::OPTIONAL, 'Automatically install the opencv directory', '/opt/opencv')
34
38
// ->addOption('dev', null, InputOption::VALUE_NONE, 'Installs the latest "development" release')
35
39
// ->addOption('edition', 'e', InputOption::VALUE_REQUIRED, 'Automatically install the opencv directory', '/opt/opencv')// ->addOption('php-opencv-version', 'pov', InputOption::VALUE_NONE, 'Specify the installed php-opencv version')
36
- ->addOption ('path ' , 'p ' , InputOption::VALUE_REQUIRED , 'Automatically install the opencv directory ' , '/opt/opencv ' )// ->addOption('php-opencv-version', 'pov', InputOption::VALUE_NONE, 'Specify the installed php-opencv version')
40
+ ->addOption ('path ' , 'p ' , InputOption::VALUE_REQUIRED , 'Automatically install the opencv directory ' , '/opt/phpopencv ' )// ->addOption('php-opencv-version', 'pov', InputOption::VALUE_NONE, 'Specify the installed php-opencv version')
37
41
// ->addOption('enable-contrib', null, InputOption::VALUE_REQUIRED, 'Automatically install the opencv directory', false)// ->addOption('php-opencv-version', 'pov', InputOption::VALUE_NONE, 'Specify the installed php-opencv version')
38
42
;
39
43
}
40
44
41
45
46
+ protected function checkIsRoot ()
47
+ {
48
+ $ process = new Process (['whoami ' ]);
49
+ try {
50
+ $ process ->mustRun ();
51
+ $ username = str_replace (PHP_EOL , '' , $ process ->getOutput ());
52
+ if ($ username == 'root ' ) {
53
+ $ this ->isRoot = true ;
54
+ }
55
+ $ this ->systemUsername = $ username ;
56
+ } catch (\Exception $ e ) {
57
+ throw new RuntimeException ($ process ->getErrorOutput ());
58
+ }
59
+ }
60
+
61
+
42
62
/**
43
63
* 检测安装环境
44
64
* @author hihozhou
@@ -65,7 +85,7 @@ protected function cloneOpenCV(string $directory)
65
85
{
66
86
$ version = self ::OPENCV_VERSION ;
67
87
$ opencvUrl = 'https://github.com/opencv/opencv.git ' ;
68
- $ command = "sudo git clone {$ opencvUrl } --branch {$ version } --depth 1 " ;
88
+ $ command = "git clone {$ opencvUrl } --branch {$ version } --depth 1 " ;
69
89
$ process = new Process ($ command , $ directory , null , null , null );//给予当前用户
70
90
$ process ->setTty (Process::isTtySupported ());//检查TTY支持
71
91
try {
@@ -86,7 +106,7 @@ protected function cloneOpenCVContrib(string $directory)
86
106
{
87
107
$ version = self ::OPENCV_VERSION ;
88
108
$ opencvContribUrl = 'https://github.com/opencv/opencv_contrib.git ' ;
89
- $ command = "sudo git clone {$ opencvContribUrl } --branch {$ version } --depth 1 " ;
109
+ $ command = "git clone {$ opencvContribUrl } --branch {$ version } --depth 1 " ;
90
110
$ process = new Process ($ command , $ directory , null , null , null );//给予当前用户
91
111
$ process ->setTty (Process::isTtySupported ());//检查TTY支持
92
112
try {
@@ -112,57 +132,122 @@ protected function findExistOpenCV(OutputInterface $output)
112
132
}
113
133
}
114
134
115
- /**
116
- * Execute the command.
117
- *
118
- * @param \Symfony\Component\Console\Input\InputInterface $input
119
- * @param \Symfony\Component\Console\Output\OutputInterface $output
120
- *
121
- * @return void
122
- */
123
- protected function execute (InputInterface $ input , OutputInterface $ output )
124
- {
125
135
136
+ protected function checkExtensionIsInstall (OutputInterface $ output )
137
+ {
126
138
//判断是否已经安装opencv扩展
127
139
if (extension_loaded (self ::EXTENSION_NAME )) {
128
140
$ process = new Process (['php ' , '--ri ' , self ::EXTENSION_NAME ]);
129
141
$ process ->mustRun ();
130
142
$ output ->writeln ($ process ->getOutput ());
131
143
throw new RuntimeException ('The OpenCV PHP extension is installed. ' );
132
144
}
133
- $ this ->buildEnvDetection ();
134
- $ this ->findExistOpenCV ($ output );
145
+ }
135
146
136
- //创建目录
137
- $ directory = $ input ->getOption ('path ' );
147
+ protected function createBaseDir ($ directory , OutputInterface $ output )
148
+ {
149
+
150
+ //提示安装目录
138
151
$ output ->writeln ("Compile the directory of opencv with {$ directory }. " );
152
+ //
139
153
if (!file_exists ($ directory )) {
140
154
$ output ->writeln ("Create {$ directory } of the directory " );
141
- $ process = new Process (['sudo ' , 'mkdir ' , $ directory ]);//给予当前用户
155
+ if ($ this ->isRoot ) {
156
+ $ process = new Process (['mkdir ' , $ directory ]);
157
+ } else {
158
+ $ process = new Process (['sudo ' , 'mkdir ' , $ directory ]);
159
+ }
142
160
try {
143
161
$ process ->mustRun ();
144
162
} catch (\Exception $ e ) {
145
163
throw new RuntimeException ($ process ->getErrorOutput ());
146
164
}
147
165
148
166
}
149
- //克隆项目
150
- // $this->cloneOpenCV($directory);
151
- // $this->cloneOpenCVContrib($directory);
152
167
168
+ //如果不是root用户,则赋予目录当前用户
169
+ if (!$ this ->isRoot ) {
170
+ try {
171
+ $ groupsCommand = 'groups ' . $ this ->systemUsername ;
172
+ $ process = new Process ($ groupsCommand );
173
+ $ process ->mustRun ();
174
+ $ str = str_replace (PHP_EOL , '' , $ process ->getOutput ());
175
+ if (substr_count ($ str , $ this ->systemUsername ) >= 2 ) {
176
+ $ chownCommand = 'sudo chown -R ' . $ this ->systemUsername . ': ' . $ this ->systemUsername . '' . $ directory ;
177
+ } else {
178
+ $ chownCommand = 'sudo chown -R ' . $ this ->systemUsername . '' . $ directory ;
179
+ }
180
+ $ process = new Process ($ chownCommand );
181
+ $ process ->mustRun ();
182
+ } catch (\Exception $ e ) {
183
+ throw new RuntimeException ($ process ->getErrorOutput ());
184
+ }
185
+ //给予当前用户
186
+ }
187
+ }
188
+
189
+
190
+ public function buildOpenCV ($ directory )
191
+ {
153
192
//编译安装
154
193
$ commands = [
155
194
'cd opencv ' ,
156
- 'sudo mkdir build ' ,
195
+ 'mkdir build ' ,
157
196
'cd build ' ,
158
- 'pwd '
197
+ 'pwd ' ,
198
+ 'cmake -D CMAKE_BUILD_TYPE=RELEASE \
199
+ -D CMAKE_INSTALL_PREFIX=/usr/local \
200
+ -D WITH_TBB=ON \
201
+ -D WITH_V4L=ON \
202
+ -D INSTALL_C_EXAMPLES=OFF \
203
+ -D INSTALL_PYTHON_EXAMPLES=OFF \
204
+ -D BUILD_EXAMPLES=OFF \
205
+ -D BUILD_JAVA=OFF \
206
+ -D BUILD_TESTS=OFF \
207
+ -D WITH_QT=ON \
208
+ -D WITH_OPENGL=ON \
209
+ -D BUILD_opencv_world=ON \
210
+ -D OPENCV_PYTHON_SKIP_DETECTION=ON \
211
+ -D OPENCV_GENERATE_PKGCONFIG=ON \
212
+ -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..\
213
+ && make\
214
+ && sudo make install '
215
+
159
216
];
160
- $ cloneOpenCVDirectory = $ directory . '/opencv ' ;
161
217
$ process = new Process (implode (' && ' , $ commands ), $ directory , null , null , null );
162
- $ process ->run (function ($ type , $ line ) use ($ output ) {
163
- $ output ->write ($ line );
164
- });
218
+ $ process ->setTty (Process::isTtySupported ());//检查TTY支持
219
+ try {
220
+ $ process ->mustRun ();
221
+ } catch (\Exception $ e ) {
222
+ throw new RuntimeException ('Aborting. ' );
223
+ }
224
+ }
225
+
226
+ /**
227
+ * Execute the command.
228
+ *
229
+ * @param \Symfony\Component\Console\Input\InputInterface $input
230
+ * @param \Symfony\Component\Console\Output\OutputInterface $output
231
+ *
232
+ * @return void
233
+ */
234
+ protected function execute (InputInterface $ input , OutputInterface $ output )
235
+ {
236
+ $ this ->checkIsRoot ();
237
+ $ this ->checkExtensionIsInstall ($ output );
238
+ $ this ->buildEnvDetection ();
239
+ $ this ->findExistOpenCV ($ output );
240
+ //创建目录
241
+ $ directory = $ input ->getOption ('path ' );
242
+
243
+ $ this ->createBaseDir ($ directory , $ output );
244
+ //克隆项目
245
+ $ this ->cloneOpenCV ($ directory );
246
+ $ this ->cloneOpenCVContrib ($ directory );
247
+
165
248
//编译扩展
249
+ $ this ->buildOpenCV ($ directory );
250
+
166
251
167
252
$ output ->writeln ('<comment>Application ready! Build something amazing.</comment> ' );
168
253
}
0 commit comments