Improved location read + tesseract

This commit is contained in:
xdrm-brackets 2017-09-14 16:57:42 +02:00
parent 89bf9722c4
commit 960f490beb
2 changed files with 50 additions and 26 deletions

View File

@ -266,7 +266,7 @@
/* (2) Manage copy error */
if( !$copied )
throw new \Exception("Cannot clip image");
return [ null, null ];
/* (3) Save to jpeg */
\imagesavealpha($clip, true);
@ -292,7 +292,7 @@
/* (2) Manage error */
}catch(\Exception $e){
$read = [ 'unkown', 'unknown' ];
$read = [ null, null ];
}
@ -430,9 +430,10 @@
$RAW .= "DTSTART:${start_t}\n";
$RAW .= "DTEND:${data[0]}\n";
$RAW .= "UID:$start_t-univ-pau-ics\n"; // required
if( !is_null($data[1]) )
$RAW .= "SUMMARY:${data[1]}\n";
if( !is_null($data[2]) )
$RAW .= "LOCATION:${data[2]}\n";
// $RAW .= "ATTACH;ENCODING=BASE64;VALUE=BINARY;FILENAME=att.jpg:${data[1]}\n";
$RAW .= "CATEGORIES: UPPA Calendar\n";
$RAW .= "END:VEVENT\n";
}

View File

@ -48,37 +48,60 @@
public function read(){
/* [1] Record the text from the image
=========================================================*/
=========================================================*/ {
/* (1) Process tesseract */
$read = shell_exec("tesseract ".$this->fname." stdout -l fra --user-words ".__ROOT__."/config/edt.user-words -c language_model_penalty_non_freq_dict_word=0.1 -c language_model_penalty_non_dict_word=.15 -c tessedit_char_whitelist=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 2>/dev/null");
// var_dump($read);
/* (2) If empty */
if( is_null($read) || !preg_match('@\n@g', $read) )
if( is_null($read) || !preg_match('@\n@m', $read) )
throw new \Exception("Nothing read");
/* (3) Split by lines */
$by_line = explode("\n", $read);
$lines = [];
/* (4) Get first line (title) */
$title = $by_line[0];
/* (4) Remove empty lines */
for( $i = 0 ; $i < count($by_line) ; $i++ ){
/* (5) Get last non-empty line */
for( $i = count($by_line)-1 ; $i > 0 ; $i-- ){
if( !empty( trim($by_line[$i]) ))
$lines[] = $by_line[$i];
// {1} Check not empty //
if( empty($by_line[$i]) )
continue;
}
// {2} Matches //
if( preg_match('@^amphi@i', $by_line[$i]) || // 'amphi A', 'amphi 600 droit'
preg_match('@^S\d+@i', $by_line[$i]) // 'S10', 'S22'
)
return [ $title, $by_line[$i] ];
/* (5) Manage if empty */
if( count($lines) < 2 )
throw new \Exception("Nothing read");
}
return [ $title, 'unknown' ];
/* [2] Extract data
=========================================================*/ {
/* (1) Get first non-empty line (title) */
$title = $lines[0];
/* (2) Get last non-empty line */
for( $i = count($lines)-1 ; $i > 0 ; $i-- ){
// Amphi ... //
if( preg_match('@^a[nm][bp][hl]i ?(.+)$@i', $lines[$i], $m) ) // 'amphi A', 'amphi 600 droit'
return [ $title, "Amphi ${m[1]}" ];
// S... OR 5... //
if( preg_match('@^[S|5] ?(\d+)@i', $lines[$i], $m) ) // 'S10', 'S22'
return [ $title, "S. ${m[1]}" ];
}
}
return [ $title, null ];
}