Face Detection using opencv – python on windows along with serial port control​

We will start from downloading python to installing python as an initial step. Follow the Step by step process below to install python on windows.

Materials Required / Supplied to Build The Project (Optional).

Artificial Intelligence / Machine Learning Kit BOM
ParticularsQuantity
7 Segment LED Display Common Cathode1
NodMCU CP21021
USB Cable for CP21021
breadboard 400 points1
Jumper wires (M-M)10
LED1
330E resistor1

To place order or for bulk enquiry , mail us at admin@pinwheel.in

Download Python 3.10.4

 

http://www.python.org

Disable Path Length Limit

 

Verify the Python Install​

 

python

Now we will install python package manager (commonly called as pip). This is used to download and install various pre-built libraries that we use while writing code.

Install pip – Python Package Manager​

 

https://bootstrap.pypa.io/get-pip.py

I also suggest to install a Rapid Environment Editor tool , though this is an optional but comes handy when we want to add/edit and environment variables or path in the system. Do remember to run this in administrator mode.

Install Rapid Environment Editor [Optional]

 

https://www.rapidee.com/en/download

Find Python Install Directory from Path 

 

Copy get-pip.py script in the Python Install directory

 

Open cmd prompt in the same path​

 

 

Install Pip -> python get-pip.py​

 

 

 

Next , we will install certain libraries which we will use in our project today.

Install numpy package 

pip install numpy

 

Install matplotlib

pip install matplotlib

 

One most important thing is to install MSVC redistributable , you might have a good question here why for python we are installing C++ redistributable , this is because most of the python libraries are built on C++ and therefore if we would want to build library rather than using the readymade we will need this . Though for today’s project we may not need them but it’s a good practice to keep it installed.

Install MSVC 2015-2019 Redistributable

 

https://docs.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-170

Next most important thing for today’s project is installing opencv library, this is the library that is most commonly used across every industry for image processing and various image and vision related projects is opencv.

Again this library is available as pre-built in pip . We will use pip to install this library.

Opencv download and install​

Pip install opencv-contrib-python​

 

Verify opencv install​
  • Open Python IDLE ​
  • Open cmd window ​
  • Start python by typing python​
  • >>>import idlelib.idle​

>>import cv2 as cv​

>>print (cv.__version__)

 

 

Forum for Understanding about opencv

If you want to learn more please visit below link and spend sometime.

https://docs.opencv.org/4.x/d5/de5/tutorial_py_setup_in_windows.html

Next install imutils again using pip.

Install imutils package​

pip install imutils​

 

There are various methods for detecting face , today we are going to use haar cascade method , I would also suggest to read more about this and also in your next assignment try to use dlib for face detection.

Face Detection Code using haar cascade method

 

https://drive.google.com/file/d/1-DJYagTB2bUJWxeibSHdY_MUzuuVFH4l/view?usp=sharing
https://drive.google.com/file/d/1-DJYagTB2bUJWxeibSHdY_MUzuuVFH4l/view?usp=sharing

 The code in this link contains the code that detects the face and display the number of face detected on frame on a 7 segment display. If you would not want to interface 7 segment display , you can comment the portion that includes pyserial use case.

Run video_face_detector.py​

Right Click On Python File and select Edit with IDLE

 

Run the code from Python IDLE

 

Video capture from Camera​

 

Modify the code to count number of faces​(The code in the link is already modified to count number of faces.)

 

Install pyserial ​

pip install pyserial

 

Access Serial Port from Python

 

Python code to write to serial port​

 

Arduino Code to interface 7 segment Display​
 

Interfacing 7 segment display with NodeMCU Module :

We need to install CP2102 or CH340 driver before we can use NodeMCU .

Check the below post for installing driver – this step is required only if NodeMCU is not assigned COM Port on the system.

Next we will use Arduino IDE to program NodeMCU module.

Download and install Arduino IDE from https://www.arduino.cc/en/software

Arduino IDE

Now we need to add NodeMCU board to Arduino.

Follow the below post to install NodeMCU board into Arduino.

Next we need to make connection of 7 segment display with NodeMCU

First make yourself acqainted with the PIN of NodeMCU ESP-12E

 

Pin Diagram Node MCU

Now make connection as shown below.

7 Segment Display With NodeMCU

Now put the below code into the IDE and Compile and Upload.

 

Code Snapshot

// Pinwheel Robotics tutorial on Seven Segement Display
// Hardware: NodeMCU

String number; // reads the number from the serial monitor
// declared the output pin of 7 segment with arduino
const int h=0; //DP pin
const int c=2;
const int d=5;
const int e=4;
const int g=15;
const int f=13;
const int a=12;
const int b=14;
int valid=1; // variable to check its valid umber given by the users or not

void setup() {
pinMode(a,OUTPUT);
pinMode(b,OUTPUT);
pinMode(c,OUTPUT);
pinMode(d,OUTPUT);
pinMode(e,OUTPUT);
pinMode(f,OUTPUT);
pinMode(g,OUTPUT);
pinMode(h,OUTPUT);
Serial.begin(9600); // begin the serial monitor
}

void loop() {
// put your main code here, to run repeatedly:
// ask the user for input
Serial.println(“——————————“);
Serial.println(“Please enter the Hexadecimal number(0-F) to print on 7 segement display” );
Serial.println(“>>>”);
while (Serial.available()==0){} // checking is there any input is available
number=Serial.readString(); // read the number from the serial monitor

// check the number given by user is single hexadecimal digit only
// if it is not the single digit number it will ask for another number
if (number.length()>2){
Serial.println(“Please enter the single hexadecimal number”);
valid=0; // declared that number is not valid to print
delay(3000);
}
else{
valid=1;
}

// if the number input is valid then this loop will work and display it
if (valid==1){
number.toUpperCase(); // convert the input number to uppercase
char val=number.charAt(0);// store the number into the char variable ‘val’
// check wether input number is valid hexadecimal number(0-F)
if ((val>47 && val<58)||(val>64 & val<71)){//comapre by ASCII values od 0-9 & A-F
Serial.print(“See the display number “);
Serial.print(val);
Serial.println(” is printed”);
}
else {
Serial.println(“Please enter a hexadecimal number(0-F) “);
delay(3000);
}
/*Note:-check your 7 segmnet display is common cathode or anode

  • In Common cathode write digitalWrite to HIGH to turn ON the segment
  • In Common anode write digitalWrite to LOW to turn ON the segment
    */
    // comapre the’val’ value with ASCII values and will print the number according to it…

//0
if (val==48)
{

digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,LOW);
}

//1
if (val==49)
{
digitalWrite(a,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}

//2
if (val==50)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,LOW);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
}

//3
if (val==51)
{digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
}

//4
if (val==52)
{
digitalWrite(a,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}

//5
if (val==53)
{
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}

//6
if (val==54)
{
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}

//7
if (val==55)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}

//8
if (val==56)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);

}

//9
if (val==57)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}

//A
if (val==65)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}

//B
if (val==66)
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}

//C
if (val==67)
{
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,LOW);
}

//D
if (val==68)
{
digitalWrite(a,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
}

//E
if (val==69)
{
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}

//F
if (val==70)
{
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}
delay(3000);
}
}

Now Upload the Code .

Open Serial Monitor .

 

Enter a number and check the output on Hexadecimal Display.

You can also find code on github at

https://github.com/pratech/FaceDetectionWith7SegmentDisplay

Next we will integrate this display with our Face Detection Code.

Go to your python code and change the Serial Port Number with the number that the NodeMCU is assigned to .

Run the python code and come near the camera , the 7 segment display should show the number of faces being detected on the frame.

If you don’t have NodeMCU module the same can be performed from Arduino Uno or Nano Board as well.

Follow below for Arduino Nano or Uno.

7 Segment Display LED interface with Arduino​

 

Arduino Connection with 7 segment diaplay.

Code:

/* Welcome to Pinwheel Robotics

  • Here is the program for 7 segment LED on which the hexadecimal number
  • will print as a user input through serial monitor.
  • For more information visit http://www.pwbotics.wordpress.com
    */

String number; // reads the number from the serial monitor
// declared the output pin of 7 segment with arduino
const int h=2; //DP pin
const int c=3;
const int d=4;
const int e=5;
const int g=6;
const int f=7;
const int a=8;
const int b=9;
int valid=1; // variable to check its valid umber given by the users or not

void setup() {
// put your setup code here, to run once:
// set the pins as OUTPUT
pinMode(a,OUTPUT);
pinMode(b,OUTPUT);
pinMode(c,OUTPUT);
pinMode(d,OUTPUT);
pinMode(e,OUTPUT);
pinMode(f,OUTPUT);
pinMode(g,OUTPUT);
pinMode(h,OUTPUT);
Serial.begin(9600); // begin the serial monitor

}

void loop() {
// put your main code here, to run repeatedly:
// ask the user for input
Serial.println(“——————————“);
Serial.println(“Please enter the Hexadecimal number(0-F) to print on 7 segement display” );
Serial.println(“>>>”);
while (Serial.available()==0){} // checking is there any input is available
number=Serial.readString(); // read the number from the serial monitor

// check the number given by user is single hexadecimal digit only
// if it is not the single digit number it will ask for another number
if (number.length()>2){
Serial.println(“Please enter the single hexadecimal number”);
valid=0; // declared that number is not valid to print
delay(3000);
}
else{
valid=1;
}

// if the number input is valid then this loop will work and display it
if (valid==1){
number.toUpperCase(); // convert the input number to uppercase
char val=number.charAt(0);// store the number into the char variable ‘val’
// check wether input number is valid hexadecimal number(0-F)
if ((val>47 && val<58)||(val>64 & val<71)){//comapre by ASCII values od 0-9 & A-F
Serial.print(“See the display number “);
Serial.print(val);
Serial.println(” is printed”);
}
else {
Serial.println(“Please enter a hexadecimal number(0-F) “);
delay(3000);
}
/*Note:-check your 7 segmnet display is common cathode or anode

  • In Common cathode write digitalWrite to HIGH to turn ON the segment
  • In Common anode write digitalWrite to LOW to turn ON the segment
    */
    // comapre the’val’ value with ASCII values and will print the number according to it…

//0
if (val==48)
{

digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,LOW);
}

//1
if (val==49)
{
digitalWrite(a,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}

//2
if (val==50)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,LOW);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
}

//3
if (val==51)
{digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
}

//4
if (val==52)
{
digitalWrite(a,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}

//5
if (val==53)
{
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}

//6
if (val==54)
{
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}

//7
if (val==55)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}

//8
if (val==56)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);

}

//9
if (val==57)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}

//A
if (val==65)
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}

//B
if (val==66)
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}

//C
if (val==67)
{
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,LOW);
}

//D
if (val==68)
{
digitalWrite(a,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
}

//E
if (val==69)
{
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}

//F
if (val==70)
{
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}
delay(3000);
}
}

Upload the code , open serial monitor and type a number and check 7 segment display.

Integrate the code with python code . Change the serial port number in python file with serial port number of Arduino Uno / Nano.

Start your face detection code and check the output.

Output​

 

 Number of face in frame is 1.

 

Number of face in frame is 2

One thought on “Face Detection using opencv – python on windows along with serial port control​

Add yours

Leave a comment

Start a Blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started