DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Detecting Text in an Image on the Web in Real Time

Here's a nice walk through for detecting text in an image in the browser using JavaScript libraries—think of the super cool use cases this little API will come in handy for!

Paul Kinlan user avatar by
Paul Kinlan
·
Feb. 01, 17 · Tutorial
Like (5)
Save
Tweet
Share
5.87K Views

Join the DZone community and get the full member experience.

Join For Free

Image title

Click to open video in a new tab


Last year just before the Chrome Dev Summit, Miguel Casas came up to me and showed me something that blew my mind: in the browser using the Shape Detection API. Shortly after that that allowed me to update my QR Code scanner so that I no longer had to include a massive (albeit awesome) port of a QR scanning library.

The Shape Detection API is still in development, and neither the FaceDetection nor the Barcode Detection APIs are available outside experimentations (you need to enable "Experimental Web Platform features" in chrome://flags) but it is a very exciting space to watch and see another platform capability being opened up to developers and users on the web.

The latest addition is the Text Detection API that will take an image and scan it for readable text. The video at the top of this article is a great example (note, I stole some of the code from Miguel but put my own spin on it, notably the synthesis part.)

The model is exactly the same as the Face and Barcode Detection APIs, you get an image (either an img, canvas, or ImgData object) and pass it into an instance of the type of detector you want to use. You can the process the results and perform some action on the data (for example draw on the image where the item was detected). In this case, the results are an Array of DetectedText which you can use to extract what text was detected.

This currently works only on Chrome Canary for Android, but if you want to experiment check out the code and the demo, and the process isn't too painful although my code is incredibly hacky. I quite like this demo, it detects text in the image, draws a box around the text and then when the user clicks inside the bounding box, it will read the text back to the user.

The API is not amazingly complex. If you wanted to implement something yourself you can follow these steps:

1. Get Access to the Camera

Query the list of mediaDevices and select the first camera that is front-facing (note: there are better ways to do this).

navigator.mediaDevices.enumerateDevices()
  .then((devices) => {
    let thedevice;
    for(let device of devices) {
      if (device.kind == 'videoinput') {
        thedevice = navigator.mediaDevices.getUserMedia({
          "video": {
            deviceId: {exact : device.deviceId},
            width: { max: 640 }
          }
        });
      }
    }
    return thedevice;
}) 

2. Capture a Full Resolution Frame

capturer = new ImageCapture(theStream.getVideoTracks()[0]);
capturer.grabFrame().then(frame => {  /* */  })

3. Create a TextDetector and Start Detection

var textDetector = new TextDetector(); 
return textDetector.detect(frame).then(boundingBoxes => { /* */ }) 

4. Process the Results

For each item that is detected and element will appear in the array that is passed to the Promise returned from the detect function. You can then itterate over this array, find where they are positioned in the image, and get access to the data detected.

for(let box of boundingBoxes) {
  // box.boudingBox => DOMRect
  speechSynthesis.speak(new SpeechSynthesisUtterance(box.rawValue));
} 

Yup, I Am Excited!

This API opens up so many interesting possibilities for users such as easier and broader access to assistive technologies for parsing content in images; real-time translation of text in images; extracting urls for slides at conferences (totally going to try and get a web app ready for Google IO that does this)... these are just a few examples that quickly spring to mind.

What would you do with this API?

Published at DZone with permission of Paul Kinlan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Unleashing the Power of JavaScript Modules: A Beginner’s Guide
  • What Java Version Are You Running? Let’s Take a Look Under the Hood of the JDK!
  • Distributed SQL: An Alternative to Database Sharding
  • Using QuestDB to Collect Infrastructure Metrics

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: