Press "Enter" to skip to content

Category: OCR Project

Using AWS Rekognition to Detect Text in Images with PHP

A couple years ago, I tinkered with a solution to use a webcam to capture images of receipts, covert the images to raw text, and store in a database. My scrappy solution worked okay but it lacked the accuracy to make it viable for anything real-world. With AWS Rekognition launching since then, I figured I’d try it out and see how it compares. I used a fake receipt to see how it’d do. Like every other AWS product I’ve used, it was incredibly easy to work it. I’ll share the simple script I used at the bottom of this post but, needless to say, there’s not much to it. While use was a breeze, the results were disappointing. Primarily, the fact that Rekognition is limited to ONLY 50 words in an image. So clearly it’s not a full-on OCR tool. Somewhat more disappointing was the limited range of confidence scores Rekognition returned (for each text detection, it provides a confidence score). The overall output was pretty accurate but not accurate enough for me to consider it “wow” worthy. Despite this, all of the confidence scores were above 93%. To be considered an OCR service, AWS Rekognition has a long way…

Webcam Captures Text and Stores in MySQL Database

While visiting my family recently, I saw my dad entering numbers from each of the 5-8 ticket receipts he receives daily to keep track of the work he’s done, report for payroll, etc.  I knew there had to be an easier way to collect this information without having to key each ticket manually or without using a clunky, slow scanner.  After a bit of research, I found an API for OCR from Haven OnDemand and I wrote a simple script to use the camera on his laptop to snap pictures of the tickets, scrape the text and position of the text from the tickets, store it all in a MySQL database, and retain the image of the tickets in a digital archive. Demo: Snapping image via webcam and storing text The script itself is actually very simple: <?php $con=mysqli_connect(localhost,<user>,<pw>,<db>); $name = date(‘Y-m-d_H:i:s’); $newname=”images/”.$name.”.jpg”; $file = file_put_contents( $newname, file_get_contents(‘php://input’) ); if (!$file) { print “Unable to write image to directory.”; exit(); } else { $filePath = ‘http://’ . $_SERVER[‘HTTP_HOST’] . dirname($_SERVER[‘REQUEST_URI’]) . ‘/’ . $newname; $result_json = file_get_contents(“https://api.idolondemand.com/1/api/sync/ocrdocument/v1?apikey=<dedacted>&url=$filePath&mode=scene_photo”); $json_a=json_decode($result_json,true); $result_left=0; $result_top=0; $result_widht=0; $result_height=0; foreach($json_a[text_block] as $p){ $result_text=htmlspecialchars($p[text]); $result_left=$p[left]; $result_top=$p[top]; $result_width=$p[width]; $result_height=$p[height]; $sql=”insert into image (name,pxleft,pxtop,pxwidth,pxheight,result) values (‘$name’,’$result_left’,’$result_top’,’$result_width’,’$result_height’,’$result_text’)”; $result=mysqli_query($con,$sql); $value=mysqli_insert_id($con); } }…