The principle and implementation _php example of PHP image recognition technology

Source: Internet
Author: User
Tags php image recognition

In fact, the image recognition technology and we usually do the password verification and so no difference, are in advance to check the data into the warehouse, and then use the input (identification) data and the data in the library, but the image recognition technology has a part of fault tolerance, and our usual password verification is to 100% match.

A few days ago, have friends talk about doing game Click Draw, identify the text in the picture, then immediately think of the JS control or flash to do mask layer, feel this approach is the most convenient and quick effect, and save server resources, but the request is actually through PHP to identify the text in the image.

Got that two days of news: 1, MA Yun Face recognition payment, 2, 12306 use the new verification code, said what now the domestic ticket software can not be used, published less than a day to be cracked. Then it was quite a coincidence that morning saw a Java image recognition technology article. So I was thinking about looking at the image recognition technology in PHP.

In fact, the so-called image recognition, is not a new technology, at least I found the information is very early. But I have not been involved in this area of work, have not seen.

First of all, the need for this experiment: there is a picture, inside three locations have three digits, require the corresponding position of the value of the number. (Eagle-eyed students may see the following code is I take the others, yes, it is true I directly copy others and cut, after all, I am also a taste of these, will eventually post the original author's initial code)

Class Gjphone {protected $imgPath;//Picture path protected $imgSize;//Picture size protected $hecData;//separated after array protected $ Hordata; The data protected $verData in transverse arrangement;
  Vertically collated data function __construct ($path) {$this->imgpath = $path;
    The Public Function Gethec () {$size = getimagesize ($this->imgpath);
    $res = Imagecreatefrompng ($this->imgpath); for ($i = 0; $i < $size [1]; + + $i) {for ($j = 0; $j < $size [0]; + + $j) {$rgb = Imagecolorat ($res, $j
        , $i);
        $rgbarray = Imagecolorsforindex ($res, $RGB);
             if ($rgbarray [' Red '] < | | $rgbarray [' green '] < 125 | |
        $rgbarray [' Blue '] <) {$data [$i] [$j] = 1;
        else {$data [$i] [$j] = 0;
    }}} $this->imgsize = $size;
  $this->hecdata = $data;
    The Public Function Maghordata () {$data = $this->hecdata;
    $size = $this->imgsize;
    $z = 0;
      for ($i = 0; $i < $size [1]; + + $i) {if (In_array (' 1 ', $data [$i])) {$z + +;
          for ($j = 0; $j < $size [0]; + + $j) {if ($data [$i] [$j] = = ' 1 ') {$newdata [$z] [$j] = 1;
          else {$newdata [$z] [$j] = 0;
  return $this->hordata = $newdata;
    Public Function Showphone ($ndatas) {error_reporting (0);
    $phone = null;
    $d = 0; foreach ($ndatas as $key => $val) {if (In_array (1, $val)) {foreach ($val as $k => $v) {$n
        darr[$d]. = $v;
      } if (! In_array (1, $val)) {$d + +;
    ' foreach ($ndArr as $key => $val} {$phone. = $this->initdata ($val 01);
  return $phone;
    }/** * Initial data */Public Function InitData ($numStr) {$result = null; $data = Array (' 1 ' => ' 0000000011100000000000000111000000000100100010000000001010001100000000001100011000000000 0110000100000000010110011000000 ', ' 5 ' => ' 00000000001000000000000000010000000000100100100000000000101001110000000000100000110000000011000000100000001101000010000 ' , ' => ' 000000111000111000000000110011001001001000100100010001100001001000100011000010010001000110000100100
    01001001001100010100 ');
      foreach ($data as $key => $val) {similar_text ($numStr, $val, $pre);
        if ($pre > 95) {//similarity of more than 95% $result = $key;
      Break
  } return $result;
}} $imgurl = ' jd.png ';
List ($width, $heght, $type, $attr) = getimagesize ($imgurl);
$new _w = 17;
$new _h = 11; $thisimage = Imagecreatetruecolor ($new _w, $new _h);
$new _w, $new _h for cropped pictures with wide height $background = imagecolorallocate ($thisimage, 255, 255, 255);
Imagefilledrectangle ($thisimage, 0, 0, $new _w, $new _h, $background); $oldimg = Imagecreatefrompng ($imgurl);
    Load the original picture//first position to take the location of the map (this can be done through the front-end JS or other means of positioning, because I this is testing, so the PS positioning and write dead) $weizhi = Array (' 1 ' => 165,

' 5 ' => 308, ' => 456 '; foreach ($weizhi as $wwzz) {$src _y = 108; Imagecopy ($thisimage, $oldimg, 0, 0, $wwzz, $src _y, $new _w, $new _h); $src _y, $new _w Copy part of the image to the upper-left corner of the cropped area of the original image src_im the coordinates from the src_x,src_y, the width is src_w, and the height is part of the src_h copy to the dst_im image coordinates dst_x and dst_
  The position of Y.
  $tem _png = ' tem_1.png '; Imagepng ($thisimage, __dir__. '/' . $tem _png);
  
  By locating the location you want to identify from the original image and generating a new cache graph, use it for the later images recognition class. $gjPhone = new Gjphone ($tem _png); Instantiate class $gjPhone->gethec (); Image pixel Separation $horData = $gjPhone->maghordata (); will be separated from the data into 01 representations of the image, here can be determined according to their preferences $phone = $gjPhone->showphone ($horData); To match the data represented by the converted 01 to the data in the library, a match of 95 or more is successful, and the library here writes the array echo directly because of the test. ' . $phone. ' |
';
 }

So it seems, in fact, 12306 verification code is cracked is also a sentient being, there is no need to be so vocal. As long as the constant grasp of the code picture and turn into their own program readable data into the library, and then verify the time to match it. Then Ali's face recognition payment principle is understood, but they may be very fine.

Front-end time to see Aliyun a verification code form, just start feeling may be better, now it seems, as long as the heart, in fact, can be cracked ah.

Okay, here's the code for the original.

/** * Phone number identification. * @author by ZSC for 2010.03.24 */class Gjphone {protected $imgPath;//Picture path protected $imgSize;//Picture size protect Ed $hecData; The separated array protected $horData; The data protected $verData in transverse arrangement;
  Vertically collated data function __construct ($path) {$this->imgpath = $path;
  /** * Color Separation conversion ... * * @param unknown_type $path * @return Unknown/Public Function gethec ()
    {$size = getimagesize ($this->imgpath);
    $res = Imagecreatefrompng ($this->imgpath); for ($i = 0; $i < $size [1]; + + $i) {for ($j = 0; $j < $size [0]; + + $j) {$rgb = Imagecolorat ($res, $j
        , $i);
        $rgbarray = Imagecolorsforindex ($res, $RGB);
             if ($rgbarray [' Red '] < | | $rgbarray [' green '] < 125 | |
        $rgbarray [' Blue '] <) {$data [$i] [$j] = 1;
        else {$data [$i] [$j] = 0;
    }}} $this->imgsize = $size;
  $this->hecdata = $data; Number of/** * Color separationAccording to the transverse finishing ... * * @return unknown/Public Function maghordata () {$data = $this->hecdata;
    $size = $this->imgsize;
    $z = 0;
        for ($i = 0; $i < $size [1]; + + $i) {if (In_array (' 1 ', $data [$i])) {$z + +;
          for ($j = 0; $j < $size [0]; + + $j) {if ($data [$i] [$j] = = ' 1 ') {$newdata [$z] [$j] = 1;
          else {$newdata [$z] [$j] = 0;
  return $this->hordata = $newdata; /** * Collating longitudinal data ... * * @return unknown/Public function Magverdata ($newdata) {for ($i = 0; $i &l T 132;
      + + $i) {for ($j = 1; $j < + + $j) {$ndata [$i] [$j] = $newdata [$j] [$i];
    }} $sum = count ($ndata);
    $c = 0;
      for ($a = 0; $a < $sum; $a + +) {$value = $ndata [$a];
        if (In_array (1, $value)) {$ndatas [$c] = $value;
      $c + +;
        } elseif (Is_array ($ndatas)) {$b = $c-1; if (In_arrAy (1, $ndatas [$b])) {$ndatas [$c] = $value;
        $c + +;
  }} return $this->verdata = $ndatas;
    /** * Show phone number ... * * @return unknown/Public function Showphone ($ndatas) {$phone = null;
    $d = 0; foreach ($ndatas as $key => $val) {if (In_array (1, $val)) {foreach ($val as $k => $v) {$n
        darr[$d]. = $v;
      } if (! In_array (1, $val)) {$d + +;
    ' foreach ($ndArr as $key => $val} {$phone. = $this->initdata ($val 01);
  return $phone; /** * Separate Display ... * * @param unknown_type $DATAARR/function Drawwh ($DATAARR) {if (Is_array ($DATAARR))
            {foreach ($dataArr as $key => $val) {foreach ($val as $k => $v) {if ($v = = 0) { $c. = "<font color= ' #FFFFFF ' >". $v.
          "</font>";
          else {$c. = $v;
       }
        } $c. = "<br/>";
  } echo $c; /** * Initial Data ... * * @param unknown_type $NUMSTR * @return Unknown/Public function InitData ($
    NUMSTR) {$result = null; $data = Array (0 => ' 000011111000001111111110011000000011110000000001110000000001110000000001110000000001011000  000011011100000111000111111100000001110000 ', 1 => ' 011000000000011000000000111111111111111111111111 ', 2 => '
        001000000011011000000111110000001101110000011001110000011001110000110001111001100001011111100001000110000001 ', 3 => '
        001000000010011000000011110000000001110000000001110000110001110000110001011001110011011111011111000110001100 ', 4 => ' 0000000011000000001111000000011111000000111011000001110011000011000011000110000011001111111111111111111111110000000011000 00000000100 ', 5 => ' 111111000001111111000001110001000001110001000001110001100001110001100001110000110011110000 111111000000001100 ', 6 => ' 000011111000001111111110011000110011110001100001110001100001110001100001110001100001010001110011010000111111000000001100 '
        , 7 => ' 110000000000110000000111110000111111110001110000110111000000111100000000111000000000111000000000 ', 8 => '
        000100011110011111111111110011100001110001100001110001100001110001100001110011100001011111111111000100011110 ', 9 => ' 
    001111000000011111100001110000110001110000110001110000110001110000110001011000100001011111100111000111111110000001110000 '
    );
      foreach ($data as $key => $val) {similar_text ($numStr, $val, $pre);
        if ($pre > 95) {//similarity of more than 95% $result = $key;
      Break
  } return $result;
}} $imgPath = "Http://bj.ganji.com/tel/5463013757650d6c5e31093e563c51315b6c5c6c5237.png";
$gjPhone = new Gjphone ($imgPath);
The color separation $gjPhone->gethec ();
Draw the horizontal data $horData = $gjPhone->maghordata ();
echo "=============== transverse data ==============<br/><br/><br/>"; $gjPhone->DRAWWH ($horData);
Draw longitudinal data $verData = $gjPhone->magverdata ($horData);
echo "<br/><br/><br/>=============== longitudinal data ==============< br/><br/><br/>";

$gjPhone-&GT;DRAWWH ($verData);
Output telephone $phone = $gjPhone->showphone ($verData); echo "<br/><br/><br/>=============== phone ==============<br/><br/><br/>".

 $phone;

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.