-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gocr.php
229 lines (205 loc) · 5.76 KB
/
Gocr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**
* A wrapper to work with Gocr inside PHP scripts.
*
* @author Stéphane Monnot <smonnot@solire.fr>
* @license MIT http://mit-license.org/
*/
namespace Shinbuntu\Gocr;
/**
* A wrapper to work with Gocr inside PHP scripts.
*
* @author Stéphane Monnot <smonnot@solire.fr>
* @license MIT http://mit-license.org/
*/
class Gocr
{
/**
* Path to Gocr binary
* @var string
*/
protected $gocrBinPath = '/usr/bin/gocr';
/**
* Path of the image to be recognized
* @var string
*/
protected $imagePath;
protected $spaceWidthParam;
protected $modeParam;
protected $valueForCertaintyOfRecognitionParam;
protected $databasePathParam;
/**
* Construct
*
* @param string $imagePath Path to the image to be recognized
*
* @throws \Exception
*/
public function __construct($imagePath)
{
if ($imagePath == '') {
throw new \InvalidArgumentException('Path to the image can\'t be empty.');
}
if (!file_exists($imagePath)) {
throw new \Exception(sprintf('The file "%s" does not exist.', $imagePath));
}
$this->imagePath = $imagePath;
}
/**
* Get spacewidth between words in units of dots
*
* @return float|int|null
*/
public function getSpaceWidthParam()
{
return $this->spaceWidthParam;
}
/**
* Set spacewidth between words in units of dots (default: 0 for autodetect), wider widths are interpreted as word
* spaces, smaller as character spaces
*
* @param float|int|null $spaceWidthParam Spacewidth to set
* @return void
*/
public function setSpaceWidthParam($spaceWidthParam)
{
$this->spaceWidthParam = $spaceWidthParam;
}
/**
* Get Operational mode
*
* @return int|null
*/
public function getModeParam()
{
return $this->modeParam;
}
/**
* Set Operational mode; mode is a bitfield (default: 0)
*
* @param int|null $modeParam Operational mode to set
* @return void
*/
public function setModeParam($modeParam)
{
if (!is_null($modeParam) && !is_int($modeParam)) {
$exception = new \InvalidArgumentException(
sprintf('The parameter "$modeParam" must be an integer or null.')
);
throw $exception;
}
$this->modeParam = $modeParam;
}
/**
* Get value for certainty of recognition
*
* @return int|null
*/
public function getValueForCertaintyOfRecognitionParam()
{
return $this->valueForCertaintyOfRecognitionParam;
}
/**
* Set value for certainty of recognition (0..100; default: 95), characters with a higher certainty are accepted,
* characters with a lower certainty are treated as unknown (not recognized); set higher values, if you want to have
* only more certain recognized characters
*
* @param int|null $valueForCertaintyOfRecognitionParam Value for certainty of recognition to set.
* If null, the default value (95) will be used
* @return void
*/
public function setValueForCertaintyOfRecognitionParam($valueForCertaintyOfRecognitionParam)
{
if (!is_null($valueForCertaintyOfRecognitionParam) && !is_int($valueForCertaintyOfRecognitionParam)) {
$exception = new \InvalidArgumentException(
sprintf('The parameter "$valueForCertaintyOfRecognitionParam" must be an integer or null.')
);
throw $exception;
}
$this->valueForCertaintyOfRecognitionParam = $valueForCertaintyOfRecognitionParam;
}
/**
* Get database path
*
* @return string|null
*/
public function getDatabasePathParam()
{
return $this->databasePathParam;
}
/**
* Set database path, a final slash must be included, default is ./db/, this path will be populated with images of
* learned characters
*
* @param string|null $databasePathParam Database path
* @return void
*/
public function setDatabasePathParam($databasePathParam)
{
$this->databasePathParam = $databasePathParam;
}
/**
* Run recognize
*
* @return string|null Text extract from image
*/
public function recognize()
{
return $this->execute();
}
/**
* Execute Gocr command and return output
*
* @return string
*/
protected function execute()
{
exec($this->getCommand(), $output);
return implode("\n", $output);
}
/**
* Get Gocr command line with params
*
* @return string
*/
protected function getCommand()
{
$command = $this->gocrBinPath
. ' '
. $this->getParams()
. ' '
. $this->imagePath
;
return $command;
}
/**
* Get Gocr command line params string
*
* @return string
*/
protected function getParams()
{
$stringParams = '';
if ($this->valueForCertaintyOfRecognitionParam) {
$stringParams .= '-a '
. escapeshellarg($this->valueForCertaintyOfRecognitionParam)
. ' ';
}
if ($this->databasePathParam) {
$stringParams .= '-p '
. escapeshellarg($this->databasePathParam)
. ' ';
}
if ($this->modeParam) {
$stringParams .= '-m '
. escapeshellarg($this->modeParam)
. ' ';
}
if ($this->spaceWidthParam) {
$stringParams .= '-s '
. escapeshellarg($this->spaceWidthParam)
. ' ';
}
return $stringParams;
}
}