Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 64878a5

Browse files
committed
opencv build
1 parent 593e9fc commit 64878a5

File tree

2 files changed

+112
-27
lines changed

2 files changed

+112
-27
lines changed

‎phpopencv

100644100755
File mode changed.

‎src/InstallCommand.php

Lines changed: 112 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class InstallCommand extends Command
1919

2020
protected $installInfo = [];
2121

22+
protected $isRoot = false;
23+
protected $systemUsername;
24+
25+
2226
/**
2327
* Configure the command options.
2428
*
@@ -33,12 +37,28 @@ protected function configure()
3337
// ->addArgument('version', InputArgument::OPTIONAL, 'Automatically install the opencv directory', '/opt/opencv')
3438
// ->addOption('dev', null, InputOption::VALUE_NONE, 'Installs the latest "development" release')
3539
// ->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')
3741
// ->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')
3842
;
3943
}
4044

4145

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+
4262
/**
4363
* 检测安装环境
4464
* @author hihozhou
@@ -65,7 +85,7 @@ protected function cloneOpenCV(string $directory)
6585
{
6686
$version = self::OPENCV_VERSION;
6787
$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";
6989
$process = new Process($command, $directory, null, null, null);//给予当前用户
7090
$process->setTty(Process::isTtySupported());//检查TTY支持
7191
try {
@@ -86,7 +106,7 @@ protected function cloneOpenCVContrib(string $directory)
86106
{
87107
$version = self::OPENCV_VERSION;
88108
$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";
90110
$process = new Process($command, $directory, null, null, null);//给予当前用户
91111
$process->setTty(Process::isTtySupported());//检查TTY支持
92112
try {
@@ -112,57 +132,122 @@ protected function findExistOpenCV(OutputInterface $output)
112132
}
113133
}
114134

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

136+
protected function checkExtensionIsInstall(OutputInterface $output)
137+
{
126138
//判断是否已经安装opencv扩展
127139
if (extension_loaded(self::EXTENSION_NAME)) {
128140
$process = new Process(['php', '--ri', self::EXTENSION_NAME]);
129141
$process->mustRun();
130142
$output->writeln($process->getOutput());
131143
throw new RuntimeException('The OpenCV PHP extension is installed.');
132144
}
133-
$this->buildEnvDetection();
134-
$this->findExistOpenCV($output);
145+
}
135146

136-
//创建目录
137-
$directory = $input->getOption('path');
147+
protected function createBaseDir($directory, OutputInterface $output)
148+
{
149+
150+
//提示安装目录
138151
$output->writeln("Compile the directory of opencv with {$directory}.");
152+
//
139153
if (!file_exists($directory)) {
140154
$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+
}
142160
try {
143161
$process->mustRun();
144162
} catch (\Exception $e) {
145163
throw new RuntimeException($process->getErrorOutput());
146164
}
147165

148166
}
149-
//克隆项目
150-
// $this->cloneOpenCV($directory);
151-
// $this->cloneOpenCVContrib($directory);
152167

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+
{
153192
//编译安装
154193
$commands = [
155194
'cd opencv',
156-
'sudo mkdir build',
195+
'mkdir build',
157196
'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+
159216
];
160-
$cloneOpenCVDirectory = $directory . '/opencv';
161217
$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+
165248
//编译扩展
249+
$this->buildOpenCV($directory);
250+
166251

167252
$output->writeln('<comment>Application ready! Build something amazing.</comment>');
168253
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /