title: Automatic face detection using Picasa API to extract individual images
body: <p><em>(A similar question has been asked on superuser for answers related to applications. The question is posted here to gather programmable solutions for the same)</em></p>
  
  <p>At my work place, passport sized photographs are scanned together, then cut up into individual pictures and saved with unique file numbers. Currently we use Paint.net to manually select, cut and save the pictures.</p>
  
  <p><strong>Sample Scanned Document Picasa Screenshot:</strong>
  (from: google image search multiple sources, fairuse)</p>
  
  <p><img src="https://i.stack.imgur.com/exNFw.jpg" alt="picasa screenshot"></p>
  
  <p>For eg. In Picasa 3.8, On clicking View > People, all the faces are shown and I am asked to name them, can I save these individual pictures automatically with the names as different pictures?</p>
  
  <p><strong>Updated</strong></p>
  
  <p><strong><em>All I want to do is convert the picture above to individual pictures.</em></strong></p>
  
  <p>In the image above, I have shown how Picasa 3.8 detects the images and prompts me to name them. I do not need face recognition, I simply need face detection. Picasa detects the individual images and shows them on the RHS. These individual images are what I need. Picasa creates a .ini file which saves the hex values which contains the co-ordinates of the individual faces.</p>
  
  <p><em>These individual faces are what I am interested in If I can have the co-ordinates, I can crop the required images from the picture.</em></p>
  
  <p><strong>SAMPLE.jpg</strong></p>
  
  <p><img src="https://i.stack.imgur.com/MjdKK.jpg" alt="sample.jpg"></p>
  
  <p><strong>ini contents</strong></p>
  
  <pre><code> [SAMPLE.jpg]
  faces=rect64(c18f4c8ef407851e),d4ff0a020be5c3c0;rect64(534a06d429ae627),dff6163dfd9d4e41;rect64(b9c100fae46b3046),e1059dcf6672a2b3;rect64(7b5105daac3a3cf4),4fc7332c107ffafc;rect64(42a036a27062a6c),ef86c3326c143248;rect64(31f4efe3bd68fd8),90158b3d3b65dc9b;rect64(327904e0614d390d),43cbda6e92fcb63e;rect64(4215507584ae9b8c),15b6a967e857f334;rect64(895d4efeb8b68425),5c4ff70ac70b27d3
  backuphash=3660
  </code></pre>
  
  <p>*The ini file seems to be saving the co-ordinates of the face tags as <code>rect64(534a06d429ae627),dff6163dfd9d4e41</code> for each tag. 
  Quoting from <a href="http://www.google.com/support/forum/p/Picasa/thread?tid=36ae553a7b49088e&amp;hl=en" rel="nofollow noreferrer">Picasa Help Site</a> user <a href="http://www.google.com/support/forum/p/Picasa/user?userid=02815233294807518823&amp;hl=en" rel="nofollow noreferrer">Technonath</a> says</p>
  
  <blockquote>
    <p>@oedious wrote:- This is going to be
    somewhat technical, so hang on.
    * The number encased in rect64() is a 64-bit hexadecimal number.
    * Break that up into four 16-bit numbers.
    * Divide each by the maximum unsigned 16-bit number (65535) and you'll have
    four numbers between 0 and 1.
    * The four numbers remaining give you relative coordinates for the face
    rectangle: (left, top, right, bottom).
    * If you want to end up with absolute coordinates, multiple the left and
    right by the image width and the top
    and bottom by the image height.</p>
  </blockquote>
  
  <p>The above quote talks about the number encased in rect64() what about the number outside the parentheses after the comma?</p>
  
  <p>I have asked a related question. Answers of which may help you too.
  <a href="https://stackoverflow.com/questions/3892408/get-four-16bit-numbers-from-a-64bit-hex-value">Get four 16bit numbers from a 64bit hex value</a></p>
  
  <blockquote>
    <p>Note: The 
    ini details are the same which picasa
    generated for the particular image.</p>
    
    <p>Plus the question has been updated multiple times and may not be clear enough.</p>
  </blockquote>
  
  <p>There are some responses at the <a href="http://www.google.com/support/forum/p/Picasa/thread?tid=36ae553a7b49088e&amp;hl=en" rel="nofollow noreferrer">Picasa Help site</a>, where I asked the same question
  One of the answers from that thread to get co-ordinates based on the hex values from the ini file. The following code is in C# from <a href="http://www.google.com/support/forum/p/Picasa/user?userid=06712422887226750608&amp;hl=en" rel="nofollow noreferrer">esac</a> from the help site. Can I do the same in PHP?</p>
  
  <pre><code>public static RectangleF GetRectangle(string hashstr)
  {
      UInt64 hash = UInt64.Parse(hashstr, System.Globalization.NumberStyles.HexNumber);
      byte[] bytes = BitConverter.GetBytes(hash);
  
      UInt16 l16 = BitConverter.ToUInt16(bytes, 6);
      UInt16 t16 = BitConverter.ToUInt16(bytes, 4);
      UInt16 r16 = BitConverter.ToUInt16(bytes, 2);
      UInt16 b16 = BitConverter.ToUInt16(bytes, 0);
  
      float left = l16 / 65535.0F;
      float top = t16 / 65535.0F;
      float right = r16 / 65535.0F;
      float bottom = b16 / 65535.0F;
  
      return new RectangleF(left, top, right - left, bottom - top);
  } 
  </code></pre>
  
  <p><strong>PHP code</strong> <em>trying to</em> convert 64bit to numbers between 1 and 0</p>
  
  <pre><code>&lt;?php
  $dim = getimagesize("img.jpg");    
  $hex64=array();
  $b0="c18f4c8ef407851e";
  $hex64[]=substr($b0,0,4);
  $hex64[]=substr($b0,4,4);
  $hex64[]=substr($b0,8,4);
  $hex64[]=substr($b0,12,4);
  $width=$dim[0];
  $height=$dim[1];
  foreach($hex64 as $hex16){
  $dec=hexdec($hex16);
  $divide=65536;
  $mod=$dec%$divide;
  $result=$dec/$divide;
  $cordinate1=$result*$width;
  $cordinate2=$result*$height;
  echo "Remainder 1 : ".$mod." ; Result 1 :  ".$result."&lt;br/&gt;CO-ORDINATES : &lt;B&gt;".$cordinate1." ".$cordinate2."&lt;/B&gt;&lt;br/&gt;";
  }
  ?&gt;
  </code></pre>
  
  <p><strong>The output</strong></p>
  
  <blockquote>
    <p>Remainder 1 : 49551 ; Result 1 :
    0.75608825683594 CO-ORDINATES : 371.99542236328 396.94633483887 Remainder 1 : 19598 ; Result 1 :
    0.29904174804688 CO-ORDINATES : 147.12854003906 156.99691772461 Remainder 1 : 62471 ; Result 1 :
    0.95323181152344 CO-ORDINATES : 468.99005126953 500.4467010498 Remainder 1 : 34078 ; Result 1 :
    0.51998901367188 CO-ORDINATES : 255.83459472656 272.99423217773 </p>
  </blockquote>
  
  <p>So I have the co-ordinates too and @Nirmal has <a href="https://stackoverflow.com/questions/3872112/automatic-face-detection-using-picasa-api-to-extract-individual-images/3932181#3932181">shown how to crop them</a>. Now next steps would be to parse picasa.ini for the hex codes and file names and integrate the code. Picasa doesn't currently provide the hex codes via a api(or <a href="http://code.google.com/apis/picasa/" rel="nofollow noreferrer">Do they?</a>). If that were the case, things would have been better.</p>
  
  <p>So we are nearing a solution. Thank you all, I wish I could award the bounty to everyone(I cannot, but fear not and look out for a spike in your rep!)</p>