I will be using Raspberry Pi Model 3 B+. But you can use any model of raspberry and any brand SoC or Practically any computer.
Here I used 1 web cameras & 1 USB Camera.
Because, I dont have Two IP camera and cannot buy due to the lockdown.
You can also do it with one USB camera & Raspberry Pi camera Module. The problem while using two USB camera is that USB 2.0 bandwidth limitation. You cannot stream 2 usb camera on the same bus without using a external hub. That is the reason I have switched to IP cameras.
Second reason is that you can place your Raspberry Pi anywhere. And also you can run this script on a system without having to buy or spare a raspberry pi.
STEP 1 : Installing MySQL server.If you are familiar with database and wish to setup different SQL Server, you can as long as you change the user id, password in the script.
There are many tutorials on the web for installing MySQL server in raspberry pi. To install MySQL Server in Raspberry Pi, you need to run the following commands.
First, update your system.
sudo apt-get update && sudo apt-get upgrade -y
Install the mysql server. If you get any error try, mysql-server instead.
sudo apt-get install default-mysql-server
You will be prompted to enter a root password. If not run the below code, else ignore it. login to mysql and create a new user. I created root and password. Feel free to change it.
DROP USER 'root'@'localhost';
CREATE USER 'root'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
then install phpmyadmin. Answer the questions like user id, password
sudo apt-get install phpmyadmin
sudo phpenmod mysqli
sudo /etc/init.d/apache2 restart
Now, the Installation of mysql is complete. Now, we need to create database and tables for storing our data.
Login to your phpmyadmin by navigating to ip_address/phpmyadmin login using your credentials and click SQL in the upper tabs. Copy the following queries, and execute it.
CREATE DATABASE Attendance;
CREATE TABLE `Attendance`.`Users` ( `EMP_ID` INT NOT NULL , `EMP_Name` VARCHAR(50) NOT NULL , `Designation` VARCHAR(20) NOT NULL , `Joined` TIMESTAMP NOT NULL ) ENGINE = InnoDB;
CREATE TABLE `Attendance`.`Attendance_IN` ( `EMP_ID` INT NOT NULL, `IN_Time` TIMESTAMP NOT NULL ) ENGINE = InnoDB;
CREATE TABLE `Attendance`.`Attendance_OUT` ( `EMP_ID` INT NOT NULL, `OUT_Time` TIMESTAMP NOT NULL ) ENGINE = InnoDB;
All the codes are given at the end of this project.
I am using a SQL Server that is already installed in another system. Only UI differs the procedures stays the same.
The following libaries are required to run this project.
- Opencv
- Pillow
- Numpy
- Mysql Connector
All the dependencies can be installed using requirements.txt in final code link.
pip3 install -r requirements.txt
The cascade_classifier is obtained from opencv github repository.
Download the classifier (data/haarcascades/haarcascade_frontalface_default.xml) from the above github and place it the folder along with main.py.
## Don't Rename it ##
Now, you can run the code. But before doing so, you need to open main.py and change the ip_address of the SQL server, user id, password. If you have installed Mysql on the same device then leave it the same (localhost or 127.0.0.1).
Create a folder called Images in the same directory as main.py and FR.py.
Last, the command line arguments. To run the code, execute the following.
python3 main.py ****** IN/OUT
Here replace, ****** with your camera link.
i.e rstp://ip_address:port/* or http://ip_address:port/
Depending on the model and make of your camera. Some also requires username and password. The IN/OUT defines whether the particular camera is placed through in or out.
You need to run terminal window side by side with 2 different commands.
If you get any import error on opencv try adding the following line in the front. Like so.
LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1.2.0 python3 main.py 0 OUT
This troubleshooting is fetched from piwheel github page.
STEP 4: To view the Data.You can view the data by logging in to the sql server.
Here are the queries to view data.
select * from `attendance`.`users`;
select * from `attendance`.`attendance_in`;
select * from `attendance`.`attendance_out`;
Comments