Python – Face detection and sending notification
Last Updated :
03 Jan, 2023
Nowadays python has become one of the most popular languages as well as favorite programming language among developers. The simplified syntax and pattern of this language make the presence of this language in the trending list.
The biggest strength of Python is a huge collection of standard library which can be used for the following:
- Machine Learning
- GUI Applications (like Kivy, Tkinter, PyQt etc. )
- Web frameworks like Django (used by YouTube, Instagram, Dropbox)
- Image processing (like OpenCV, Pillow)
- Web scraping (like Scrapy, BeautifulSoup, Selenium)
- Test frameworks
- Multimedia
- Scientific computing
- Text processing and many more..
For Machine learning and AI python language is the first priority for the developers because pre-built libraries of python language (like NumPy, Pandas, Pybrain, and SciPy) help expedite AI development.
In this article , A simple method is implemented using python how to detect human face and after detecting sends notifications to the user. If face is not recognized it does not send notifications to the owner.
Technologies used:
- OpenCV: OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a highly optimized library for numerical operations, then the number of weapons increases in your Arsenal i.e whatever operations one can do in Numpy can be combined with OpenCV.This OpenCV tutorial will help you learn the Image-processing from Basics to Advance, like operations on Images, Videos using a huge set of Opencv-programs and projects.
Sinch : Sinch is used to send messages to the user whenever the camera will detect any face. The user will have to make an account on sinch then he/she can get the ‘service_plan_id’ and ‘token’ from them. After which the user can input the latter in the code. The sender and recipients number also needs to be changed accordingly.
Steps to SMS Token:
- Create a new account on Sinch (Refer this link)
Click on Sign – Up
2. Click on messaging and conversations Panel :
Click on messaging and conservations
3. Click on SMS option on the Home window:
Click on sms
4.You will get your code from there.
We are using Harcascade Classifier front face file download that file and specify the path.
Clx-sdk-xms 1.0.0 : It is a Python SDK for the CLX Communications REST API (also called XMS) for sending and receiving single or batch SMS messages. It also supports scheduled sends, organizing your frequent recipients into groups, and customizing your message for each recipient using parameterization. Sinch uses clx-sdk-xms to create API’S.
Python3
from cv2 import cv2
import clx.xms
import requests
client = clx.xms.Client(service_plan_id = 'your_service id' ,
token = 'token_id' )
create = clx.xms.api.MtBatchTextSmsCreate()
create.sender = 'sender no.'
create.recipients = { 'recipients no.' }
create.body = 'This is a test message from your Sinch account'
detector = cv2.CascadeClassifier(
"path" )
cap = cv2.VideoCapture( 0 , cv2.CAP_DSHOW)
counter = 0
while True :
ret, img = cap.read()
if ret:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.1 , 4 )
for face in faces:
x, y, w, h = face
if (face. any () and counter = = 0 ):
try :
batch = client.create_batch(create)
except (requests.exceptions.RequestException, clx.xms.exceptions.ApiException) as ex:
print ( 'Failed to communicate with XMS: %s' % str (ex))
cv2.rectangle(img, (x, y), (x + w, y + h), ( 255 , 0 , 0 ), 2 )
cv2.imshow( "Face" , img)
counter = 1
key = cv2.waitKey( 1 )
if key = = ord ( "q" ):
break
cap.release()
cv2.destroyAllWindows()
|
Output:
Detecting image:
Notification:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...