|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Service; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\Image; |
|
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
7
|
|
|
use Google\Cloud\Vision\V1\Feature; |
|
8
|
|
|
use Google\Cloud\Vision\V1\Feature\Type; |
|
9
|
|
|
use Google\Cloud\Vision\V1\ImageAnnotatorClient; |
|
10
|
|
|
use Psr\Log\LoggerInterface; |
|
11
|
|
|
|
|
12
|
|
|
class GoogleImageTaggingService implements ImageTaggingServiceInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var EntityManagerInterface */ |
|
15
|
|
|
private $entityManager; |
|
16
|
|
|
|
|
17
|
|
|
/** @var ImageAnnotatorClient */ |
|
18
|
|
|
private $client; |
|
19
|
|
|
|
|
20
|
|
|
/** @var LoggerInterface */ |
|
21
|
|
|
private $logger; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct( |
|
24
|
|
|
string $projectId, |
|
25
|
|
|
string $serviceAccountFile, |
|
26
|
|
|
EntityManagerInterface $entityManager, |
|
27
|
|
|
LoggerInterface $logger |
|
28
|
|
|
) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->client = new ImageAnnotatorClient([ |
|
31
|
|
|
'projectId' => $projectId, |
|
32
|
|
|
'credentials' => $serviceAccountFile |
|
33
|
|
|
]); |
|
34
|
|
|
$this->entityManager = $entityManager; |
|
35
|
|
|
$this->logger = $logger; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function tagImage(Image $image, bool $overwriteExisting = false): bool |
|
39
|
|
|
{ |
|
40
|
|
|
if ($overwriteExisting === false && ($image->getAutoTagsCount() > 0 || $image->getTextTagsCount() > 0)) { |
|
41
|
|
|
return false; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$labelDetection = new Feature(); |
|
45
|
|
|
$labelDetection->setType(Type::LABEL_DETECTION); |
|
46
|
|
|
$labelDetection->setMaxResults(15); |
|
47
|
|
|
|
|
48
|
|
|
$textDetection = new Feature(); |
|
49
|
|
|
$textDetection->setType(Type::TEXT_DETECTION); |
|
50
|
|
|
$textDetection->setMaxResults(100); |
|
51
|
|
|
|
|
52
|
|
|
$result = $this->client->annotateImage( |
|
53
|
|
|
$this->getImageToSend($image), |
|
54
|
|
|
[$labelDetection, $textDetection] |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$tags = []; |
|
58
|
|
|
foreach ($result->getLabelAnnotations() as $annotation) { |
|
59
|
|
|
$tags[] = $annotation->getDescription(); |
|
60
|
|
|
} |
|
61
|
|
|
$image->setAutoTags($tags); |
|
62
|
|
|
|
|
63
|
|
|
$tags = []; |
|
64
|
|
|
foreach ($result->getTextAnnotations() as $annotation) { |
|
65
|
|
|
$tags[] = $annotation->getDescription(); |
|
66
|
|
|
} |
|
67
|
|
|
$image->setTextTags($tags); |
|
68
|
|
|
|
|
69
|
|
|
$this->entityManager->persist($image); |
|
70
|
|
|
$this->entityManager->flush(); // Calling the API's a lot more overhead; we might as well flush on every image. |
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* |
|
76
|
|
|
* Can be overridden, e.g. our dev service overrides this to provide |
|
77
|
|
|
* the actual image data through readfile as the dev service's URLs |
|
78
|
|
|
* aren't public. |
|
79
|
|
|
* |
|
80
|
|
|
* @return mixed|string |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function getImageToSend(Image $image) |
|
83
|
|
|
{ |
|
84
|
|
|
// We just send back a reasonable-resolution public URL |
|
85
|
|
|
return $image->getMediumImageUri(); |
|
86
|
|
|
} |
|
87
|
|
|
} |