index.d.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. declare namespace Tesseract {
  2. function createScheduler(): Scheduler
  3. function createWorker(langs?: string | string[] | Lang[], oem?: OEM, options?: Partial<WorkerOptions>, config?: string | Partial<InitOptions>): Promise<Worker>
  4. function setLogging(logging: boolean): void
  5. function recognize(image: ImageLike, langs?: string, options?: Partial<WorkerOptions>): Promise<RecognizeResult>
  6. function detect(image: ImageLike, options?: Partial<WorkerOptions>): any
  7. interface Scheduler {
  8. addWorker(worker: Worker): string
  9. addJob(action: 'recognize', ...args: Parameters<Worker['recognize']>): Promise<RecognizeResult>
  10. addJob(action: 'detect', ...args: Parameters<Worker['detect']>): Promise<DetectResult>
  11. terminate(): Promise<any>
  12. getQueueLen(): number
  13. getNumWorkers(): number
  14. }
  15. interface Worker {
  16. load(jobId?: string): Promise<ConfigResult>
  17. writeText(path: string, text: string, jobId?: string): Promise<ConfigResult>
  18. readText(path: string, jobId?: string): Promise<ConfigResult>
  19. removeText(path: string, jobId?: string): Promise<ConfigResult>
  20. FS(method: string, args: any[], jobId?: string): Promise<ConfigResult>
  21. reinitialize(langs?: string | Lang[], oem?: OEM, config?: string | Partial<InitOptions>, jobId?: string): Promise<ConfigResult>
  22. setParameters(params: Partial<WorkerParams>, jobId?: string): Promise<ConfigResult>
  23. getImage(type: imageType): string
  24. recognize(image: ImageLike, options?: Partial<RecognizeOptions>, output?: Partial<OutputFormats>, jobId?: string): Promise<RecognizeResult>
  25. detect(image: ImageLike, jobId?: string): Promise<DetectResult>
  26. terminate(jobId?: string): Promise<ConfigResult>
  27. getPDF(title?: string, textonly?: boolean, jobId?: string):Promise<GetPDFResult>
  28. }
  29. interface Lang {
  30. code: string;
  31. data: unknown;
  32. }
  33. interface InitOptions {
  34. load_system_dawg: string
  35. load_freq_dawg: string
  36. load_unambig_dawg: string
  37. load_punc_dawg: string
  38. load_number_dawg: string
  39. load_bigram_dawg: string
  40. }
  41. type LoggerMessage = {
  42. jobId: string
  43. progress: number
  44. status: string
  45. userJobId: string
  46. workerId: string
  47. }
  48. interface WorkerOptions {
  49. corePath: string
  50. langPath: string
  51. cachePath: string
  52. dataPath: string
  53. workerPath: string
  54. cacheMethod: string
  55. workerBlobURL: boolean
  56. gzip: boolean
  57. legacyLang: boolean
  58. legacyCore: boolean
  59. logger: (arg: LoggerMessage) => void,
  60. errorHandler: (arg: any) => void
  61. }
  62. interface WorkerParams {
  63. tessedit_pageseg_mode: PSM
  64. tessedit_char_whitelist: string
  65. tessedit_char_blacklist: string
  66. preserve_interword_spaces: string
  67. user_defined_dpi: string
  68. tessjs_create_hocr: string
  69. tessjs_create_tsv: string
  70. tessjs_create_box: string
  71. tessjs_create_unlv: string
  72. tessjs_create_osd: string
  73. [propName: string]: any
  74. }
  75. interface OutputFormats {
  76. text: boolean;
  77. blocks: boolean;
  78. layoutBlocks: boolean;
  79. hocr: boolean;
  80. tsv: boolean;
  81. box: boolean;
  82. unlv: boolean;
  83. osd: boolean;
  84. pdf: boolean;
  85. imageColor: boolean;
  86. imageGrey: boolean;
  87. imageBinary: boolean;
  88. debug: boolean;
  89. }
  90. interface RecognizeOptions {
  91. rectangle: Rectangle
  92. pdfTitle: string
  93. pdfTextOnly: boolean
  94. rotateAuto: boolean
  95. rotateRadians: number
  96. }
  97. interface ConfigResult {
  98. jobId: string
  99. data: any
  100. }
  101. interface RecognizeResult {
  102. jobId: string
  103. data: Page
  104. }
  105. interface GetPDFResult {
  106. jobId: string
  107. data: number[]
  108. }
  109. interface DetectResult {
  110. jobId: string
  111. data: DetectData
  112. }
  113. interface DetectData {
  114. tesseract_script_id: number | null
  115. script: string | null
  116. script_confidence: number | null
  117. orientation_degrees: number | null
  118. orientation_confidence: number | null
  119. }
  120. interface Rectangle {
  121. left: number
  122. top: number
  123. width: number
  124. height: number
  125. }
  126. enum OEM {
  127. TESSERACT_ONLY,
  128. LSTM_ONLY,
  129. TESSERACT_LSTM_COMBINED,
  130. DEFAULT,
  131. }
  132. enum PSM {
  133. OSD_ONLY = '0',
  134. AUTO_OSD = '1',
  135. AUTO_ONLY = '2',
  136. AUTO = '3',
  137. SINGLE_COLUMN = '4',
  138. SINGLE_BLOCK_VERT_TEXT = '5',
  139. SINGLE_BLOCK = '6',
  140. SINGLE_LINE = '7',
  141. SINGLE_WORD = '8',
  142. CIRCLE_WORD = '9',
  143. SINGLE_CHAR = '10',
  144. SPARSE_TEXT = '11',
  145. SPARSE_TEXT_OSD = '12',
  146. RAW_LINE = '13'
  147. }
  148. const enum imageType {
  149. COLOR = 0,
  150. GREY = 1,
  151. BINARY = 2
  152. }
  153. type ImageLike = string | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement
  154. | CanvasRenderingContext2D | File | Blob | ImageData | Buffer | OffscreenCanvas;
  155. interface Block {
  156. paragraphs: Paragraph[];
  157. text: string;
  158. confidence: number;
  159. baseline: Baseline;
  160. bbox: Bbox;
  161. blocktype: string;
  162. polygon: any;
  163. page: Page;
  164. lines: Line[];
  165. words: Word[];
  166. symbols: Symbol[];
  167. }
  168. interface Baseline {
  169. x0: number;
  170. y0: number;
  171. x1: number;
  172. y1: number;
  173. has_baseline: boolean;
  174. }
  175. interface RowAttributes {
  176. ascenders: number;
  177. descenders: number;
  178. row_height: number;
  179. }
  180. interface Bbox {
  181. x0: number;
  182. y0: number;
  183. x1: number;
  184. y1: number;
  185. }
  186. interface Line {
  187. words: Word[];
  188. text: string;
  189. confidence: number;
  190. baseline: Baseline;
  191. rowAttributes: RowAttributes
  192. bbox: Bbox;
  193. paragraph: Paragraph;
  194. block: Block;
  195. page: Page;
  196. symbols: Symbol[];
  197. }
  198. interface Paragraph {
  199. lines: Line[];
  200. text: string;
  201. confidence: number;
  202. baseline: Baseline;
  203. bbox: Bbox;
  204. is_ltr: boolean;
  205. block: Block;
  206. page: Page;
  207. words: Word[];
  208. symbols: Symbol[];
  209. }
  210. interface Symbol {
  211. choices: Choice[];
  212. image: any;
  213. text: string;
  214. confidence: number;
  215. baseline: Baseline;
  216. bbox: Bbox;
  217. is_superscript: boolean;
  218. is_subscript: boolean;
  219. is_dropcap: boolean;
  220. word: Word;
  221. line: Line;
  222. paragraph: Paragraph;
  223. block: Block;
  224. page: Page;
  225. }
  226. interface Choice {
  227. text: string;
  228. confidence: number;
  229. }
  230. interface Word {
  231. symbols: Symbol[];
  232. choices: Choice[];
  233. text: string;
  234. confidence: number;
  235. baseline: Baseline;
  236. bbox: Bbox;
  237. is_numeric: boolean;
  238. in_dictionary: boolean;
  239. direction: string;
  240. language: string;
  241. is_bold: boolean;
  242. is_italic: boolean;
  243. is_underlined: boolean;
  244. is_monospace: boolean;
  245. is_serif: boolean;
  246. is_smallcaps: boolean;
  247. font_size: number;
  248. font_id: number;
  249. font_name: string;
  250. line: Line;
  251. paragraph: Paragraph;
  252. block: Block;
  253. page: Page;
  254. }
  255. interface Page {
  256. blocks: Block[] | null;
  257. confidence: number;
  258. lines: Line[];
  259. oem: string;
  260. osd: string;
  261. paragraphs: Paragraph[];
  262. psm: string;
  263. symbols: Symbol[];
  264. text: string;
  265. version: string;
  266. words: Word[];
  267. hocr: string | null;
  268. tsv: string | null;
  269. box: string | null;
  270. unlv: string | null;
  271. sd: string | null;
  272. imageColor: string | null;
  273. imageGrey: string | null;
  274. imageBinary: string | null;
  275. rotateRadians: number | null;
  276. pdf: number[] | null;
  277. debug: string | null;
  278. }
  279. }
  280. export = Tesseract;
  281. export as namespace Tesseract;