Skip to content

Commit 9318eec

Browse files
committed
First draft of crop hints
1 parent 81d9ae3 commit 9318eec

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
19+
namespace Google\Cloud\Samples\Vision;
20+
21+
use Symfony\Component\Console\Command\Command;
22+
use Symfony\Component\Console\Input\InputArgument;
23+
use Symfony\Component\Console\Input\InputOption;
24+
use Symfony\Component\Console\Input\InputInterface;
25+
use Symfony\Component\Console\Output\OutputInterface;
26+
27+
/**
28+
* Command line utility to detect which language some text is written in.
29+
*/
30+
class DetectCropHintsCommand extends Command
31+
{
32+
protected function configure()
33+
{
34+
$this
35+
->setName('crop-hints')
36+
->setDescription('Detect crop hints in an image using '
37+
. 'Google Cloud Vision API')
38+
->setHelp(<<<EOF
39+
The <info>%command.name%</info> command prints crop hints for an image using
40+
the Google Cloud Vision API.
41+
42+
<info>php %command.full_name% -k YOUR-API-KEY path/to/image.png</info>
43+
44+
EOF
45+
)
46+
->addArgument(
47+
'path',
48+
InputArgument::REQUIRED,
49+
'The image to examine.'
50+
)
51+
->addOption(
52+
'project',
53+
'p',
54+
InputOption::VALUE_REQUIRED,
55+
'Your Project ID.'
56+
)
57+
;
58+
}
59+
60+
protected function execute(InputInterface $input, OutputInterface $output)
61+
{
62+
$projectId = $input->getOption('project');
63+
$path = $input->getArgument('path');
64+
if (preg_match('/^gs:\/\/([a-z0-9\._\-]+)\/(\S+)$/', $path, $matches)) {
65+
list($bucketName, $objectName) = array_slice($matches, 1);
66+
$result = require __DIR__ . '/snippets/detect_crop_hints_gcs.php';
67+
} else {
68+
$result = require __DIR__ . '/snippets/detect_crop_hints.php';
69+
}
70+
}
71+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Vision;
19+
20+
// [START vision_crop_hint_detection]
21+
use Google\Cloud\Vision\VisionClient;
22+
23+
// $projectId = 'YOUR_PROJECT_ID';
24+
// $path = 'path/to/your/image.jpg'
25+
26+
$vision = new VisionClient([
27+
'projectId' => $projectId,
28+
]);
29+
$image = $vision->image(file_get_contents($path), ['CROP_HINTS']);
30+
$result = $vision->annotate($image);
31+
print("Crop Hints:\n");
32+
foreach ((array) $result->cropHints() as $hint) {
33+
$boundingPoly = $hint->boundingPoly();
34+
$vertices = $boundingPoly['vertices'];
35+
foreach ((array) $vertices as $vertice) {
36+
if (!isset($vertice['x'])) $vertice['x'] = 0;
37+
if (!isset($vertice['y'])) $vertice['y'] = 0;
38+
print('X: ' . $vertice['x'] . ' Y: ' . $vertice['y'] . PHP_EOL);
39+
}
40+
}
41+
// [END vision_crop_hint_detection]

vision/api/vision.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@
2727
$application->add(new Google\Cloud\Samples\Vision\DetectLogoCommand());
2828
$application->add(new Google\Cloud\Samples\Vision\DetectSafeSearchCommand());
2929
$application->add(new Google\Cloud\Samples\Vision\DetectImagePropertyCommand());
30+
$application->add(new Google\Cloud\Samples\Vision\DetectCropHintsCommand());
3031
$application->run();

0 commit comments

Comments
 (0)