Created
          May 24, 2010 09:15 
        
      - 
      
- 
        Save yuroyoro/411678 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import java.io._ | |
| import java.awt._ | |
| import java.awt.geom._ | |
| import java.awt.event._ | |
| import java.awt.image.BufferedImage | |
| import javax.swing.{ImageIcon, JWindow, JLabel, JFrame} | |
| import javax.imageio.ImageIO | |
| import java.net.{URL,HttpURLConnection,URLEncoder} | |
| import scala.io.Source | |
| import scala.xml.{XML,NodeSeq } | |
| // detectface();で顔認識 | |
| object DetectFace extends Frame { | |
| val FileExt = """.*\.(png|jpg)$""".r | |
| def main( args:Array[String] ) = { | |
| args.headOption foreach { file => file match { | |
| case FileExt( ext ) => new ViewWindow( new File( file )) | |
| case _ => println("unsupported file format." ) | |
| }} | |
| } | |
| trait ClickEventHandler extends Component{ | |
| val handle: ()=> Unit | |
| addMouseListener(new MouseAdapter(){ override def mouseClicked(e:MouseEvent) = { handle() } }) | |
| } | |
| trait ImageWindow extends JFrame{ | |
| val w:Int | |
| val h:Int | |
| val t:String | |
| val pane = getContentPane | |
| def open = { | |
| setSize( w, h ) | |
| setTitle( t ) | |
| setVisible(true) | |
| } | |
| def createIcon( file:File )( f:(BufferedImage) => Image) = { | |
| val icon = new ImageIcon | |
| val image = ImageIO.read( file ) | |
| icon.setImage( f(image) ) | |
| (icon , image ) | |
| } | |
| } | |
| class ViewWindow( file:File ) extends ImageWindow | |
| with ClickEventHandler{ | |
| def post = { | |
| val u = (new URL( "http://detectface.com/api/detect")).openConnection.asInstanceOf[HttpURLConnection] | |
| u.setRequestMethod( "POST" ) | |
| u.setDoOutput( true ) | |
| u.setRequestProperty( "Content-Type", "image/" + (ext match{ | |
| case "jpg" => "jpeg" | |
| case _ => ext | |
| })) | |
| u.setRequestProperty( "Content-Length" , file.length.toString ) | |
| val os = u.getOutputStream() | |
| val in = new FileInputStream( file ) | |
| val buf = new Array[Byte](1024) | |
| for(i <- Stream.continually(in.read(buf)).takeWhile(_ != -1)){ | |
| os.write( buf , 0, i) | |
| } | |
| os.flush() | |
| os.close() | |
| val src = Source.fromInputStream( u.getInputStream ) | |
| val s = src.mkString | |
| println( s ) | |
| XML.loadString( s ) | |
| } | |
| val FileExt( ext ) = file.getName | |
| val (icon, image ) = createIcon( file ){ image => | |
| def attr( xml:NodeSeq , s:String ) = (xml \ ("@" + s) text ).toInt | |
| val xml = post | |
| val g = image.createGraphics | |
| g.setColor( Color.RED ); | |
| xml \ "face" foreach{ face => | |
| face \ "bounds" map{ b => | |
| ( attr( b, "width"), attr(b, "height"), attr(b, "x"), attr(b, "y") ) | |
| } foreach{ case (w, h, x, y) => | |
| g.drawRect( x, y, w, h ) | |
| } | |
| val Point = """([A-Z]+)\d""".r | |
| g.setColor( Color.BLUE); | |
| face \ "features" \ "point" map { p => | |
| ( p \ "@id" text , attr(p, "x" ), attr(p, "y")) | |
| } groupBy { case ( id, x, y ) => | |
| Point.findFirstMatchIn( id ) map{ _.group(1) } getOrElse( id ) | |
| } foreach { case ( id , points ) => | |
| val pt = points.sorted | |
| val xs = pt.map{ case ( id, x, y ) => x }.toArray | |
| val ys = pt.map{ case ( id, x, y ) => y }.toArray | |
| g.draw( new Polygon( xs, ys, xs.length ) ) | |
| } | |
| } | |
| image | |
| } | |
| val (t, w, h )= ( file.getName, image.getWidth, image.getHeight ) | |
| val handle = dispose _ | |
| pane.add(new JLabel(icon)) | |
| open | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment