WebAssembly-based method for controlling website with face tracking function
By compiling C/C++ code into WebAssembly modules and combining the Haar cascade classifier and Kalman filter algorithm, the performance bottleneck and security issues of facial tracking in JavaScript are solved, achieving efficient and safe facial tracking functions, and ensuring Cross-browser compatibility.
Patent Information
- Application Number
- CN202410528229.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-29
- Publication Date
- 2024-09-17
AI Technical Summary
The existing technology has performance bottlenecks when performing real-time image processing in JavaScript, security and privacy issues during facial tracking, and poor cross-browser portability.
Use WebAssembly to compile C/C++ code into a module that can run in the browser, combine the Haar cascade classifier and Kalman filter algorithm for facial tracking, and call the WebAssembly module through glue Javascript to ensure performance and security, and pass compatibility testing Ensure cross-browser operation.
It improves the performance and real-time performance of facial tracking, ensures the security of user data, and achieves compatibility on different browsers and operating systems, providing wider user coverage.
Smart Images

Figure CN118658187A
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer vision, and in particular to a method for controlling a website with face tracking based on WebAssembly. Background Art
[0002] WebRTC is commonly used to obtain the user's video stream in a web browser. WebRTC is a real-time communication technology that allows the browser to communicate directly with the device's camera and microphone to achieve real-time audio and video transmission. By requesting user authorization to access the camera, WebRTC can obtain a real-time video stream from the user's device. JavaScript, as the main client-side scripting language, is used to process the video stream obtained from WebRTC and perform facial tracking. It can be used with the WebRTC API and other libraries to implement video stream processing and analysis. Computer vision libraries use various facial tracking algorithms to detect and track faces. These algorithms may include feature point-based methods (such as feature point detectors) or deep learning-based methods (such as face recognition neural networks). These algorithms identify and track the user's facial features based on the input video stream.
[0003] Although existing technologies can achieve the functions of facial tracking and control to a certain extent. However, these existing technical solutions may have some limitations and challenges: 1. Performance issues: Real-time image processing in JavaScript may cause performance bottlenecks, especially when processing high-resolution videos. Facial tracking usually requires fast and accurate calculations, so performance is a key issue. 2. Security issues: Since facial tracking involves the use of cameras and access to users' personal information, security and privacy issues are also important aspects to consider. 3. Portability: Since different browsers may have different levels of support for WebRTC and computer vision libraries, implementing cross-browser facial tracking control websites may face some challenges. Summary of the invention
[0004] The purpose of this section is to summarize some aspects of embodiments of the present invention and briefly introduce some preferred embodiments. Some simplifications or omissions may be made in this section and the specification abstract and the invention title of this application to avoid blurring the purpose of this section, the specification abstract and the invention title, and such simplifications or omissions cannot be used to limit the scope of the present invention.
[0005] In order to solve the above technical problems, the present invention provides the following technical solutions:
[0006] A method for controlling a website with face tracking based on WebAssembly, comprising:
[0007] The face tracking module detects faces in real time and tracks their positions in consecutive frames based on the input image or video data, collects a large-scale face image dataset with facial key point annotations, and performs preprocessing;
[0008] Load the WebAssembly module and use the compiler toolchain to compile native C and C++ code into WebAssembly format so that these codes can run in the browser;
[0009] Load and instantiate the WebAssembly module, place the WebAssembly module file on a specific server, and load it through fetch;
[0010] Call the face tracking algorithm to interact with the user, call the functions and objects in the WebAssembly module through the glue Javascript code, pass the video frame to the face tracking algorithm for processing, and draw the detected face box on the page canvas.
[0011] As a preferred solution of the method of controlling a website with face tracking based on WebAssembly described in the present invention, the face tracking module adopts a Haar cascade classifier, and a pre-trained model provided by OpenCV, such as cv2.CascadeClassifier, can be used.
[0012] As a preferred solution of the method for controlling a website with facial tracking based on WebAssembly described in the present invention, the Haar cascade classifier is trained using the AdaBoost algorithm.
[0013] As a preferred solution of the method of controlling a website with face tracking based on WebAssembly described in the present invention, face tracking is the process of tracking a detected face in consecutive frames, which usually involves using a tracking algorithm to predict the position of the face in the next frame, using Kalman filtering.
[0014] As a preferred solution of the method of a WebAssembly-based face tracking control website described in the present invention, after face tracking, post-processing is performed, and moving average smoothing technology is used to reduce noise and jitter in continuous data, while retaining trend information to remove false detections or smooth tracking results.
[0015] As a preferred solution of the method of controlling a website with facial tracking based on WebAssembly described in the present invention, the written C or C++ code is passed to the compiler, and the codes are first converted to LLVM IR, and the converted LLVM IR is translated into WebAssembly bytecode.
[0016] As a preferred solution of the method for controlling a website with facial tracking based on WebAssembly described in the present invention, the binary data of the .wasm file is obtained by fetch, and then the binary data is compiled into a WebAssembly module object by using WebAssembly.compile, and instantiation is realized by using WebAssembly.instantiate.
[0017] As a preferred solution of the method for controlling a website with facial tracking based on WebAssembly described in the present invention, wherein: by setting the functions, memory, etc. to be imported in importObject, and then accessing the functions and objects exported in the module through instance.exports, while adding more error handling and adaptation, the server must correctly configure the MIME type of the WebAssembly module.
[0018] As a preferred solution of the method for controlling a website with face tracking based on WebAssembly described in the present invention, the functions and objects in the WebAssembly module are called through the glue Javascript code, the video frame is passed to the face tracking algorithm for processing, and the detected face frame is drawn on the page canvas.
[0019] As a preferred solution of the method of controlling a website with facial tracking based on WebAssembly described in the present invention, the web page obtains the user's current action and the offset of the current action through the callback function of the glue Javascript, and then calls the DOM event of the web.
[0020] Beneficial effects of the present invention:
[0021] 1. Convert the face tracking algorithm from languages like Python to C / C++, and then compile it to a WebAssembly module through Emscripten so that it can run in the browser. WebAssembly can efficiently run performance-intensive algorithms such as face tracking in the browser. Compared with traditional JavaScript implementations, WebAssembly provides higher execution speed and better performance, making face tracking smoother and more real-time.
[0022] 2. Choose a lightweight face detection algorithm suitable for WebAssembly, such as a streamlined version of the Haar cascade classifier or a deep learning-based algorithm (such as SSD, YOLO, etc.). Lightweight algorithms can run faster in WebAssembly and take up less system resources. This helps improve the performance and responsiveness of the website.
[0023] 3. Test and handle compatibility issues of different user devices to ensure that the website can run normally on different browsers and operating systems. Compatibility testing and processing can provide a wider user coverage, ensuring that users can enjoy the facial tracking control function no matter what device they use.
[0024] 4. Ensure the privacy and security of user data when processing user image or video data in the WebAssembly module. User data security is an important consideration in website development. Through appropriate encryption and security measures, user data is protected from unauthorized access and use. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required for describing the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative labor. Among them:
[0026] Figure 1 A flowchart of a method for controlling a website with face tracking based on WebAssembly proposed by the present invention;
[0027] Figure 2 A flowchart of a method for controlling a website with facial tracking based on WebAssembly proposed by the present invention;
[0028] Figure 3 The present invention provides a WebAssembly loading and running process in a method for controlling a website with facial tracking based on WebAssembly. DETAILED DESCRIPTION
[0029] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are described in detail below in conjunction with the accompanying drawings.
[0030] In the following description, many specific details are set forth to facilitate a full understanding of the present invention, but the present invention may also be implemented in other ways different from those described herein, and those skilled in the art may make similar generalizations without violating the connotation of the present invention. Therefore, the present invention is not limited to the specific embodiments disclosed below.
[0031] Secondly, the term "one embodiment" or "embodiment" as used herein refers to a specific feature, structure, or characteristic that may be included in at least one implementation of the present invention. The term "in one embodiment" that appears in different places in this specification does not necessarily refer to the same embodiment, nor does it refer to a separate or selective embodiment that is mutually exclusive with other embodiments.
[0032] Secondly, the present invention is described in detail with reference to the schematic diagram. When describing the embodiments of the present invention in detail, for the sake of convenience, the cross-sectional diagrams showing the device structure will not be partially enlarged according to the general scale, and the schematic diagrams are only examples, which should not limit the scope of protection of the present invention. In addition, in actual production, the three-dimensional dimensions of length, width and depth should be included.
[0033] Reference Figure 1-3 The present invention provides a method for controlling a website with face tracking based on WebAssembly, comprising:
[0034] The face tracking module detects faces in real time and tracks their positions in consecutive frames based on the input image or video data. It collects a large-scale face image dataset with facial key point annotations and performs preprocessing, including image normalization, alignment, and quality control.
[0035] Load the WebAssembly module and use the compiler toolchain to compile native C and C++ code into WebAssembly format so that these codes can run in the browser;
[0036] Load and instantiate the WebAssembly module, place the WebAssembly module file on a specific server, and load it through fetch;
[0037] Call the face tracking algorithm to interact with the user, call the functions and objects in the WebAssembly module through the glue Javascript code, pass the video frame to the face tracking algorithm for processing, and draw the detected face frame on the page canvas so that the user can observe his movements in real time.
[0038] The face tracking module uses the Haar cascade classifier, and you can use the pre-trained model provided by OpenCV, such as cv2.CascadeClassifier. Haar feature is a feature descriptor based on rectangular boxes. The Haar feature classifier uses multiple rectangular boxes of different sizes and shapes to represent features, and determines whether a specific feature exists by calculating the difference in pixel values within these rectangular boxes.
[0039] Furthermore, the Haar cascade classifier is trained using the AdaBoost algorithm. Assume that we have a binary image (a binary version of a grayscale image), with 1 representing the target area and 0 representing the non-target area. The Haar feature can be calculated in the following form: H(x, y) = ∑ i w i ·f i (x,y), where f i (x,y) is the i-th Haar feature, W i is its corresponding weight. For face detection, the Haar features commonly used are horizontal rectangles, vertical rectangles, and diagonal rectangles. The AdaBoost algorithm used in the Viola-Jones algorithm is an ensemble learning method that improves the accuracy of face detection by combining multiple weak classifiers to form a strong classifier.
[0040] Furthermore, face tracking is the process of tracking a detected face in consecutive frames, which usually involves using a tracking algorithm to predict the position of the face in the next frame, using Kalman filtering, which is a recursive state estimation algorithm used to estimate the state produced by a dynamic system. In face tracking, we can use Kalman filters to track the position and velocity of the face. Suppose we use x t To represent the position of the face in the tth frame, v t to represent the speed of the face in the tth frame. Then the state vector of the Kalman filter is The Kalman filter consists of two main steps: the prediction step and the update step. In the prediction step, we predict the state of the current frame based on the state estimate of the previous frame. This can be achieved using the state transfer matrix A and the control vector u: in, is the predicted state vector. In the update step, we use the predicted state vector and the observed actual position to correct the state estimate. This can be done by measuring the matrix H and the observation vector z t To achieve Among them, X t is the updated state vector, K t is the Kalman gain, which is used to weigh the predictions and observations.
[0041] Furthermore, after face tracking, post-processing is performed, using moving average smoothing technology to reduce noise and jitter in continuous data while retaining trend information to remove false detections or smooth tracking results. For the tracking results in continuous frames, assume that we have a series of face position coordinate data: (x1, y1), (x2, y2),…, (x n ,y n ). Among them, (x i ,y i ) represents the face position detected in the i-th frame. The moving average formula can be expressed as: Where N is the size of the moving average window, x i and i are the horizontal and vertical coordinates of the face position detected in the i-th frame. Here, we average the continuous N frames of data to get the smoothed position, thereby reducing the jitter of the position data. The window size N determines the degree of smoothness. A larger window will be smoother, but it will also make the response slower. Choosing the appropriate window size needs to be determined according to specific application requirements and scenarios.
[0042] Furthermore, the written C or C++ code is passed to the compiler, which first converts the code into LLVM IR, and the converted LLVM IR is translated into WebAssembly bytecode. These optimizations are designed to improve the performance and efficiency of the code, while also reducing the size of the generated WebAssembly code. At the same time, the compiler generates a binary file containing a WebAssembly module, which contains the necessary information to translate C / C++ code into WebAssembly instructions. Although WebAssembly is an independent binary format, it is often used with JavaScript. The compiler links the WebAssembly module with the JavaScript code so that it can run in the browser.
[0043] Furthermore, we fetch the binary data of the .wasm file, and then use WebAssembly.compile to compile the binary data into a WebAssembly module object, which is instantiated through WebAssembly.instantiate. Once the module is successfully loaded and compiled, it can be instantiated.
[0044] Furthermore, by setting the functions and memory to be imported in importObject, and then accessing the exported functions and objects in the module through instance.exports, more error handling and adaptation are added. The server must correctly configure the MIME type of the WebAssembly module so that the browser can correctly load and parse the module.
[0045] Furthermore, the glue Javascript code calls the functions and objects in the WebAssembly module, passes the video frame to the facial tracking algorithm for processing, and draws the detected facial frame on the page canvas, so that the user can observe their movements in real time, and at the same time, the current movement of the face is processed: left, right, up, down, nodding, etc. There are also the left deviation, right offset, upward offset, downward offset, etc.
[0046] Furthermore, the web page obtains the user's current action and the offset of the current action through the callback function of the glue Javascript, and then calls the web's DOM event, allowing the user to control the page's sliding down, left, up, and right through the face, and to achieve the click effect of the page by nodding, and so on.
[0047] It should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention rather than to limit it. Although the present invention has been described in detail with reference to the preferred embodiments, those skilled in the art should understand that the technical solutions of the present invention may be modified or replaced by equivalents without departing from the spirit and scope of the technical solutions of the present invention, which should all be included in the scope of the claims of the present invention.
Claims
1. A method for controlling a website with facial tracking based on WebAssembly, characterized in that: include: The face tracking module detects faces and tracks their positions in consecutive frames in real time based on the input image or video data, collects a large-scale face image dataset with facial key point annotations, and performs preprocessing; Load the WebAssembly module and use the compiler toolchain to compile native C and C++ code into WebAssembly format so that these codes can run in the browser; Load and instantiate the WebAssembly module, place the WebAssembly module file on a specific server, and load it through fetch; Call the face tracking algorithm to interact with the user, call the functions and objects in the WebAssembly module through the glue Javascript code, pass the video frame to the face tracking algorithm for processing, and draw the detected face box on the page canvas.
2. A method for controlling a website with facial tracking based on WebAssembly according to claim 1, characterized in that: The face tracking module uses the Haar cascade classifier, and you can use the pre-trained models provided by OpenCV, such as cv2.CascadeClassifier.
3. A method for controlling a website with facial tracking based on WebAssembly according to claim 2, characterized in that: The Haar cascade classifier is trained using the AdaBoost algorithm.
4. A method for controlling a website with facial tracking based on WebAssembly according to claim 3, characterized in that: Face tracking is the process of following a detected face across consecutive frames, which typically involves using a tracking algorithm to predict where the face will be in the next frame, using a Kalman filter.
5. A method for controlling a website with facial tracking based on WebAssembly according to claim 4, characterized in that: After face tracking, post-processing is performed using a moving average smoothing technique to reduce noise and jitter in continuous data while retaining trend information to remove false detections or smooth tracking results.
6. A method for controlling a website with facial tracking based on WebAssembly according to claim 5, characterized in that: The written C or C++ code is passed to the compiler, which first converts the code into LLVM IR, which is then translated into WebAssembly bytecode.
7. A method for controlling a website with facial tracking based on WebAssembly according to claim 6, characterized in that: Get the binary data of the .wasm file through fetch, then use WebAssembly.compile to compile the binary data into a WebAssembly module object, and use WebAssembly.instantiate to instantiate it.
8. A method for controlling a website with facial tracking based on WebAssembly according to claim 7, characterized in that: By setting the functions and memory to be imported in importObject, and then accessing the functions and objects exported in the module through instance.exports, while adding more error handling and adaptation, the server must correctly configure the MIME type of the WebAssembly module.
9. A method for controlling a website with facial tracking based on WebAssembly according to claim 8, characterized in that: The glue Javascript code calls the functions and objects in the WebAssembly module, passes the video frame to the face tracking algorithm for processing, and draws the detected face box on the page canvas.
10. A method for controlling a website with facial tracking based on WebAssembly according to claim 9, characterized in that: The web page obtains the user's current action and the offset of the current action through the callback function of the glue Javascript, and then calls the web's DOM event.
