Bonjour,

Alors que je suis en train de travailler sur un projet perso de petite GED, j'ai eu besoin d'un filtre pour l'indexation de SQL Server, afin de reconnaître et indexer le texte contenu dans des images.

Voici donc le source de mon IFilter.

Il est en version initiale, et est certainement perfectible.

Il utilise Tesseract (Google projet d'un OCR open source). Je dois notamment améliorer l'utilisation de cet outil, afin de mieux l'intégrer à mon projet.

N'hésitez pas à me contacter si vous avez des questions/suggestions, ou si vous désirez m'aider à l'améliorer !

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
 
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
 
/*
 * To use the tagging of data types, the Chunk type (PropID) is the
 * propid from the schema.txt file; e.g. 
 * "D5CDD505-2E9C-101B-9397-08002B2CF9AE/PerceivedType" for
 * "Perceived Type".
 * Then GetValue() returns one string from the list of:
 * contact communications communications/e-mail communications/calendar communications/task
 * communications/im document/note document document/text document/spreadsheet document/presentation
 * music images images/picture images/video folder favorite program
 * 
 * Note also that the acceptable attributes (FULLPROPSPEC property sets)
 * are passed into aAttributes via Init(), if cAttributes is non-zero.
*/
 
namespace IFilterPicture
{
    /// <summary>
    /// The IFilter and IPersistFile interfaces, and some support structure
    /// for the File Filter (Extractor)
    /// </summary>
    [ComVisible(true), Guid("78BD7653-D16A-43ad-B119-7080D635F27E")]
    public class IFilterPicture : IPersistFile, IFilter
    {
        private static string myGuid = "78BD7653-D16A-43ad-B119-7080D635F27E"; /// <summary>For convenience, GUID in a string</summary>
        //private static string myHandlerGUID = "{60995D18-5F02-4196-B618-5D9C2A6ABC9D}"; /// <summary>We don't use this here, but it's needed for the registry.</summary>
 
        private bool bApplyIndexAttributes = false;
        private bool bCrawlAttributes = false;
        private bool bApplyOther = false;
 
        private bool doneFirstChunk = false;
 
        private string currentFileName = string.Empty;
        private int iChunkCount = 0;
 
        private const uint LANGUAGE_FR = 0x040C;
 
        private string text;
 
        public IFilterPicture()
        {
#if DEBUG
            _writeLog("On construit IFilterPicture()");
#endif
        }
 
        //	IFilter Methods
        /// <summary>Initializes a filtering session.</summary>
        /// <param name="grfFlags">What processing to perform</param>
        /// <param name="cAttributes">How many Attribute elements are in the aAttributes array, may be 0</param>
        /// <param name="aAttributes">Attributes to process</param>
        /// <param name="pdwFlags"></param>
        public void Init([MarshalAs(UnmanagedType.U4)] IFILTER_INIT grfFlags, uint cAttributes, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] FULLPROPSPEC[] aAttributes, ref uint pdwFlags)
        {
#if DEBUG
            _writeLog("On entre dans Init()");
#endif
            bApplyIndexAttributes = ((grfFlags & IFILTER_INIT.APPLY_INDEX_ATTRIBUTES) == IFILTER_INIT.APPLY_INDEX_ATTRIBUTES);
            bCrawlAttributes = ((grfFlags & IFILTER_INIT.APPLY_CRAWL_ATTRIBUTES) == IFILTER_INIT.APPLY_CRAWL_ATTRIBUTES);
            bApplyOther = ((grfFlags & IFILTER_INIT.APPLY_OTHER_ATTRIBUTES) == IFILTER_INIT.APPLY_OTHER_ATTRIBUTES);
#if DEBUG
            _writeLog("On sort de Init()");
#endif
        }
 
        /// <summary>
        /// Loads the first chunk or sets the pointers to zero on fail
        /// </summary>
        /// <returns></returns>
        private bool getFirstChunk()
        {
#if DEBUG
            _writeLog("On entre dans getFirstChunk()");
#endif
 
            if ((currentFileName == null) || (File.Exists(currentFileName) == false))
            {
                currentFileName = "";
#if DEBUG
                _writeLog("On n'a pas de fichier Ã* indexer (?)");
                _writeLog("On sort de getFirstChunk()");
#endif
                return false;
            }
 
#if DEBUG
            _writeLog(string.Format("On index le fichier {0}", currentFileName));
#endif
            try
            {
                TesseractOCR tess = new TesseractOCR(Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(IFilterPicture)).Location) + @"\tesseract.exe");
                text = tess.OCRFromFile(currentFileName);
            }
            catch (Exception e)
            {
#if DEBUG
                _writeLog(string.Format("Erreur : {0}", e.Message));
#endif
                text = "crashed";
            }
 
#if DEBUG
            _writeLog("On sort de getFirstChunk()");
#endif
 
            return true;
        }
 
        /// <summary>The IFilter::GetChunk method positions the filter at the beginning of the next chunk, or at the first chunk if this is the first call to the GetChunk method, and returns a description of the current chunk.</summary>
        /// <remarks>Each chunk might be tiny; the file extractor (filter) decides.
        /// Note that Chunk ID 0 is invalid; must start at 1.</remarks>
        /// <param name="pStat">STAT_CHUNK pointer</param>
        [PreserveSig]
        public int GetChunk(out STAT_CHUNK pStat)
        {
#if DEBUG
            _writeLog("On entre dans GetChunk()");
#endif
 
            bool bSuccess = true;
            STAT_CHUNK myStat = new STAT_CHUNK();
            pStat = myStat;
            pStat.locale = LANGUAGE_FR;
            iChunkCount++;
            pStat.idChunk = (uint)iChunkCount;
 
            if (iChunkCount == 2)
            {
#if DEBUG
                _writeLog("On sort de GetChunk()");
#endif
                return Constants.FILTER_E_END_OF_CHUNKS;
            }
 
            if (!doneFirstChunk)
            {
#if DEBUG
                _writeLog("!doneFirstChunk");
#endif
                doneFirstChunk = true;
                bSuccess = getFirstChunk();
            }
 
            if (!bSuccess)
            {
#if DEBUG
                _writeLog("!bSuccess");
                _writeLog("On sort de GetChunk() avec le message Constants.FILTER_E_END_OF_CHUNKS");
#endif
                return Constants.FILTER_E_END_OF_CHUNKS;
            }
            pStat.flags = CHUNKSTATE.CHUNK_TEXT;
 
            pStat.attribute.guidPropSet = new Guid(0xb725f130, 0x47ef, 0x101a, 0xa5, 0xf1, 0x02, 0x60, 0x8c, 0x9e, 0xeb, 0xac);
            pStat.attribute.psProperty.ulKind = (int)PSKIND.PROPID;
            pStat.attribute.psProperty.propid = Constants.PID_STG_CONTENTS;
            pStat.idChunkSource = pStat.idChunk;
            pStat.breakType = CHUNK_BREAKTYPE.CHUNK_EOC;
            pStat.cwcStartSource = 0;
#if DEBUG
            _writeLog("On sort de GetChunk() avec le message Constants.FILTER_E_END_OF_CHUNKS");
#endif
            return Constants.S_OK;
        }
 
        /// <summary>Gets the text from this chunk of type CHUNK_TEXT</summary>
        /// <param name="pcwcBuffer">Size of input buffer in UNICODE characters; fill with output size in CHARACTERS</param>
        /// <param name="buffer">Null-terminated UNICODE string of the text.</param>
        /// <returns></returns>
        /// <remarks>This is called repeatedly INSIDE THE ONE CHUNK until all the text 
        /// is delivered.</remarks>
        [PreserveSig]
        public int GetText(ref int pcwcBuffer, IntPtr buffer)
        {
#if DEBUG
            _writeLog("On entre dans GetText()");
#endif
            pcwcBuffer = text.Length;
            Marshal.Copy(text.ToCharArray(), 0, buffer, text.Length);
#if DEBUG
            _writeLog("On sort de GetText()");
#endif            
            return Constants.FILTER_S_LAST_TEXT;
        }
 
        /// <summary>Retrieves a value (internal value-type property) from a chunk, which must have a CHUNKSTATE enumeration value of CHUNK_VALUE.</summary>
        /// <param name="ppPropValue"></param>
        /// <remarks>According to the blog, the most important ones are:
        /// DocAuthor, PrimaryDate, PercievedType and DocTitle.
        /// PerceivedTypes include contact, communications/calendar
        ///		document/note and communications/task</remarks>
        ///		
        [PreserveSig]
        public int GetValue(UIntPtr ppPropValue)
        {
#if DEBUG
            _writeLog("On entre (et sort) de GetValue()"); 
#endif
            return Constants.FILTER_E_NO_VALUES;
        }
 
        /// <summary>Returns the class identifier (CLSID) for the component object. In the case 
        /// of a filter, this is the file type identifier.</summary>
        /// <param name="pClassID"></param>
        public void GetClassID(out Guid pClassID)
        {
#if DEBUG
            _writeLog("On entre (et sort) de GetClassID()");
#endif
            pClassID = new Guid(myGuid);
        }
        public void BindRegion([MarshalAs(UnmanagedType.Struct)]FILTERREGION origPos, ref Guid riid, ref UIntPtr ppunk)
        {
#if DEBUG
            _writeLog("On entre (et sort) de BindRegion()");
#endif
            return;
        }
 
 
        /// <summary>Open and initialize the file</summary>
        /// <param name="pszFileName">Absolute file path to open/initialize</param>
        /// <param name="dwMode">STGM flags for how to open</param>
        /// <remarks>The STGM Flags provide 18 options and additional combinations for how to open
        /// and process the file.  Since IFilters are inherently read-only to the IPersist:: file, 
        /// most of these are irrelevant.  So we're ignoring it.</remarks>
        public void Load(string pszFileName, int dwMode)
        {
#if DEBUG
            _writeLog("On entre dans Load()");
            _writeLog(string.Format("On indexe {0}", pszFileName));
#endif
            currentFileName = pszFileName;
#if DEBUG
            _writeLog("On sort de Load()");
#endif
        }
 
#if DEBUG
        static public void _writeLog(string text)
        {
            string logFileName = "c:\\in\\logfile.txt";
            StreamWriter mylog = new StreamWriter(logFileName, true);
            if (mylog != null)
                mylog.WriteLine(DateTime.Now.ToLongTimeString() + " " + text);
            mylog.Close();
 
        }
#endif
 
        //	These are stubs needed for the interface but never called for Read-Only Access
        public void GetCurFile(out string ppszFileName) { ppszFileName = currentFileName; }
        public int IsDirty() { return Constants.E_NOTIMPL; }
        public void Save(string pszFileName, bool fRemember) { }
        public void SaveCompleted(string pszFileName) { }
    }
 
 
 
    [Flags]
    public enum IFILTER_INIT
    {
        NONE = 0,
        CANON_PARAGRAPHS = 1,
        HARD_LINE_BREAKS = 2,
        CANON_HYPHENS = 4,
        CANON_SPACES = 8,
        APPLY_INDEX_ATTRIBUTES = 16,
        APPLY_CRAWL_ATTRIBUTES = 256,
        APPLY_OTHER_ATTRIBUTES = 32,
        INDEXING_ONLY = 64,
        SEARCH_LINKS = 128,
        FILTER_OWNED_VALUE_OK = 512
    }
 
    [Flags]
    public enum IFILTER_FLAGS
    {
        OLE_PROPERTIES = 1
    }
 
    /// <summary>Describes what separates the CURRENT chunk from the PREVIOUS chunk with the same Attribute.
    /// Used by IFilter::GetChunk() in the return.</summary>
    public enum CHUNK_BREAKTYPE
    {
        CHUNK_NO_BREAK = 0,	/// <summary>No Break from previous and doesn't get flags/attributes; glue them </summary>
        CHUNK_EOW = 1,		/// <summary>Reached the End-Of-Word.  Don't use it; language dependent.</summary>
        CHUNK_EOS = 2,		/// <summary>Sentence Break</summary>
        CHUNK_EOP = 3,		///	<summary>Paragraph Break</summary>
        CHUNK_EOC = 4		///	<summary>Chapter Break</summary>
    }
 
    [Flags]
    public enum CHUNKSTATE
    {
        CHUNK_TEXT = 0x1,
        CHUNK_VALUE = 0x2,
        CHUNK_FILTER_OWNED_VALUE = 0x4
    }
 
    public enum PSKIND
    {
        LPWSTR = 0,
        PROPID = 1
    }
 
    // [StructLayout(LayoutKind.Sequential)]
    [StructLayout(LayoutKind.Explicit)]
    public struct PROPSPEC
    {
        [FieldOffset(0)]
        public uint ulKind;
        [FieldOffset(4)]
        public uint propid;
 
        //	NOTE: The real structure uses a Union.  C# doesn't support unions.  So I've commented
        //		  out this element to make the correct size.  Other samples don't do this.  TMcN
        [FieldOffset(4)]
        public IntPtr lpwstr;
    }
 
    [StructLayout(LayoutKind.Sequential)]
    public struct FULLPROPSPEC
    {
        public Guid guidPropSet;
        public PROPSPEC psProperty;
    }
 
    [StructLayout(LayoutKind.Sequential)]
    public struct STAT_CHUNK
    {
        public uint idChunk;
        [MarshalAs(UnmanagedType.U4)]
        public CHUNK_BREAKTYPE breakType;
        [MarshalAs(UnmanagedType.U4)]
        public CHUNKSTATE flags;
        public uint locale;
        [MarshalAs(UnmanagedType.Struct)]
        public FULLPROPSPEC attribute;
        public uint idChunkSource;
        public uint cwcStartSource;
        public uint cwcLenSource;
    }
 
    [StructLayout(LayoutKind.Sequential)]
    public struct FILTERREGION
    {
        public uint idChunk;
        public uint cwcStart;
        public uint cwcExtent;
    }
 
 
    [StructLayout(LayoutKind.Sequential)]
    public struct PROPVARIANT
    {
        public short vt;			/// <summary>Variant Type</summary>
        public short reserved1;
        public short reserved2;
        public short reserved3;
        unsafe
        public char* pwszVal;
    }
 
 
    [ComImport]
    [Guid("89BCB740-6119-101A-BCB7-00DD010655AF")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IFilter
    {
        void Init([MarshalAs(UnmanagedType.U4)] IFILTER_INIT grfFlags, uint cAttributes,
            [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] FULLPROPSPEC[] aAttributes,
            ref uint pdwFlags);
        [PreserveSig]
        int GetChunk(out STAT_CHUNK pStat);
 
 
        [PreserveSig]
        int GetText(ref int pcwcBuffer, IntPtr buffer);
        [PreserveSig]
        int GetValue(UIntPtr ppPropValue);
 
        void BindRegion([MarshalAs(UnmanagedType.Struct)]FILTERREGION origPos, ref Guid riid, ref UIntPtr ppunk);
    }
 
    [ComImport]
    [Guid("f07f3920-7b8c-11cf-9be8-00aa004b9986")]
    public class CFilter
    {
    }
 
    public class Constants
    {
        public const uint PID_STG_DIRECTORY = 0x00000002;
        public const uint PID_STG_CLASSID = 0x00000003;
        public const uint PID_STG_STORAGETYPE = 0x00000004;
        public const uint PID_STG_VOLUME_ID = 0x00000005;
        public const uint PID_STG_PARENT_WORKID = 0x00000006;
        public const uint PID_STG_SECONDARYSTORE = 0x00000007;
        public const uint PID_STG_FILEINDEX = 0x00000008;
        public const uint PID_STG_LASTCHANGEUSN = 0x00000009;
        public const uint PID_STG_NAME = 0x0000000a;
        public const uint PID_STG_PATH = 0x0000000b;
        public const uint PID_STG_SIZE = 0x0000000c;
        public const uint PID_STG_ATTRIBUTES = 0x0000000d;
        public const uint PID_STG_WRITETIME = 0x0000000e;
        public const uint PID_STG_CREATETIME = 0x0000000f;
        public const uint PID_STG_ACCESSTIME = 0x00000010;
        public const uint PID_STG_CHANGETIME = 0x00000011;
        public const uint PID_STG_CONTENTS = 0x00000013;
        public const uint PID_STG_SHORTNAME = 0x00000014;
        public const int FILTER_E_END_OF_CHUNKS = (unchecked((int)0x80041700));
        public const int FILTER_E_NO_MORE_TEXT = (unchecked((int)0x80041701));
        public const int FILTER_E_NO_MORE_VALUES = (unchecked((int)0x80041702));
        public const int FILTER_E_NO_TEXT = (unchecked((int)0x80041705));
        public const int FILTER_E_NO_VALUES = (unchecked((int)0x80041706));
        public const int FILTER_S_LAST_TEXT = (unchecked((int)0x00041709));
        public const int S_OK = (unchecked((int)0x00000000L));
        public const int S_FALSE = (unchecked((int)0x00000001L));
        public const int E_NOTIMPL = (unchecked((int)0x80004001L));
        public const int E_ABORT = (unchecked((int)0x80004004L));
 
    }
 
    public class TesseractOCR
    {
        private string commandpath;
        private string outpath;
 
        public TesseractOCR(string commandpath)
        {
#if DEBUG
            _writeLog("TesseractOCR()");
#endif
            this.commandpath = commandpath;
            outpath = Path.ChangeExtension(Path.GetTempFileName(), ".txt");
 
#if DEBUG
            _writeLog(string.Format("commandpath : {0}", commandpath));
            _writeLog(string.Format("outpath : {0}", outpath));
#endif
        }
        private string analyze(string filename)
        {
#if DEBUG
            _writeLog("analyze()");
#endif
            string args = string.Format("\"{0}\" \"{1}\" -l fra", filename, outpath.Replace(".txt", string.Empty));
#if DEBUG
            _writeLog(string.Format("args : {0}", args));
#endif
            ProcessStartInfo startinfo = new ProcessStartInfo(commandpath, args);
            startinfo.CreateNoWindow = true;
            startinfo.UseShellExecute = false;
#if DEBUG
            _writeLog(string.Format("commandpath : {0}", commandpath));
            if (!File.Exists(filename))
            {
                _writeLog(string.Format("Le fichier {0} n'existe pas !", filename));
            }
            _writeLog("Lancement de la reconnaissance OCR");
#endif
 
            Process.Start(startinfo).WaitForExit();
 
            string ret = "";
#if DEBUG
            if (!File.Exists(outpath))
            {
                _writeLog(string.Format("Le fichier {0} n'existe pas !", outpath));
            }
#endif            
            using (StreamReader r = new StreamReader(outpath))
            {
#if DEBUG
                _writeLog("lecture du fichier de sortie");
#endif
                string content = r.ReadToEnd();
                ret = content;
            }
#if DEBUG
            _writeLog(string.Format("ret : {0}", ret));
#endif
            File.Delete(outpath);
            return ret;
        }
 
        public string OCRFromFile(string filename)
        {
#if DEBUG
            _writeLog("OCRFromFile()");
#endif
            return analyze(filename);
        }
 
#if DEBUG
        static public void _writeLog(string text)
        {
            string logFileName = "c:\\in\\logfile2.txt";
            StreamWriter mylog = new StreamWriter(logFileName, true);
            if (mylog != null)
                mylog.WriteLine(DateTime.Now.ToLongTimeString() + " " + text);
            mylog.Close();
 
        }
#endif
    }
}