1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google Vision APIを使って、投稿画像が他サイトで流用されていないかチェックする

Posted at

概要

Google Vision APIの力を借りて、サイト内で投稿された画像が他サイトでも使われていないかチェックする処理を実装してみます。

Google Vision API

類似画像を探す、画像内のテキストを抽出する、顔を検出する等、画像に関する様々な処理を行えるAPI。
Google Vision API を参照。
一月あたり1,001回以上のAPI利用は有料となるため注意。

PHPでの実装

$url = チェックしたい画像のURL;
$key = APIキー;

// APIにアクセス
$ch = curl_init("https://vision.googleapis.com/v1/images:annotate?key=$key");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT,          10);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
	'requests' => [
		'image'      => [
			'source' => [
				'imageUri'   => $url
			]
		],
		'features'   => [
			[
				'type'       => 'WEB_DETECTION',
				'maxResults' => 20
			]
		]
	]
]));
$response = curl_exec($ch);
// 結果が取れなかった場合
if ($results === null || !isset($results['responses'][0]['webDetection']['pagesWithMatchingImages'][0])) {
	echo "一致無し\n";
	exit;
}

$items = [];
// 複数のサイトがヒットするのでループ
foreach ($results['responses'][0]['webDetection']['pagesWithMatchingImages'] as $matching_image) {
	$column = isset($matching_image['fullMatchingImages']) ? 'fullMatchingImages' : 'partialMatchingImages';
	$items[] = [
		'website' => $matching_image['pageTitle'] ?? '[サイト名無し]',
		'url'     => $matching_image['url'],
		'image'   => $matching_image[$column][0]['url'] ?? '[画像URL無し]'
	];
}

print_r($items);

結果

実際にチェックしてみた実例を貼りたいところですが、ここでは控えます。
感触としては、サイズは異なるものの内容自体は完全一致するような画像が多数ヒットします。
また、部分的に一致するものや、被さっている文字が異なるものも探してくれます。

しかし、1~2割程度、大外しするようです。
色味が似てるだけで全然違うサッカーのユニフォーム普通のTシャツなど。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?