<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>exploration &#187; tesseract</title>
	<atom:link href="http://robertcarlsen.net/tag/tesseract/feed" rel="self" type="application/rss+xml" />
	<link>http://robertcarlsen.net</link>
	<description>accounts of success and misadventure</description>
	<lastBuildDate>Tue, 13 Jul 2010 14:37:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>OCR on iPhone demo</title>
		<link>http://robertcarlsen.net/2009/12/06/ocr-on-iphone-demo-1043</link>
		<comments>http://robertcarlsen.net/2009/12/06/ocr-on-iphone-demo-1043#comments</comments>
		<pubDate>Sun, 06 Dec 2009 20:33:39 +0000</pubDate>
		<dc:creator>Robert</dc:creator>
				<category><![CDATA[iphone]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[app]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[ocr]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tesseract]]></category>

		<guid isPermaLink="false">http://robertcarlsen.net/?p=1043</guid>
		<description><![CDATA[Update: Source code for demo project released.

i finally got around to building a proof of concept implementation of tesseract-ocr for the iPhone. months ago, i documented the steps which helped to get the library cross-compiled for the iPhone&#8217;s ARM processor, and how to build a fat library for use with the simulator as well. several [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update</strong>: Source code for demo project <a href="http://robertcarlsen.net/2010/01/12/ocr-for-iphone-source-1080">released</a>.</p>
<p><img class="size-full wp-image-1051 alignright" title="TessIcon" src="http://robertcarlsen.net/blog/wp-content/uploads/2009/12/TessIcon.png" alt="TessIcon" width="57" height="57" /></p>
<p>i finally got around to building a proof of concept implementation of <a href="http://code.google.com/p/tesseract-ocr/" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/tesseract-ocr/?referer=');">tesseract-ocr</a> for the iPhone. months ago, i <a href="http://robertcarlsen.net/2009/07/15/cross-compiling-for-iphone-dev-884">documented</a> the steps which helped to get the library cross-compiled for the iPhone&#8217;s ARM processor, and how to build a fat library for use with the simulator as well. several folks have helped immensely in noting how to actually run the engine in obj-c++. thanks to everyone who has commented so far.</p>
<p>anyway, below is a short video of the POC in action. the basic workflow is: select image from photo library or camera, crop tightly on the box of text you&#8217;d like to convert, wait while it processes, select / copy or email text.<span id="more-1043"></span></p>
<p>there are loads of improvements which could be implemented (image histogram adjustment, rotation / perspective correction, automatic text box/layout detection, content detection &#8211; dates, links, contact information&#8230;) but this is a nice point to stop and document.</p>
<p>i realize that there are several OCR applications available for the iPhone, including a few which also run the engine on the device rather than handing it off to a web service. this started as an educational project on cross-compiling, and to fill a personal want for a handheld OCR app of my own. for these reasons, i&#8217;m going to open-source the entire app. look for it after this semester ends when i&#8217;ll have some more time to properly document the code. in the meantime, enjoy these code snippets demonstrating how to initialize the engine and process an image.</p>
<p>Initialize the engine:</p>
<pre class="brush: cpp;">
    NSString *dataPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@&quot;tessdata&quot;];
    /*
     Set up the data in the docs dir
     want to copy the data to the documents folder if it doesn't already exist
     */
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // If the expected store doesn't exist, copy the default store.
    if (![fileManager fileExistsAtPath:dataPath]) {
        // get the path to the app bundle (with the tessdata dir)
        NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
        NSString *tessdataPath = [bundlePath stringByAppendingPathComponent:@&quot;tessdata&quot;];
        if (tessdataPath) {
            [fileManager copyItemAtPath:tessdataPath toPath:dataPath error:NULL];
        }
    }

    NSString *dataPathWithSlash = [[self applicationDocumentsDirectory] stringByAppendingString:@&quot;/&quot;];
    setenv(&quot;TESSDATA_PREFIX&quot;, [dataPathWithSlash UTF8String], 1);

    // init the tesseract engine.
    tess = new TessBaseAPI();

    tess-&gt;SimpleInit([dataPath cStringUsingEncoding:NSUTF8StringEncoding],  // Path to tessdata-no ending /.
                     &quot;eng&quot;,  // ISO 639-3 string or NULL.
                     false);
</pre>
<p>Process an image. This should be threaded as it&#8217;s a heavy process:</p>
<pre class="brush: cpp;">
    CGSize imageSize = [uiImage size];
    double bytes_per_line	= CGImageGetBytesPerRow([uiImage CGImage]);
    double bytes_per_pixel	= CGImageGetBitsPerPixel([uiImage CGImage]) / 8.0;

    CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider([uiImage CGImage]));
    const UInt8 *imageData = CFDataGetBytePtr(data);

    // this could take a while. maybe needs to happen asynchronously.
    char* text = tess-&gt;TesseractRect(imageData,
                                     bytes_per_pixel,
                                     bytes_per_line,
                                     0, 0,
                                     imageSize.width, imageSize.height);

    // Do something useful with the text!
    NSLog(@&quot;Converted text: %@&quot;,[NSString stringWithCString:text encoding:NSUTF8StringEncoding]);

    delete[] text;
</pre>
<p>Enjoy the video!<br />
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/MICew5-nZp4&#038;fs=1" /><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><embed src="http://www.youtube.com/v/MICew5-nZp4&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://robertcarlsen.net/2009/12/06/ocr-on-iphone-demo-1043/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
