0

Detecting text in an image on the web in real-time

The Shape Detection API is still in development, and neither the FaceDetection nor the Barcode Detection API’s 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 a 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 detect). 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 is not 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 are just a few examples that quickly spring to mind.

Originally Posted at https://paul.kinlan.me/detecting-text-in-an-image/

 

0

Generate random numbers within range

A formula for generating a random number within a given range using JavaScript is as follows:

Math.floor(Math.random() * (UpperRange - LowerRange + 1)) + LowerRange;

 

To generate a random number between 25 and 75, the equation would be
Math.floor(Math.random() * (75-25+1)) + 25;

i.e.
Math.floor(Math.random() * 51) + 25;

0

Limit Checkbox selections

The below JavaScript jQuery snippet helps in limiting checkbox selections out of all available checkboxes in any particular page/scenario.

$('input[type="checkbox"]').click(function() {
var limit = 3,
bool = $('input[type="checkbox"]:checked').length >= limit;
$('input[type="checkbox"]').not(":checked").attr("disabled",bool);
});

jsFiddle

0

How To Stop YouTube From Displaying Other People’s Related Videos At The End of Your Video

yT

Often when we embed YouTube video for a blog or client, at the end of the YouTube video we see a bunch of related videos. The viewers tend to watch other videos or move out of the website itself. How do we avoid showing other related videos at the end of out videos ?

The solution is simple, we need to add an additional parameter to the embedded YouTube URL. Add the following text, &rel=0.

So, for example, if the embed code for one of the videos is:
<!iframe width=”560″ height=”315″ src=”//www.youtube.com/embed/3ygRTw” frameborder=”0″ allowfullscreen></iframe>

We would simply need to add additional parameter to the URL.

<!iframe width=”560″ height=”315″ src=”//www.youtube.com/embed/3ygRTw&rel=0” frameborder=”0″ allowfullscreen></iframe>

All done. No more related video suggestions popping up after our videos!

0

HTML SSI – The forgotten awesomeness!!

Server Side Includes or SSI is an age old simple interpreted server-side scripting language used almost exclusively for the web. It basically consists a set of directives that are inserted in an HTML page which are evaluated on the server when the page is being served. Simply put it allows for more modular and dynamic HTML. The most important use of SSI is however for creating prototypes without using languages like PHP for including files etc and again to keep things modular.

As an interactive developer many times we are given VD’s (visual designs) in Photoshop which need to be converted into static HTML templates that can be integrated with the Server side scripting logic. In most cases we have the assets i.e. the CSS, Images and JS  files arranged in a modular fashion for better understanding. But what about the modules used inside the HTML like header, main menu , sidebar or footers. They are merely copy pasted from one HTML to another.  Well that’s not modular !!. So here is what you need to do.

The Apache set up:

1. Stop your apache server if it is running.

2. Open the apache httpd.config file and locate the following lines

AddType text/html .shtml

AddOutputFilter INCLUDES .shtml
Once located insert the following on a new line
Options +Includes

just after the line “AddOutputFilter INCLUDES .shtml”
(This tells Apache that you want to permit files to be parsed for SSI directives.)

3.  Next we would like to add some legacy support for directives if the apache instance has been updated recently.

Add the following lines inside a Directory tag in the httpd.config file.

    <IfModule include_module>
          SSILegacyExprParser on
    </IfModule>

4. Restart your apache server.

Using Grunt:

Finally if you have to dispatch the work you have done for code review or html validation or to a client who hasn’t set up apache you can use the grunt task defined in the project folder to  create a distribution folder containing the compiled .shtml file into pure well formed .html pages with all the includes appearing as inline HTML.

For more info on SSI and the various directives that exist please visit the following links:

Apache Tutorial: Introduction to Server Side Includes – Apache HTTP Server

Server Side Includes – Wikipedia, the free encyclopedia

0

JavaScript Interview Question

1.    What is prototype? Give an example?
2.    What are the objects in JavaScript? How to create them?
3.    How to debug JavaScript – which tool u used?
4.    How to access array elements in JavaScript?
5.    What is singleton?
6.    What is inheritance?
7.    What is unobtrusive JavaScript?
8.    What is MVC?
9.    What is the result of 1 + “2” + 3?
10.    What is decodeURI(), encodeURI() in JavaScript?

iQ-JS

0

JavaScript Interview Questions

Most popular JavaScript interview Questions.

1.    What is event bubbling?
2.    What is the difference between null and undefined ?
3.    What is .call() and .apply() function?
4.    What is closure?
5.    What is prototypal inheritance?
6.    Give example to implement inheritance.
7.    What are the methods to create objects?
8.    What is “this” keyword?
9.    What is the use of accesskeys?
10.    What are the different datatype available in JavaScript?

2

Password Strength – jQuery code snippet

<form name="password_strength" method="POST" id="pass_form">
    <label>Enter a password</label>
    <input type="password" name="pass" id="pass" />
    <span id="passstrength"></span>
</form>

$('#pass').keyup(function(e) {
     var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
     var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
     var enoughRegex = new RegExp("(?=.{6,}).*", "g");
     if (false == enoughRegex.test($(this).val())) {
             $('#passstrength').html('More Characters');
     } else if (strongRegex.test($(this).val())) {
             $('#passstrength').className = 'ok';
             $('#passstrength').html('Strong!');
     } else if (mediumRegex.test($(this).val())) {
             $('#passstrength').className = 'alert';
             $('#passstrength').html('Medium!');
     } else {
             $('#passstrength').className = 'error';
             $('#passstrength').html('Weak!');
     }
     return true;
});

jsFiddle: http://jsfiddle.net/aleem/KE3RB/8/