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.

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

	<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" />


$url = "{api_key}";
$detection_type = "TEXT_DETECTION";
$image_validation = array('image/jpeg','image/png','image/gif');


	// validate uploaded file for allowed mime type

		// 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);

		// 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
			case 'image/jpeg':
				$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
			case 'image/png':
				$im = imagecreatefrompng($_FILES['image']['tmp_name']);
			case 'image/gif':
				$im = imagecreatefromgif($_FILES['image']['tmp_name']);

		// transform the json response to an associative array
		$response = json_decode($json_response, true);			
		// display the first text annotation
    	echo 'File type not allowed';



Phone: (+91) 8800417876
Noida, 201301
sakarya escort sakarya escort sakarya escort sakarya escort serdivan escort izmir escort eporner
akyazı escort sakarya escort ferizli escort sakarya escort geyve escort sakarya escort hendek escort sakarya escort karasu escort sakarya escort sapanca escort serdivan escort webmaster forum ankara travesti