Webcam Face Detection Using JavaScript, PHP, and MySQL 2023

In the past, I had published a very popular blog post regarding JavaScript Face Detection and Recognition Libraries. So, in this tutorial, I’ll use a library from that list which is known as “pico.js“.

We’ll use this JavaScript library to detect a human face in webcam feed. An interesting thing is that this library is consists of just around 200 lines of code. Meaning that it is very lightweight and easy to use inside web projects.

Basic functionality of this project

  • Real-time human face detection in Webcam video.
  • When face is detected then get its image from Webcam.
  • Upload the image to server.
  • Save image path in database so that we can display it later.

Create Database and Table

Create a database using phpMyAdmin or below SQL.

SQL

Create a table to store images path.

SQL

Files and folders used in this tutorial

First of all, create a folder on your local environment (e.g. inside XAMPP “htdocs” folder). Name it anything you like. But, for the sake of this tutorial, I’ll name it as face_detection_project.

Basically, it will be used to hold our project files and folders.

Now, below is a list of files and folders that we will create in this tutorial. It will help you get an overview of the project’s folder structure.

  • images
  • picojs
  • db_connect.php
  • save.php
  • view.php
  • index.php

Install Pico.js Face Detection Library

  • Download pico.js library from GitHub.
  • Extract the .zip file inside your project folder.
  • Now, change the name of the extracted folder to picojs.

Important Note

I’ve explained important and complex code using comments. So, read them in the below code-snippets.


Detect Face in Webcam using JavaScript

Detect Face in Webcam using JavaScript

Here’s the content for index.php.

HTML

Connect With Database

Here’s the content for db_connect.php

PHP

Store Image on the Server and Database

Here’s the content for save.php

PHP

View Saved Images

view detected face images

Here’s the content for view.php

PHP

Run the Project

  • Create a folder called images inside the project root. Our detected face images will be uploaded here.
  • Open XAMPP or any other PHP local development environment. Now, start Apache and MySQL.
  • Finally, open the project by typing this URL in your web browser’s address bar: localhost/face_detection_project

Download Full Project

Download

6 thoughts on “Webcam Face Detection Using JavaScript, PHP, and MySQL 2023”

  1. thank you for the tutorial but i have a problem in my database because the script is storing more than one image in my database, i would like to know how to change it please

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.