Skip to content

Commit 7a4ee0b

Browse files
committed
php opencv install
0 parents  commit 7a4ee0b

5 files changed

+156
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
/vendor/
3+
composer.lock

composer.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "hihozhou/php-opencv-installer",
3+
"description": "PHPOpenCV installer.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "hihozhou",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"CV\\Installer\\Console\\": "src/"
15+
}
16+
},
17+
"require": {
18+
"php": ">=7.0.0",
19+
"symfony/console": "^4.3@dev",
20+
"symfony/process": "^4.3@dev"
21+
},
22+
"minimum-stability": "dev"
23+
}

installer

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env php
2+
3+
<?php
4+
if (file_exists(__DIR__.'/../../autoload.php')) {
5+
require __DIR__.'/../../autoload.php';
6+
} else {
7+
require __DIR__.'/vendor/autoload.php';
8+
}
9+
$app = new Symfony\Component\Console\Application('PHPOpenCV Installer', '1.0.0');
10+
$app->add(new CV\Installer\Console\InstallCommand);
11+
$app->run();
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
# Detecting cmake on the system https://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script
4+
command -v cmake >/dev/null 2>&1 || { echo >&2 "Compiling OpenCV requires cmake, but no cmake was detected for system installation. Aborting."; exit 1; }
5+
command -v pkg-config >/dev/null 2>&1 || { echo >&2 "Compiling OpenCV requires pkg-config, but no cmake was detected for system installation. Aborting."; exit 1; }

src/InstallCommand.php

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace CV\Installer\Console;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Exception\RuntimeException;
7+
use Symfony\Component\Console\Input\InputArgument;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Input\InputOption;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
use Symfony\Component\Process\Exception\ProcessFailedException;
12+
use Symfony\Component\Process\Process;
13+
14+
class InstallCommand extends Command
15+
{
16+
17+
const EXTENSION_NAME = 'opencv';
18+
19+
/**
20+
* Configure the command options.
21+
*
22+
* @return void
23+
*/
24+
protected function configure()
25+
{
26+
$this
27+
->setName('install')
28+
->setDescription('Automatic installation of the php-opencv extension, including automatic installation of opencv (if no opencv is installed)')
29+
->addArgument('opencv_build_path', InputArgument::OPTIONAL, 'Automatically install the opencv directory', '/opt/opencv')
30+
// ->addOption('dev', null, InputOption::VALUE_NONE, 'Installs the latest "development" release')
31+
// ->addOption('force', 'f', InputOption::VALUE_NONE, 'Forces install even if the directory already exists')
32+
;
33+
}
34+
35+
36+
protected function buildEnvDetection()
37+
{
38+
$process = new Process(['./opencv-install-environment-detection.sh']);//给予当前用户
39+
try {
40+
$process->mustRun();
41+
} catch (\Exception $e) {
42+
throw new RuntimeException($process->getErrorOutput());
43+
}
44+
}
45+
46+
/**
47+
* @param string $directory
48+
*/
49+
protected function cloneOpenCV(string $directory)
50+
{
51+
$opencvUrl = 'https://github.com/opencv/opencv.git';
52+
$command = "sudo git clone {$opencvUrl} --depth 1";
53+
$process = new Process($command, $directory, null, null, null);//给予当前用户
54+
$process->setTty(Process::isTtySupported());//检查TTY支持
55+
try {
56+
$process->mustRun();
57+
} catch (\Exception $e) {
58+
throw new RuntimeException($process->getErrorOutput());
59+
}
60+
}
61+
62+
63+
protected function cloneOpenCVContrib(string $directory)
64+
{
65+
$opencvContribUrl = 'https://github.com/opencv/opencv_contrib.git';
66+
$command = "sudo git clone {$opencvContribUrl} --depth 1";
67+
$process = new Process($command, $directory, null, null, null);//给予当前用户
68+
$process->setTty(Process::isTtySupported());//检查TTY支持
69+
try {
70+
$process->mustRun();
71+
} catch (\Exception $e) {
72+
throw new RuntimeException($process->getErrorOutput());
73+
}
74+
}
75+
76+
/**
77+
* Execute the command.
78+
*
79+
* @param \Symfony\Component\Console\Input\InputInterface $input
80+
* @param \Symfony\Component\Console\Output\OutputInterface $output
81+
*
82+
* @return void
83+
*/
84+
protected function execute(InputInterface $input, OutputInterface $output)
85+
{
86+
87+
88+
//判断是否已经安装opencv扩展
89+
if (extension_loaded(self::EXTENSION_NAME)) {
90+
$process = new Process(['php', '--ri', self::EXTENSION_NAME]);
91+
$process->mustRun();
92+
$output->writeln($process->getOutput());
93+
throw new RuntimeException('The OpenCV PHP extension is installed.');
94+
}
95+
$this->buildEnvDetection();
96+
//创建目录
97+
$directory = $input->getArgument('opencv_build_path');
98+
$output->writeln("Compile the directory of opencv with {$directory}.");
99+
if (!file_exists($directory)) {
100+
$output->writeln("Create {$directory} of the directory");
101+
$process = new Process(['sudo', 'mkdir', $directory]);//给予当前用户
102+
try {
103+
$process->mustRun();
104+
} catch (\Exception $e) {
105+
throw new RuntimeException($process->getErrorOutput());
106+
}
107+
108+
}
109+
$this->cloneOpenCV($directory);
110+
$this->cloneOpenCVContrib($directory);
111+
//编译安装
112+
$output->writeln('<comment>Application ready! Build something amazing.</comment>');
113+
}
114+
}

0 commit comments

Comments
 (0)