📜  Python - 人脸检测和发送通知

📅  最后修改于: 2022-05-13 01:55:42.387000             🧑  作者: Mango

Python - 人脸检测和发送通知

如今, Python已成为最受欢迎的语言之一,也是开发人员最喜欢的编程语言。这种语言的简化语法和模式使这种语言出现在趋势列表中。

Python的最大优势是庞大的标准库集合,可用于以下用途:

  • 机器学习
  • GUI 应用程序(如 Kivy、Tkinter、PyQt 等)
  • Django 等网络框架(YouTube、Instagram、Dropbox 使用)
  • 图像处理(如 OpenCV、Pillow)
  • 网页抓取(如 Scrapy、BeautifulSoup、 Selenium)
  • 测试框架
  • 多媒体
  • 科学计算
  • 文本处理等等..

对于机器学习和 AI, Python语言是开发人员的首要任务,因为Python语言的预构建库(如 NumPy、Pandas、Pybrain 和 SciPy)有助于加快 AI 开发。

在本文中,使用Python实现了一个简单的方法如何检测人脸并在检测到后向用户发送通知。如果面部未被识别,则不会向所有者发送通知。

使用的技术:

  • OpenCV: OpenCV 是一个巨大的开源库,用于计算机视觉、机器学习和图像处理。 OpenCV 支持多种编程语言,如Python、C++、 Java等。它可以处理图像和视频以识别对象、面部甚至人类的笔迹。当它与各种库集成时,例如 Numpy,这是一个高度优化的数值运算库,然后你的武器库中的武器数量就会增加,即你可以在 Numpy 中进行的任何操作都可以与 OpenCV 结合。这个 OpenCV 教程将帮助你学习从基础到高级的图像处理,例如使用大量 Opencv 程序和项目对图像、视频进行操作。

Sinch :Sinch 用于在相机检测到任何面部时向用户发送消息。用户必须在 sinch 上创建一个帐户,然后他/她才能从他们那里获得“service_plan_id”和“token”。之后用户可以在代码中输入后者。发件人和收件人号码也需要相应更改。

短信令牌步骤:

  1. 在 Sinch 上创建一个新帐户(请参阅此链接)

点击注册 - 注册

2.单击消息和对话面板:

点击按摩和保护

3.单击主页窗口中的 SMS 选项:

点击短信

4.您将从那里获得您的代码。

我们正在使用 Harcascade Classifier 正面文件下载该文件并指定路径。

Clx-sdk-xms 1.0.0:它是 CLX Communications REST API(也称为 XMS)的Python SDK,用于发送和接收单个或批量 SMS 消息。它还支持计划发送,将您的常用收件人组织成组,并使用参数化为每个收件人自定义您的消息。 Sinch 使用 clx-sdk-xms 来创建 API。

Python3
# OpenCV program to detect face in real time
# import libraries of python OpenCV
# where its functionality resides
 
from cv2 import cv2
 
import clx.xms
import requests
# By creating an account in sinch sms You can get your code.
# code for sms starts here
#client is a object that carries your unique token.
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'
# code for sms ends here
# Face Recognition starts from here.
# load the required trained XML classifiers
#https://github.com/opencv/opencv/blob/master
#/data/haarcascades/haarcascade_frontalface_default.xml
# Trained XML classifiers describes some features of some
# object we want to detect a cascade function is trained
# from a lot of positive(faces) and negative(non-faces)
# images.
 
detector = cv2.CascadeClassifier(
    "path")
 
# capture frames from a camera
 
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
#We want to send sms only once not untill the face is there and for that we are
#initializing the counter
counter = 0
# loop runs if capturing has been initialized.
 
while True:
    # reads frames from a camera
 
    ret, img = cap.read()
 
    if ret:
        # convert to gray scale of each frames
 
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Detects faces of different sizes in the input image
 
        faces = detector.detectMultiScale(gray, 1.1, 4)
 
        for face in faces:
             
            x, y, w, h = face
            # if there is any face and counter is zero then only it will send notification to the sender
            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))
                #sms ends here
            # To draw a rectangle in a face
            cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
     
         
        cv2.imshow("Face", img)
        counter = 1
       # Wait for 'q' key to stop
 
    key = cv2.waitKey(1)
    if key == ord("q"):
        break
# Close the window
cap.release()
# De-allocate any associated memory usage
 
cv2.destroyAllWindows()


输出:

检测图像:

通知: