Tips & tricks for text detection from images using Google Vision API in PHP

Tips & tricks for text detection from images using Google Vision API in PHP

In this tutorial, I will let you know how to get content of images with Google Vision API.

Google Vision API can easily detect and extract text from an image. There are two annotation features (TEXT_DETECTION, DOCUMENT_TEXT_DETECTION) that perform Optical Character Recognition and return the extracted text, if any.

In order to use Google Vision API, you will have to create new project or select an existing project and enable the Vision API.

Example
<!DOCTYPE html>
<html>
<head>
	<title>Tips & tricks for text detection from images using Google Vision API</title>
</head>
<body>

	<form enctype="multipart/form-data" action="" method="POST">
		Choose an image to upload: <input name="image" type="file" /><br />
		<input type="submit" value="Upload Document" />
	</form>

<?php

$url = "https://vision.googleapis.com/v1/images:annotate?key={api_key}";
$detection_type = "TEXT_DETECTION";
$image_validation = array('image/jpeg','image/png','image/gif');

if($_FILES){

	// validate uploaded file for allowed mime type
	if(in_array($_FILES['image']['type'],$image_validation)){	    	

		// base64 encode image
    	$image = file_get_contents($_FILES['image']['tmp_name']);
		$image_base64 = base64_encode($image);

		$json_request ='{
			  	"requests": [
					{
					"image": {
					    "content":"' . $image_base64. '"
					  },
					  "features": [
					      {
					      	"type": "' .$detection_type. '",
					      	"maxResults": 200
					      }
					  ]
					}
				]
			}';

		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
		curl_setopt($curl, CURLOPT_POST, true);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $json_request);
		$json_response = curl_exec($curl);
		$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
		curl_close($curl);

		// verify if we got a correct response
		if ( $status != 200 ) {
			die("Something when wrong. Status code: $status" );
		}
		
		// create an image identifier for the uploaded file
		switch($_FILES['image']['type']){
			case 'image/jpeg':
				$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
				break;
			case 'image/png':
				$im = imagecreatefrompng($_FILES['image']['tmp_name']);
				break;
			case 'image/gif':
				$im = imagecreatefromgif($_FILES['image']['tmp_name']);
				break;
		}

		// transform the json response to an associative array
		$response = json_decode($json_response, true);			
		// display the first text annotation
		echo'<pre>';
		print_r($response['responses'][0]['textAnnotations'][0]['description']);
		echo'</pre>';
	
    }
    else{
    	echo 'File type not allowed';
    }

	}
?>

</body>
</html>

Phone: (+91) 8800417876
Noida, 201301
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
porno izle
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat
sohbet okey oyna
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort