phpOCR es una librerÃa que nos permite reconocer el texto que esta impreso en un archivo de imágen (Optical Character Recognition). Aunque en sÃ, la librerÃa es bastante básica, ya que sólo reconoce texto en imágenes de un solo color y hay que entrenarla previamente para que alcance una precisión relevante, no deja de ser útil e interesante.
Publicado por Asier Marqués el Lunes, 25 de Junio del 2007
2 Respuestas to “phpOCR, reconocer texto en imágenes desde php”
Añade un comentario
Junio 26th, 2007 at 6:36 pm
SerÃa bueno que alguien se interese y extienda este proyecto, ya que es muy bueno
Febrero 19th, 2008 at 11:22 am
PLEASE give a solution for the below quoted error.When i m running the example.php class it displays the following error.
& 0xFF; $b = $rgb & 0xFF; if (($r==255)&&($g==255)&&($b==255)){ return COLOR_WHITE; } return COLOR_BLACK; } /** * Output image * */ function outputImage(){ header(’Content-type: image/png’); imagepng($this->image); } /** * create image for recognition * * @param string $img_file * @return image */ function createImageFromFile($img_file){ $img=0; $img_sz = getimagesize( $img_file ); switch( $img_sz[2] ){ case 1: $img = ImageCreateFromGif($img_file); break; case 2: $img = ImageCreateFromJpeg($img_file); break; case 3: $img = ImageCreateFromPng($img_file); break; } $this->width = $img_sz[0]; $this->height = $img_sz[1]; return $img; } } ?>
clear(); $this->name = $str; $this->createImage(); $this->charObject->setName($str); $this->charObject->setWidth($this->width); $this->charObject->setHeight($this->height); $this->makeCharObject(); } /** * Learn systems to understand symbols form image * * @param string $path * @param string $name */ function LearnFromImage($path, $name){ $this->clear(); $this->name = $name; $im = $this->createImageFromFile($path); $this->image = imagecreatetruecolor($this->width, $this->height); imagecopy($this->image, $im, 0, 0, 0, 0, $this->width, $this->height); $this->charObject->setName($name); $this->charObject->setWidth($this->width); $this->charObject->setHeight($this->height); $this->makeCharObject(); } /** * Create char object from image object * */ function makeCharObject(){ for ($x=0; $xwidth; $x++){ for ($y=0; $yheight; $y++){ $type = $this->getCornerType($this->image,$x,$y); if ($type!==false){ $this->charObject->setCorner($x, $y, $type); } } } } /** * Recognition method * * @param string $path * @return object */ function Recognition($path){ $this->clear(); $im = $this->createImageFromFile($path); $this->image = imagecreatetruecolor($this->width, $this->height); imagecopy($this->image, $im, 0, 0, 0, 0, $this->width, $this->height); $this->charObject->setWidth($this->width); $this->charObject->setHeight($this->height); $this->makeCharObject(); $d = dir(”./storage/”); while (false !== ($entry = $d->read())) { if ((eregi(”.char$”,$entry))){ if ($this->checkCharObject(implode(”,file(”./storage/”.$entry)))){ return unserialize(implode(”,file(”./storage/”.$entry))); } } } $d->close(); return false; } /** * Get inverce corner * * @param int $type * @return int */ static function getNegative($type){ if ($type>4){ return $type - 4; }else{ return $type + 4; } } /** * Clean up OCR object * */ function clear(){ unset($this->image); unset($this->charObject); unset($this->name); $this->charObject = new char(); } /** * Comare charObjects to congruous * * @param string $str * @param boolean $print * @return boolean */ function checkCharObject($str, $print = true){ $m = 1; $char = unserialize($str); if ($print){ echo “Check \”".$char->getName().”\”
“; } $currentImagesInfo = $char->getImageInfo(); $imageInfo = $this->charObject->getImageInfo(); if (count($imageInfo)){ $m = 0; foreach ($imageInfo as $key => $value){ if ((isset($currentImagesInfo[$key]['type'])) && (($currentImagesInfo[$key]['type']!=$value['type'])&&($currentImagesInfo[$key]['type']!=OCR::getNegative($value['type'])))){ $m++; } } } if ($print){ echo “We have “.$m.” errors
“; } if ($m==0){ if ($print){ echo “It is \”".$char->getName().”\”
“; } return true; }else{ if ($print){ echo “It is not \”".$char->getName().”\”
“; } return false; } } /** * Save chars object to file in storage * */ function saveResult(){ $time = time(); $this->charObject->setTime($time); $fl = fopen(”./storage/”.$this->name.md5($this->name).”.char”,”w”); fwrite($fl,serialize($this->charObject)); fclose($fl); } /** * Make image for learning * */ function createImage(){ $im = imagecreatetruecolor(100, 100); $white = imagecolorallocate($im, 255, 255, 255); $black = imagecolorallocate($im, 0, 0, 0); imagechar($im, 2, 1, 1, $this->name, $white); $this->getSize($im); $this->image = imagecreatetruecolor($this->width, $this->height); imagecopy($this->image, $im, 0, 0, 0, 0, $this->width, $this->height); } /** * get image size * * @param image $im */ function getSize($im){ $w = 0; $h = 0; for ($x=0; $x<100; $x++){ for ($y=0; $ygetBWcolor($im, $x, $y)==COLOR_WHITE)&&($wgetBWcolor($im, $x, $y)==COLOR_WHITE)&&($h0)&&($h>0)){ $w+=2; $h+=2; $this->width = $w; $this->height = $h; } } /** * Get type of corners * * @param image $im * @param int $x * @param int $y * @return int */ function getCornerType($im, $x, $y){ $p11 = $this->getBWcolor($im, $x, $y); $p21 = $this->getBWcolor($im, $x+1, $y); $p12 = $this->getBWcolor($im, $x, $y+1); $p22 = $this->getBWcolor($im, $x+1, $y+1); $sum = $p11 + $p12 + $p21 + $p22; if ($sum % 2 == 0){ return false; } if (($p11==COLOR_BLACK)&&($p21==COLOR_WHITE)&&($p12==COLOR_WHITE)&&($p22==COLOR_WHITE)){ return CORNER_BP_LEFT_TOP; } if (($p11==COLOR_WHITE)&&($p21==COLOR_BLACK)&&($p12==COLOR_WHITE)&&($p22==COLOR_WHITE)){ return CORNER_BP_RIGHT_TOP; } if (($p11==COLOR_WHITE)&&($p21==COLOR_WHITE)&&($p12==COLOR_BLACK)&&($p22==COLOR_WHITE)){ return CORNER_BP_LEFT_BOTTOM; } if (($p11==COLOR_WHITE)&&($p21==COLOR_WHITE)&&($p12==COLOR_WHITE)&&($p22==COLOR_BLACK)){ return CORNER_BP_RIGHT_BOTTOM; } if (($p11==COLOR_WHITE)&&($p21==COLOR_BLACK)&&($p12==COLOR_BLACK)&&($p22==COLOR_BLACK)){ return CORNER_WP_LEFT_TOP; } if (($p11==COLOR_BLACK)&&($p21==COLOR_WHITE)&&($p12==COLOR_BLACK)&&($p22==COLOR_BLACK)){ return CORNER_WP_RIGHT_TOP; } if (($p11==COLOR_BLACK)&&($p21==COLOR_BLACK)&&($p12==COLOR_WHITE)&&($p22==COLOR_BLACK)){ return CORNER_WP_LEFT_BOTTOM; } if (($p11==COLOR_BLACK)&&($p21==COLOR_BLACK)&&($p12==COLOR_BLACK)&&($p22==COLOR_WHITE)){ return CORNER_WP_RIGHT_BOTTOM; } } /** * Get pixels color, understund only black or white pixels * * @param image $im * @param int $x * @param int $y * @return int */ function getBWcolor($im, $x, $y){ $rgb = @ImageColorAt($im,$x,$y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >>
Fatal error: Class ‘OCR’ not found in C:\wamp\www\phpocr-2006-02-10\example.php on line 14