#!/bin/bash
################################################################################
# Projet    : PMWS
# Programme : captcha.sh
# Création  : 04.01.2012
# Révision  : 05.01.2012
# Objet     :
################################################################################
# ocrad - Optical Character Recognition program
# ocropus - document analysis and OCR system
# tesseract-ocr - Command line OCR tool
# gocr - A command line OCR
# cuneiform - multi-language OCR system

  total=0
  correct=0

  for captcha_fn in ../client/actiond/captcha/*-*.jpg ; do
    captcha=`echo $captcha_fn|/bin/sed "s/^.*-\([0-9A-Z]\{5,7\}\)\.jpg$/\1/g"`

    if [ $captcha != $captcha_fn ] ; then

      let "total += 1"

      gocr=`/usr/bin/djpeg -pnm $captcha_fn | /usr/bin/gocr -C A-Z0-9 - | sed "s/ //g"`

      echo $gocr|grep --silent "^[0-9A-Z]\{5,7\}$"

      if [ $? -eq 0 ] ; then
        if [ $gocr != $captcha ] ; then
          result=1
        else
          result=0
        fi
      else
        result=1
      fi

      if [ $result -eq 1 ] ; then
        let "correct += 1"
      fi

      echo ================================================================================
      echo "$total:$captcha:$gocr:$result"
    fi
  done

  rate=`echo $correct/$total*100|bc -l|sed "s/^\(.*\.[0-9][0-9]\).*$/\1/g"`

  echo ================================================================================
  echo "$correct corrects sur $total ($rate %)"

# vim:encoding=utf-8
