Home » php » PHP图像识别

PHP图像识别

编 辑:Y ┊ 时 间:2018年12月12日 ┊ 访问: 22 次

对于一些几乎没有变型的图片来说,下面这个方法或者会减轻你的工作量。
事实上,我也是从http://fr.cc0311.com/php-telephone-number-ocr.html看的代码,再想想,walkerlee在05年就写过了关于bmp图片的识别。不过BMP可能会相对简单一点吧?
看了这个法月博客的代码。脑子一热,花了半小时,重写了一遍。。。没有优化。因为大部分方法还是参考的他的,只是对法月的这篇文章中的代码,我做了简单的改动。版权,还是算他的吧。。。去年的时候,也写过类似的。。只是因为图片变形,最终识别率超低,因此对于这种不变形的,还是写下代码做个笔记吧。


    <?php  
      
    $imgfile = 'http://bj.ganji.com/tel/5463013757650d6c5e31093e563c51315b6c5c6c5237.png';  
      
    interface imagedatas {  
        public function setimagedata();  
        public function getimagedata();  
    }  
      
    class GanjiImage implements imagedatas{  
        public $imagedata;  
        public function __construct(){  
            $this->setimagedata();  
        }  
        public function setimagedata(){  
            $this->imagedata = array(  
                0=>'000011111000001111111110011000000011110000000001110000000001110000000001110000000001011000000011011100000111000111111100000001110000',  
                1=>'011000000000011000000000111111111111111111111111',  
                2=>'001000000011011000000111110000001101110000011001110000011001110000110001111001100001011111100001000110000001',  
                3=>'001000000010011000000011110000000001110000000001110000110001110000110001011001110011011111011111000110001100',  
                4=>'000000001100000000111100000001111100000011101100000111001100001100001100011000001100111111111111111111111111000000001100000000000100',  
                5=>'111111000001111111000001110001000001110001000001110001100001110001100001110000110011110000111111000000001100',  
                6=>'000011111000001111111110011000110011110001100001110001100001110001100001110001100001010001110011010000111111000000001100',  
                7=>'110000000000110000000111110000111111110001110000110111000000111100000000111000000000111000000000',  
                8=>'000100011110011111111111110011100001110001100001110001100001110001100001110011100001011111111111000100011110',  
                9=>'001111000000011111100001110000110001110000110001110000110001110000110001011000100001011111100111000111111110000001110000',  
            );  
        }  
        public function getimagedata(){  
            return $this->imagedata;  
        }  
    }  
      
    class imageValidation  
    {  
        protected $imgfile;  
        protected $imgsize;  
      
        protected $imgdata; //数组  
        protected $hordata; //横向  
        protected $verdata; //纵向  
      
        protected $imgfunc;  
      
        function __construct( $imgfile , $imgsource = ''){  
            $this->imgfile = $imgfile;  
            $this->imgsize = getimagesize($imgfile);  
            $this->imgfunc = $this->getImageFunc();  
            if($this->imgfunc == 'imagecreatefromstring'){  
                $this->imgfile = file_get_contents($this->imgfile);  
            }  
            $this->imgsource = new $imgsource();  
        }  
      
        function getImageData(){  
            $func = $this->imgfunc;  
            $resource = $func( $this->imgfile );  
            for( $i=0 ; $i < $this->imgsize[1] ; $i++){  
                for( $j=0 ; $j<$this->imgsize[0] ; $j++){  
                    $rgbcolor = imagecolorat( $resource , $j , $i);  
                    $rgbarray = imagecolorsforindex( $resource , $rgbcolor );  
                    if($rgbarray['red'] < 125 || $rgbarray['green']<125 || $rgbarray['blue'] < 125){  
                        $data[$i][$j]=1;  
                    }else{  
                        $data[$i][$j]=0;  
                    }  
                }  
            }  
            $this->imgdata = $data;  
        }  
          
        function getHorData(){  
            $z = 0;  
            for($i=0; $i<$this->imgsize[1]; $i++){  
                if(in_array('1',$this->imgdata[$i])){  
                    for($j=0; $j<$this->imgsize[0]; $j++){  
                        if($this->imgdata[$i][$j] == '1'){  
                            $newdata[$z][$j] = 1;  
                        }else{  
                            $newdata[$z][$j] = 0;  
                        }  
                    }  
                    $z++;  
                }  
            }  
            $this->hordata = $newdata;  
            return $newdata;  
        }  
      
        function getVerData(){  
            //$data = array_reverse($this->hordata); //这是180度翻转,不是90度  
            for( $i=0; $i< count($this->hordata[0]) ; ++$i){  
                for( $j=0;$j<count($this->hordata);$j++){  
                    $newdata[$i][$j] = $this->hordata[$j][$i];  
                }  
            }  
            $i = 0;  
            foreach($newdata as $k=> $v){  
                if( in_array(1 , $v ) || (isset($newdata[$k+1]) && in_array(1,$newdata[$k+1]) )){  
                    $newdatas[$i] = $v;  
                    $i++;  
                }  
            }  
            $this->verdata = $newdatas;  
            return $newdatas;  
        }  
        function get(){  
            $i = 0;  
            foreach( $this->verdata as $val){  
                if(in_array(1,$val)){  
                    $datas[$i] .= join("",$val);  
                }else{  
                    $i++;  
                }  
            }  
            foreach( $datas as $k => $val ){  
                $number[$k] = $this->check($val);  
            }  
            return $number;  
        }  
        function check($str){  
            $imgsourcesdata = $this->imgsource->getimagedata();  
            foreach( $imgsourcesdata as $k => $val){  
                similar_text($str,$val,$percent);  
                $ret[$k]=$percent;  
            }  
            return array_search(max($ret),$ret);  
        }  
      
        function draw( $data ){  
            $str = '';  
            if(is_array($data)){  
                foreach ($data as $key => $val){  
                    foreach ($val as $k => $v){  
                        if($v == 0){  
                            $str .= "<font color='#FFFFFF'>".$v."</font>";  
                        }else{  
                            $str .= $v;  
                        }  
                    }  
                    $str.= "<br/>";  
                }  
            }  
            echo $str;  
        }  
      
        function getImageFunc(){  
            switch($this->imgsize[2]){  
                case IMAGETYPE_PNG :  
                    $this->imgfunc = 'imagecreatefrompng';  
                break;  
                case IMAGETYPE_JPEG :  
                case IMAGETYPE_JPG :  
                    $this->imgfunc = 'imagecreatefromjpeg';  
                break;  
                case IMAGETYPE_GIF :  
                    $this->imgfunc = 'imagecreatefromgif';  
                break;  
                default:  
                    $this->imgfunc = 'imagecreatefromstring';  
                break;  
            }  
            return $this->imgfunc;  
        }  
      
    }  
      
    $img = new imageValidation($imgfile,'GanjiImage');  
    $img->getImageData();  
    $img->getHorData();  
    $img->getVerData();  
    $phone = $img->get();  
    dump($phone);  
      
      
      
    function dump($data){  
        print("<pre>");  
        print_r($data);  
        print("</pre>");  
    }  

对于该图片,识别率很高.
http://www.neatstudio.com/show-1405-1.shtml




Copyright © 2026 Y 版权所有.网站运行:13年238天21小时25分