Disclosure of Invention
The problem of prior art H5 only can carry out ID card OCR through the mode of shooing and album selection, and because the mode of shooing is different, lead to the ID card photo quality of uploading to be different, OCR discernment error rate is high is solved.
In order to solve the technical problems, the invention adopts the following technical means:
a method for realizing OCR (optical character recognition) by H5, comprising the following steps:
step 1, acquiring a Video stream provided by a camera by adopting a getUserMedia () method through a media capture API of HTML5, adding a Video tag of HTML5, and taking a Video acquired from the camera as an input source of the tag;
step 2, setting a viewing frame by using WebRTC, adding DIV to set a camera mask for fixing the picture in the mask by a user, and realizing uniform quality of the uploaded picture;
step 3, taking a picture by adopting a Canvas function of HTML5 to capture the content of the Video tag in real time, snap-shooting a picture, and cutting according to the mask frame;
step 4, comparing the identity card images collected in the step 3 through an average absolute difference algorithm to judge whether the images are front and back images of the identity card, if so, storing the images, otherwise, returning to the step 3
Step 5, respectively carrying out base64 coding on the two stored identity card pictures;
and 6, uploading the base64 bit code obtained in the step 5 to a server, realizing an OCR function through an OCR recognition algorithm, carrying out rule verification on data recognized by the OCR, returning a verification result to a page for reverse display, and confirming by a client.
In the above technical solution, in step 3, the data of the Canvas is converted into a PNG image encoded with 64 bits of base by using the toDataURL of the Canvas.
In the above technical scheme, in step 4, the front side of the identity card collected in step 3 is compared with the template picture of the front side of the identity card by an average absolute difference algorithm, whether the picture contains name, gender, birth, address and citizen identification number is checked, if not, the step 3 is returned to for rescanning, if yes, the current picture of the identity card is stored, meanwhile, the step 3 is returned to for scanning the back side of the identity card, the step 3 is compared with the template picture of the back side of the identity card collected in step 3 and you want to check whether national emblems exist, if not, the step 3 is returned to for rescanning, and if yes, the current picture of the identity card is stored.
In the above technical solution, in step 6, the following rules are checked for the data after OCR recognition: the name, the ID card number, the date of birth, the validity period, the issuing organization, whether the household registration address is null, whether the ID card number is 18 digits, and whether the gender is consistent with the last but one after being converted into the code value.
The invention also provides a device for realizing OCR (optical character recognition) by H5, which comprises the following modules:
an input source module, which obtains a Video stream provided by a camera by adopting a getUserMedia () method through a media capture API of HTML5, adds a Video tag of HTML5, and takes the Video obtained from the camera as an input source of the tag;
the mask module is used for setting a view-finding frame by using WebRTC, adding DIV to set a camera mask and fixing pictures in the mask by a user so as to realize uniform quality of uploaded pictures;
the snapshot module and the shooting are to capture the content of the Video tag in real time by adopting the Canvas function of HTML5, snapshot a picture and cut according to the mask frame;
the comparison module compares the acquired image of the identity card by an average absolute difference algorithm to judge whether the acquired image is a front image or a back image of the identity card, if so, the acquired image is stored, otherwise, the acquired image returns to the snapshot module;
the coding module is used for respectively carrying out base64 coding on the two stored identity card pictures;
the recognition and verification module uploads the obtained base64 bit code to the server, an OCR function is realized through an OCR recognition algorithm, data after OCR recognition is regularly verified, the verification result is transmitted back to the page to be displayed reversely, and the client confirms the result.
In the above technical solution, in the snapshot module, the toDataURL of the Canvas is adopted to convert the data of the Canvas into a PNG image encoded with base64 bits.
In the technical scheme, in the comparison module, through an average absolute difference algorithm, the front face of the identity card collected in the snapshot module is compared with the template picture of the front face of the identity card, whether the picture contains a name, a gender, a birth, an address and a citizen identity number or not is checked, if the picture does not contain the name, the gender, the birth, the address and the citizen identity number, the snapshot module is returned to scan again, if the picture contains the name, the current identity card picture is stored, meanwhile, the snapshot module is returned to scan the back face of the identity card, the back face of the identity card collected in the snapshot module is compared with the template picture of the back face of the identity card and a user wants to check whether a national emblem exists or not, if the back face of the identity card collected in the snapshot module does not exist, the snapshot module is returned to scan again, and the current identity card picture exists, and the current identity card picture is stored.
In the above technical solution, in the recognition and verification module, the following rules are verified for the data recognized by the OCR: the name, the ID card number, the date of birth, the validity period, the issuing organization, whether the household registration address is null, whether the ID card number is 18 digits, and whether the gender is consistent with the last but one after being converted into the code value.
The invention also provides a storage medium, which stores a program for realizing OCR (optical character recognition) by H5, and when the program is executed by a processor, the program realizes a method for realizing OCR by H5 as claimed in claims 1-4.
Because the invention adopts the technical scheme, the invention has the following beneficial effects:
1. the invention carries out OCR identification of the identity card based on the H5 terminal, and only needs to align the identity card in real time by a mobile phone and wait for an identification result without pressing a photographing key.
2. The identification card picture adopts the average absolute difference algorithm to judge whether the picture is the identification card picture, so that other pictures which are not the identification card picture are not identified.
3. And (5) carrying out rule judgment on the identification result to prompt the user whether the identification result is correct or not.
Detailed Description
The OCR scanning recognition module is newly built, code logic is written, through programmable access to a camera provided by The Media Capture API of HTML5, a Video stream provided by The camera can be directly obtained by a getUsermedia () method, a Video tag of HTML5 is added, and Video obtained from The camera is used as an input source of The tag;
setting a view-finding frame by using WebRTC, adding DIV to set a camera mask for fixing a picture in the mask by a user, and realizing the uniformity of the picture quality at the last time;
the method comprises the steps that the Video label content is captured in real time by adopting the Canvas function of HTML5, the Video element can be used as the input of a Canvas image, and the core idea of obtaining picture data from the Canvas is to convert the Canvas data into a base64 bit coded PNG image by using the TODataURL of the Canvas, which is similar to 'data: image/PNG'; base64, xxxxx ", so that the imgData variable stores a long string of character data content representing a base64 encoding of a PNG image. Since the real image data is the part after base64 encoded comma, we can obtain it in two ways to let the image data received by the real server be this part. The first is to intercept 22-bit character string at the front end as image data; and the second method comprises the following steps: after the transmitted data is acquired at the back end, the character string after 22 bits is intercepted by a background language (namely, the character string is directly uploaded by skipping the above step in the foreground);
after the picture is obtained, the picture is transcoded by base64 and uploaded to a server, an OCR function is realized by an OCR recognition algorithm, and recognized information (including error information) is transmitted back to a page for reverse display.
Part of the code is as follows:
acquiring a camera:
<input type="file"accept="image/*;capture=camera">
<input type="file"accept="video/*;capture=camcorder">
< input type ═ file "accept ═ audio/>; capture "> set the viewfinder using WebRTC:
<div style="width:100%;position:fixed;left:0;bottom:0;top:0;right:0;">
<img src="cover.png"style="width:100%;height:100%;position:fixed;left:0;bottom:0;top:0;right:0;"/>
<video></video>
<canvas id="mycanvas"></canvas>
</div>
the img label is a framing frame covering layer;
the video tag is used for displaying the video stream of the camera;
a canvas is used to capture a picture from a video stream and upload it.