@@ -83,6 +83,12 @@ protected function buildEnvDetection()
83
83
*/
84
84
protected function cloneOpenCV (string $ directory )
85
85
{
86
+
87
+ //检测OpenCV是否已经存在,如果已经存在则过滤
88
+ if (file_exists ($ directory . '/opencv ' )) {
89
+ //todo 切回到某个版本
90
+ return ;
91
+ }
86
92
$ version = self ::OPENCV_VERSION ;
87
93
$ opencvUrl = 'https://github.com/opencv/opencv.git ' ;
88
94
$ command = "git clone {$ opencvUrl } --branch {$ version } --depth 1 " ;
@@ -104,6 +110,12 @@ protected function cloneOpenCV(string $directory)
104
110
*/
105
111
protected function cloneOpenCVContrib (string $ directory )
106
112
{
113
+
114
+ //检测OpenCV是否已经存在,如果已经存在则过滤
115
+ if (file_exists ($ directory . '/opencv_contrib ' )) {
116
+ //todo 切回到某个版本
117
+ return ;
118
+ }
107
119
$ version = self ::OPENCV_VERSION ;
108
120
$ opencvContribUrl = 'https://github.com/opencv/opencv_contrib.git ' ;
109
121
$ command = "git clone {$ opencvContribUrl } --branch {$ version } --depth 1 " ;
@@ -117,6 +129,12 @@ protected function cloneOpenCVContrib(string $directory)
117
129
}
118
130
119
131
132
+ /**
133
+ * 检测OpenCV是否已经安装
134
+ * @author hihozhou
135
+ *
136
+ * @param OutputInterface $output
137
+ */
120
138
protected function findExistOpenCV (OutputInterface $ output )
121
139
{
122
140
$ output ->writeln ('Try to find the installed OpenCV on the system via pkg-config... ' );
@@ -125,10 +143,12 @@ protected function findExistOpenCV(OutputInterface $output)
125
143
$ process ->mustRun ();
126
144
$ existOpencvVersion = $ process ->getOutput ();
127
145
$ output ->writeln ('Found, opencv version is ' . $ existOpencvVersion );
146
+ return true ;
128
147
129
148
} catch (\Exception $ e ) {
130
149
//没有检测到opencv
131
150
$ output ->writeln ('Did not find opencv installed on the system. ' );
151
+ return false ;
132
152
}
133
153
}
134
154
@@ -195,30 +215,38 @@ protected function createBaseDir($directory, OutputInterface $output)
195
215
*/
196
216
public function buildOpenCV ($ directory )
197
217
{
218
+ if (!file_exists ($ directory )) {
219
+ $ process = new Process (['mkdir ' , 'build ' ], $ directory . '/opencv ' );
220
+ try {
221
+ $ process ->mustRun ();
222
+ } catch (\Exception $ e ) {
223
+ throw new RuntimeException ($ process ->getErrorOutput ());
224
+ }
225
+ }
226
+
227
+ $ cmakeCommand = 'cmake -D CMAKE_BUILD_TYPE=RELEASE ' ;
228
+ $ cmakeCommand .= ' -D CMAKE_INSTALL_PREFIX=/usr/local ' ;
229
+ $ cmakeCommand .= ' -D WITH_TBB=ON ' ;
230
+ $ cmakeCommand .= ' -D WITH_V4L=ON ' ;
231
+ $ cmakeCommand .= ' -D INSTALL_C_EXAMPLES=OFF ' ;
232
+ $ cmakeCommand .= ' -D INSTALL_PYTHON_EXAMPLES=OFF ' ;
233
+ $ cmakeCommand .= ' -D BUILD_EXAMPLES=OFF ' ;
234
+ $ cmakeCommand .= ' -D BUILD_JAVA=OFF ' ;
235
+ $ cmakeCommand .= ' -D BUILD_TESTS=OFF ' ;
236
+ $ cmakeCommand .= ' -D WITH_QT=ON ' ;
237
+ $ cmakeCommand .= ' -D WITH_OPENGL=ON ' ;
238
+ $ cmakeCommand .= ' -D OPENCV_PYTHON_SKIP_DETECTION=ON ' ;
239
+ $ cmakeCommand .= ' -D OPENCV_GENERATE_PKGCONFIG=ON ' ;
240
+ $ cmakeCommand .= ' -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ' ;
241
+ $ cmakeCommand .= ' .. ' ;
198
242
//编译安装
199
243
$ commands = [
200
- 'cd opencv ' ,
201
- 'mkdir build ' ,
202
- 'cd build ' ,
203
- 'cmake -D CMAKE_BUILD_TYPE=RELEASE \
204
- -D CMAKE_INSTALL_PREFIX=/usr/local \
205
- -D WITH_TBB=ON \
206
- -D WITH_V4L=ON \
207
- -D INSTALL_C_EXAMPLES=OFF \
208
- -D INSTALL_PYTHON_EXAMPLES=OFF \
209
- -D BUILD_EXAMPLES=OFF \
210
- -D BUILD_JAVA=OFF \
211
- -D BUILD_TESTS=OFF \
212
- -D WITH_QT=ON \
213
- -D WITH_OPENGL=ON \
214
- -D BUILD_opencv_world=ON \
215
- -D OPENCV_PYTHON_SKIP_DETECTION=ON \
216
- -D OPENCV_GENERATE_PKGCONFIG=ON \
217
- -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..\
218
- && make\
219
- && sudo make install ' ,
220
- 'sh -c \'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf \''
221
-
244
+ 'cd opencv/build ' ,
245
+ $ cmakeCommand ,
246
+ 'make ' ,
247
+ 'sudo make install ' ,
248
+ 'sudo sh -c \'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf \'' ,
249
+ 'sudo ldconfig '
222
250
];
223
251
$ process = new Process (implode (' && ' , $ commands ), $ directory , null , null , null );
224
252
$ process ->setTty (Process::isTtySupported ());//检查TTY支持
@@ -231,15 +259,31 @@ public function buildOpenCV($directory)
231
259
232
260
protected function buildPHPOpenCV ($ directory )
233
261
{
234
- $ phpOpencvUrl = 'https://github.com/hihozhou/php-opencv.git ' ;
235
- $ command = "git clone {$ phpOpencvUrl } --branch master --depth 1 " ;
236
- $ process = new Process ($ command , $ directory , null , null , null );
237
- $ process ->setTty (Process::isTtySupported ());//检查TTY支持
262
+
263
+ if (!file_exists ($ directory . '/php-opencv ' )) {
264
+ $ phpOpencvUrl = 'https://github.com/hihozhou/php-opencv.git ' ;
265
+ $ command = "git clone {$ phpOpencvUrl } --branch master --depth 1 " ;
266
+ $ process = new Process ($ command , $ directory , null , null , null );
267
+ $ process ->setTty (Process::isTtySupported ());//检查TTY支持
268
+ try {
269
+ $ process ->mustRun ();
270
+ } catch (\Exception $ e ) {
271
+ throw new RuntimeException ('Aborting. ' );
272
+ }
273
+ }
274
+
238
275
try {
276
+ $ commands = [
277
+ 'cd php-opencv ' ,
278
+ 'phpize ' ,//todo
279
+ './configure --with-php-config=/usr/bin/php-config ' ,//todo
280
+ 'make ' ,
281
+ 'sudo make install '
282
+ ];
283
+ $ process = new Process (implode (' && ' , $ commands ), $ directory , null , null , null );
284
+ $ process ->setTty (Process::isTtySupported ());//检查TTY支持
239
285
$ process ->mustRun ();
240
286
241
- //
242
-
243
287
} catch (\Exception $ e ) {
244
288
throw new RuntimeException ('Aborting. ' );
245
289
}
@@ -258,17 +302,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
258
302
$ this ->checkIsRoot ();
259
303
$ this ->checkExtensionIsInstall ($ output );
260
304
$ this ->buildEnvDetection ();
261
- $ this ->findExistOpenCV ($ output );
262
305
//创建目录
263
306
$ directory = $ input ->getOption ('path ' );
264
-
265
307
$ this ->createBaseDir ($ directory , $ output );
266
- //克隆项目
267
- $ this ->cloneOpenCV ($ directory );
268
- $ this ->cloneOpenCVContrib ($ directory );
269
308
270
- //编译扩展
271
- $ this ->buildOpenCV ($ directory );
309
+ if (!$ this ->findExistOpenCV ($ output )) {
310
+
311
+ //克隆项目
312
+ $ this ->cloneOpenCV ($ directory );
313
+ $ this ->cloneOpenCVContrib ($ directory );
314
+
315
+ //编译扩展
316
+ $ this ->buildOpenCV ($ directory );
317
+ }
318
+
272
319
//编译phpopencv扩展
273
320
$ this ->buildPHPOpenCV ($ directory );
274
321
0 commit comments