智能摄像头DIY教程

你要去度假,想看看家里的情况吗?你想了解人工智能和计算机视觉吗?你有 Raspberry Pi、网络摄像头和一些空闲时间吗?那么这个项目就是为你准备的!
在本文中,我们将介绍如何使用 Raspberry Pi 在 Python 中创建智能监控摄像头,成本低廉,而且不会失去对数据的控制。

这款摄像头将能够检测到网络摄像头视野范围内的任何人,并向您发送带有照片的电子邮件警报。

为了开展这个项目,我们将使用计算机视觉,这是人工智能的一个分支,它可以通过分析组成图像的像素来处理图像上的信息。

让我们从基础知识开始,了解人工智能 (AI) 是什么以及保护个人数据 (RGPD) 的重要性。

1、你提到人工智能,它是什么?

人工智能 (AI) 是一种模仿人类智能的计算机程序,可以执行通常需要人类智能的任务,例如语音识别、机器翻译、国际象棋或自动驾驶。

计算机视觉是人工智能的一个分支学科,专注于图像和视频的处理和分析。它的目的是让机器能够看到和理解视觉世界,就像人类用眼睛和大脑一样。

计算机视觉基于数学和统计技术,如今有许多实际应用,包括安全、医学、机器人技术、汽车工业、娱乐和商业。例如,计算机视觉可用于预测和帮助检测 X 射线或 MRI 上的病理,在复杂环境中引导机器人,识别道路上的路标或行人,为虚拟角色制作动画或识别货架上的产品。

2、GDPR:通用数据保护条例

那么如何保护你的面部和身份等个人数据呢?

为什么我们应该尊重 GDPR 和数据保护?

与任何技术一样,计算机视觉既有优势,也有风险。主要风险之一是尊重被摄像机拍摄或拍照的人的隐私和个人数据。图像和视频可能包含敏感信息,例如身份、面部表情、情绪、位置、活动等。……

《通用数据保护条例》(GDPR)是一部欧洲法律,旨在保护欧洲公民在处理个人数据方面的权利和自由。

《通用数据保护条例》要求数据控制者遵守合法性、公平性和透明度、目的限制、数据最小化、准确性和保留的原则。《通用数据保护条例》还赋予受数据处理影响的人权利,例如访问权、更正权、删除权、可移植性或反对权。

因此,在使用计算机视觉时,尤其是对于监控摄像机项目,尊重 GDPR 和数据所有权至关重要,这就是为什么开发自己的解决方案意味着您可以保留对整个数据生命周期的控制权:获取、处理、使用和存储。

3、所需的物料

  • Raspberry Pi:运行计算机视觉并执行所有任务
  • 网络摄像头:用于获取图像
  • [可选] Arduino KY-035 和 ADS1115:可用于检测门打开的磁传感器。它必须与 16 位模拟数字转换器 (ADC) 结合使用,才能将信息传输到 Raspberry Pi。

Raspberry Pi 将成为该机制的大脑。它将接收网络摄像头捕获的您家的图像,使用计算机视觉模型对其进行处理,然后发送电子邮件。

Raspberry Pi 使用 Python 编程语言,要使用 Tensorflow Lite 模型,您需要安装两个库:

opencv-python 是一个开源库,包含数百种计算机视觉算法。它允许您使用网络摄像头、处理图像并将其传输到模型。

为了使用这些库,需要运行以下命令来安装 Python 库:

pip install numpy imutils opencv-python
pip install ultralytics

4、Yolo

本教程的目的不是训练你自己的模型,而是测试一种使用已经训练有素且经过验证的模型(如 Yolo)即可轻松实现的方法。

Yolo 是一种用于检测图像中对象的算法。它是一种用于检测和分类图像中对象(如人、汽车或物体)的人工智能方法。为了实现这一点,它使用卷积神经网络,这是一种能够分析图像中的像素并从中提取有用信息的模型。

Yolo 不是多次查看图像来查找对象,而是将其分成小单元。每个单元都试图预测其周边的物体在哪里,以及它们属于哪个类别。它通过计算边界框(即围绕对象的矩形)的坐标和尺寸以及属于每个类别的概率来实现这一点。例如,一个单元可以说一个物体是人的概率为 80%,是自行车的概率为 20%。

示例:让我们想象一张有汽车和行人的街道图片。YOLO 会将此图像划分为网格,预测每辆汽车和行人周围的边界框,为每个框分配一个类别(汽车、行人),并消除冗余或不太可能的预测。最终结果是,图像中每个检测到的物体周围都有精确的边界框,这一切都是一次性完成的,因此得名“你只需看一次”。

在了解了 Yolo 算法的工作原理后,我们现在将研究如何使用 Python 和 Raspberry Pi 来创建智能监控摄像头。

此用例的目的是检测是否有人进入摄像头拍摄的区域,并发送带有照片的警报电子邮件。因此,我们将这种方法分为三个主要阶段:

  • 加载 Yolo 模型,这将使​​我们能够检测摄像头捕获的图像中的物体。
  • 从模型中进行推断,它将返回检测到的物体的边界框和类别,并检查其中是否有任何物体与人相对应。
  • 发送电子邮件,它将使用 smtplib 库将带有照片的消息发送到预定义的地址。

我们将在本文的其余部分更详细地介绍每个步骤。

5、代码实现

代码的第一部分将用于加载预先训练的 Yolo 模型。为此,我们首先选择Ultralytics仓库

然后将其本地下载到 RaspberryPi 上。Yolo 模型已初始化(程序启动时下载 .pt 文件)。

from ultralytics import YOLO

#load and save YOLO model
path_dir = "/home/florianberga/Desktop/camera/"
model = YOLO(path_dir+"yolov8n.pt"). #yolov5nu.pt is better for my case

#show all classes of the Yolo model
print(model.names)

现在,模型已经可用,是时候对其进行测试了。

5.1 Yolo 模型推理

计算机视觉模型推理涉及使用经过训练的模型分析图像或视频并检测物体、面部、动作或任何其他相关元素。推理将视觉数据转化为可操作的信息,然后可用于做出决策、自动化流程或改进服务。

# initialize the video capture object
vs = VideoStream(src=0, resolution=(700, 700)).start()

#define the accuracy min to validate the class identified
accuracy_min = 0.7 
 
#define the colour of the text and the bounding box on the picture
Green_RGB_color = (0, 255, 0)

i=0
while True:
    try : # if the camera stop to work

        #get the picture of the webcam
        frame = vs.read() 

         #press key Q to exit
        if cv2.waitKey(1) & 0xFF ==ord('q'):
            break
        
        i=i+1
        if i>=15 :# 1 image per second is enough
   
            # we run the model of YOLO on the picture
            detection = model(frame)[0]
            Label_total=""
            nb_person=0

            # loop on all the detections
            
            for box in detection.boxes:

                #we extract the accuracy with the detection
                data=box.data.tolist()[0]
                accuracy = data[4]
                #we extract the class label name
                label=model.names.get(box.cls.item())

              # filter out bad detections
                if float(accuracy) < accuracy_min :
                    continue
                else:
                    Label_total=Label_total+label+'_'
                    if label=="person":
                        #A person has been detected per Yolo
                        nb_person=nb_person+1
                    
                    
               # draw the bounding box on the picture
                xmin, ymin, xmax, ymax = int(data[0]), int(data[1]), int(data[2]), int(data[3])
                cv2.rectangle(frame, (xmin, ymin) , (xmax, ymax), Green_RGB_color, 2)
               #draw confidence and label
                y = ymin - 15 if ymin - 15 > 15 else ymin + 15
                cv2.putText(frame, "{} {:.1f}%".format(label,float(confidence*100)), (xmin, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, Green_RGB_color, 2)


            if nb_person>0:
                
                today = datetime.date.today()
                datetoday = today.strftime("%Y-%m-%d")
                now = datetime.datetime.now()
                current_time = now.strftime("%H:%M:%S")

                cv2.putText(frame, datetoday+" "+current_time, (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 1)
                
                #we would like to save the picture : Create Dir if they don't exist
                if os.path.exists(path_dir+'folder_imwrite/')==False:
                    os.mkdir(path_dir+'folder_imwrite/')

                if os.path.exists(path_dir+'folder_imwrite/'+str(datetoday))==False:
                    os.mkdir(path_dir+'folder_imwrite/'+str(datetoday))
                #save
                cv2.imwrite(path_dir+'folder_imwrite/'+str(datetoday)+'/'+current_time+'__'+str(nb_person)+"-pers__"+str(j)+'.jpg', frame)


                # we would like to show the frame to our screen
                #cv2.imshow("Frame", frame)




    except :
        print("Error camera")


#vs.release()
vs.stop()
cv2.destroyAllWindows()

一旦模型识别出图像中的某个人,它就会进入下一阶段:发送电子邮件让人们知道有人在那里。

5.2 发送邮件

在了解如何使用 Python 发送邮件之前,这里有一些提示和先决条件:

就我而言,我创建了一个独特的 Gmail 帐户:camera.xxxx@gmail.com。应用密码是一个 16 位密码,它允许不太安全的应用或设备访问你的 Google 帐户。应用密码只能用于已启用两步验证的帐户。要了解如何恢复密码请访问这里


def envoie_mail(nb_people_max, table_photo):

    email_subject = "Home XXX - Detection someone at home"

    sender_email_address = "Camera.xxxx@gmail.com"
    receiver_email_address = "xxx.xxxx@gmail.com"
    email_smtp = "smtp.gmail.com"
    email_password = "xxxx xxxx xxxx xxxx" #App Password : https://support.google.com/mail/answer/185833?hl=en
  
# create an email message object
    message = EmailMessage()
  
# configure email headers
    message['Subject'] = email_subject
    message['From'] = sender_email_address
    message['To'] = receiver_email_address
  
# set email body text
    message.set_content(str(nb_people_max)+" people detected at home")
    i_image=0
    while i_image<len(table_photo):
        image_data=""
  # open image as a binary file and read the contents
        with open(table_photo[i_image], 'rb') as file:
            image_data = file.read()
  # attach image to email
  # message.add_attachment(image_data, maintype='image', subtype=imghdr.what(None, image_data))
        message.add_attachment(image_data, maintype='image', subtype="jpeg")
        i_image=i_image+1


# set smtp server and port
    server = smtplib.SMTP(email_smtp, '587')
# identify this client to the SMTP server
    server.ehlo()
# secure the SMTP connection
    server.starttls()
  
# login to email account
    server.login(sender_email_address, email_password)
# send email
    server.send_message(message)
# close connection to server
    server.quit()

main.py 的完整代码:

# -*- coding: utf-8 -*-

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import threading

import datetime
from ultralytics import YOLO
import cv2
from imutils.video import VideoStream
import screeninfo
import os
import smtplib
from email.message import EmailMessage


#load and save YOLO model
path_dir = "/home/florianberga/Desktop/camera/"
model = YOLO(path_dir+"yolov8n.pt") #yolov5nu.pt is better for my case


accuracy_min = 0.7
Green_RGB_color = (0, 255, 0)



def envoie_mail(nb_people_max, table_photo):

    email_subject = "Home XXX - Detecting someone at home"

    sender_email_address = "Camera.xxxx@gmail.com"
    receiver_email_address = "xxx.xxxx@gmail.com"
    email_smtp = "smtp.gmail.com"
    email_password = "xxxx xxxx xxxx xxxx" #App Passworld
  
# create an email message object
    message = EmailMessage()
  
# configure email headers
    message['Subject'] = email_subject
    message['From'] = sender_email_address
    message['To'] = receiver_email_address
  
# set email body text
    message.set_content(str(nb_people_max)+" people detected at home")
    i_image=0
    while i_image<len(table_photo):
        image_data=""
  # open image as a binary file and read the contents
        with open(table_photo[i_image], 'rb') as file:
            image_data = file.read()
  # attach image to email
  # message.add_attachment(image_data, maintype='image', subtype=imghdr.what(None, image_data))
        message.add_attachment(image_data, maintype='image', subtype="jpeg")
        i_image=i_image+1

# set smtp server and port
    server = smtplib.SMTP(email_smtp, '587')
# identify this client to the SMTP server
    server.ehlo()
# secure the SMTP connection
    server.starttls()
  
# login to email account
    server.login(sender_email_address, email_password)
# send email
    server.send_message(message)
# close connection to server
    server.quit()




# initialize the video capture object
vs = VideoStream(src=0, resolution=(700, 700)).start()

   
i=0
j=0
nb_image_mail=0
nb_personne_max=0
table_path_image=[]

while True:
    try : # if the camera stop to work

        #get the picture of the webcam
        frame = vs.read() 

         #press key Q to exit
        if cv2.waitKey(1) & 0xFF ==ord('q'):
            break
        
        i=i+1
        if i>=15 :# 1 image per second is enough
   
            # we run the model of YOLO on the picture
            detection = model(frame)[0]
            Label_total=""
            nb_person=0

            # loop on all the detections
            
            for box in detection.boxes:

                #we extract the accuracy with the detection
                data=box.data.tolist()[0]
                accuracy = data[4]
                #we extract the class label name
                label=model.names.get(box.cls.item())

              # filter out bad detections
                if float(accuracy) < accuracy_min :
                    continue
                else:
                    Label_total=Label_total+label+'_'
                    if label=="person":
                        #A person has been detected per Yolo
                        nb_person=nb_person+1
                    
                    
               # draw the bounding box on the picture
                xmin, ymin, xmax, ymax = int(data[0]), int(data[1]), int(data[2]), int(data[3])
                cv2.rectangle(frame, (xmin, ymin) , (xmax, ymax), Green_RGB_color, 2)
               #draw confidence and label
                y = ymin - 15 if ymin - 15 > 15 else ymin + 15
                cv2.putText(frame, "{} {:.1f}%".format(label,float(confidence*100)), (xmin, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, Green_RGB_color, 2)
  
            if nb_person>0:
                
                today = datetime.date.today()
                datetoday = today.strftime("%Y-%m-%d")
                now = datetime.datetime.now()
                current_time = now.strftime("%H:%M:%S")

                cv2.putText(frame, datetoday+" "+current_time, (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 1)
                
                #we would like to save the picture : Create Dir if they don't exist
                if os.path.exists(path_dir+'folder_imwrite/')==False:
                    os.mkdir(path_dir+'folder_imwrite/')

                if os.path.exists(path_dir+'folder_imwrite/'+str(datetoday))==False:
                    os.mkdir(path_dir+'folder_imwrite/'+str(datetoday))
                #save
                cv2.imwrite(path_dir+'folder_imwrite/'+str(datetoday)+'/'+current_time+'__'+str(nb_person)+"-pers__"+str(j)+'.jpg', frame)
                # we would like to show the frame to our screen
                #cv2.imshow("Frame", frame)

                bilan_day_nbpersonnes.append(str(nb_person))
                bilan_heure_presence.append(str(current_time))
                bilan_day_presence.append(str(datetoday))
   
                if nb_personne_max<nb_person:
                    nb_personne_max=nb_person
   
                if j>1800: #noboady during 1h (1800)
                    table_path_image.append(path_dir+'folder_imwrite/'+str(datetoday)+'/'+current_time+'__'+str(nb_person)+"-pers__"+str(j)+'.jpg')

    #We can send the email to inform there is someone
    # We would like to collect other pictures before to send the mail : to catch other pictures : 3
                    if nb_image_mail>=3:
     #we send the mail
                        envoie_mail(nb_personne_max, table_path_image)
                        j=0
                        nb_image_mail=0
                        nb_personne_max=0
                        table_path_image.clear()
                    else:
                        nb_image_mail=nb_image_mail+1
            else:
                today = datetime.date.today()
                datetoday = today.strftime("%Y-%m-%d")
                now = datetime.datetime.now()
                current_time = now.strftime("%H:%M:%S")

            j=j+1
            i=0

    except :
        print("Error camera")


#video_cap.release()
vs.stop()
cv2.destroyAllWindows()

5.3 在 Raspberry Pi 开启时启动该机制

如果你想在开启 Raspberry Pi 时运行代码,只需修改设置“ crontab -e ”:

你可以使用像 nano 这样的控制台模式编辑器。只需添加以下带有 @reboot 选项的行:

@reboot python3 /home/florianberga/Desktop/camera/main.py &

需要注意以下两点:

  • 使用绝对路径
  • 在行尾添加 & 以在单独的进程中启动命令。如果你的软件没有快速放弃,这可以避免阻止 Raspberry Pi 的启动。

6、结束语

我希望这对你在自己的项目中使用计算机视觉有所帮助和启发。当然,这只是一个演示,可以通过面部识别和其他训练模型以及其他功能轻松改进……


原文链接:Yolo & Raspberry Pi : How to create a smart camera

BimAnt翻译整理,转载请标明出处