Real time face detection in C++ using Haar Cascade on OpenCV

I was using mostly Python to experiment AI/ML until recently but now it is time to find ways to improve the performance by making use of all the possible resources such as GPU, and including experimental change of language to C++. Here is the sample code I ported from simple Python face detection (not recognition) Hello-World program, and thought to bookmark for future. This uses Haar Cascade machine learning algorithm and you can read more about it here. Definitely, compared to Python code I feel there is some improvement in the performance, which I will publish the benchmarks later in another blog post.

Note: Error handling and other best practice aspects has not been considered in this sample.

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main()
{
VideoCapture cap;
cap.open(0);

CascadeClassifier face_cascade;
face_cascade.load("D:\\OpenCV\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_alt.xml");

while (waitKey(10) != 27) //Press ESC to exit
{
Mat frame;
cap >> frame;

std::vector<Rect> faces;
face_cascade.detectMultiScale(frame, faces, 1.1, 3,0, Size(20, 20));

for(size_t i = 0; i < faces.size(); i++) {
rectangle(frame, faces[i], Scalar(255, 255, 255), 1, 1, 0);
}

imshow("Webcam", frame);
}
}

fd

Enable Windows Explorer previews for custom file types

Coders, have you ever wished if you get preview of source code while you are in Windows Explorer? Yes it is possible. You might know already that there is a “Preview Pane” feature present. According to a file’s Content Type the preview will be shown at the right side of the window. Common formats supported are – .txt, .mp4, .jpg etc.

image

BUT, most of the developer’s files like .cs, .php, .config, .log files are not supported by default and you will be presented with a message “No preview Available” like this:

image

How to enable preview for a specific filetype?

Step 1: Go to Regedit. Expand the node HKEY_CLASSES_ROOT

Step 2: look for your required extension, eg: .php (If the extension is not already present, then create a key)

Step 3: Add a string value PerceivedType and set its Data to text

Voila!, your file preview is ready!

image

Loop multiple videos in HTML5 video tag

<video id="v" autoplay>
       <source src="1.mp4" type="video/mp4">
     </video>

    <script>
         var c = 0;
         f = [
                 "1.mp4",
                 "2.mp4",
                 "3.webm"
             ];
         var v = document.getElementById('v');
         v.addEventListener('ended', function(e) {
             c = (c < f.length-1)?++c:0;
             v.src = f[c];
             v.play();
         });
     </script>

Ajax file uploader using jQuery

Here is a sample code to upload multiple files using ajax, and without using the traditional <form> tags. Find the full source code in GitHub – https://github.com/ninethsense/code-share/tree/master/jQuery-File-Upload .

<body>
    <input type="button" value="Choose file" id="fup" />

    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>    

    <script>
            $(function() {

                // Show file upload dialog on click of button
                $("#fup").click(function(){

                    //Create File component
                    var fu = document.createElement("input");
                    fu.type = "file";
                    fu.id="fuc";
                    $(fu).attr("multiple","");

                    // Trigger file selection event
                    $(fu).change(function(){
                        var fd = new FormData();
                        $.each($(fu)[0].files, function(i, file){
                            fd.append('file[]', file);
                        });

                        // Send fd to server
                        $.ajax({
                            url:"FileUpload.php",
                            type: 'POST',
                            data:fd,
                            cache: false,
                            contentType: false,
                            processData: false,
                            success: function(d) {
                                console.log(d);
                            }
                        });
                    });

                    // To invoke the file selection dialog box
                    $(fu).click();

                });
            });
    </script>
</body>

Sample server code in PHP for the reference:

foreach ($_FILES['file']['tmp_name'] as $k => $v) {
move_uploaded_file(
$_FILES['file']['tmp_name'][$k],
$_FILES['file']['name'][$k]
);
}

Whitepaper on Principles of container-based application design by Red Hat

Good read – https://www.redhat.com/en/resources/cloud-native-container-design-whitepaper

Cloud-native” is a term used to describe applications designed specifically to run on a cloud-based infrastructure. Typically, cloud-native applications are developed as loosely coupled microservices running in containers managed by platforms. These applications anticipate failure, and they run and scale reliably even when their underlying infrastructure is experiencing outages. To offer such capabilities, cloud-native platforms impose a set of contracts and constraints on the applications running on them. These contracts ensure that the applications conform to certain constraints and allow the platforms to automate the management of the containerized applications.