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 | |
Particulars | Quantity |
7 Segment LED Display Common Cathode | 1 |
NodMCU CP2102 | 1 |
USB Cable for CP2102 | 1 |
breadboard 400 points | 1 |
Jumper wires (M-M) | 10 |
LED | 1 |
330E resistor | 1 |
To place order or for bulk enquiry , mail us at admin@pinwheel.in
Download Python 3.10.4
Disable Path Length Limit
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
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]
Find Python Install Directory from Path
Copy get-pip.py script in the Python Install directory
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
pip install matplotlib
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
- 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
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
Video capture from Camera
pip install pyserial
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
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
Now make connection as shown below.
Now put the below code into the IDE and Compile and Upload.
// 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 .
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
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