百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

智能摄像头DIY教程

off999 2024-12-13 15:37 41 浏览 0 评论

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

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

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

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

NSDT工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器 - REVIT导出3D模型插件 - 3D模型语义搜索引擎 - AI模型在线查看 - Three.js虚拟轴心开发包 - 3D模型在线减面 - STL模型在线切割

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、结束语

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


原文链接:智能摄像头DIY教程 - BimAnt

相关推荐

安全教育登录入口平台(安全教育登录入口平台官网)

122交通安全教育怎么登录:122交通网的注册方法是首先登录网址http://www.122.cn/,接着打开网页后,点击右上角的“个人登录”;其次进入邮箱注册,然后进入到注册页面,输入相关信息即可完...

大鱼吃小鱼经典版(大鱼吃小鱼经典版(经典版)官方版)

大鱼吃小鱼小鱼吃虾是于谦跟郭麒麟的《我的棒儿呢?》郭德纲说于思洋郭麒麟作诗的相声,最后郭麒麟做了一首,师傅躺在师母身上大鱼吃小鱼小鱼吃虾虾吃水水落石出师傅压师娘师娘压床床压地地动山摇。...

谷歌地球下载高清卫星地图(谷歌地球地图下载器)
  • 谷歌地球下载高清卫星地图(谷歌地球地图下载器)
  • 谷歌地球下载高清卫星地图(谷歌地球地图下载器)
  • 谷歌地球下载高清卫星地图(谷歌地球地图下载器)
  • 谷歌地球下载高清卫星地图(谷歌地球地图下载器)
哪个软件可以免费pdf转ppt(免费的pdf转ppt软件哪个好)
哪个软件可以免费pdf转ppt(免费的pdf转ppt软件哪个好)

要想将ppt免费转换为pdf的话,我们建议大家可以下一个那个wps,如果你是会员的话,可以注册为会员,这样的话,在wps里面的话,就可以免费将ppt呢转换为pdfpdf之后呢,我们就可以直接使用,不需要去直接不需要去另外保存,为什么格式转...

2026-02-04 09:03 off999

电信宽带测速官网入口(电信宽带测速官网入口app)

这个网站看看http://www.swok.cn/pcindex.jsp1.登录中国电信网上营业厅,宽带光纤,贴心服务,宽带测速2.下载第三方软件,如360等。进行在线测速进行宽带测速时,尽...

植物大战僵尸95版手机下载(植物大战僵尸95 版下载)

1可以在应用商店或者游戏平台上下载植物大战僵尸95版手机游戏。2下载教程:打开应用商店或者游戏平台,搜索“植物大战僵尸95版”,找到游戏后点击下载按钮,等待下载完成即可安装并开始游戏。3注意:确...

免费下载ppt成品的网站(ppt成品免费下载的网站有哪些)

1、Chuangkit(chuangkit.com)直达地址:chuangkit.com2、Woodo幻灯片(woodo.cn)直达链接:woodo.cn3、OfficePlus(officeplu...

2025世界杯赛程表(2025世界杯在哪个国家)

2022年卡塔尔世界杯赛程公布,全部比赛在卡塔尔境内8座球场举行,2022年,决赛阶段球队全部确定。揭幕战于当地时间11月20日19时进行,由东道主卡塔尔对阵厄瓜多尔,决赛于当地时间12月18日...

下载搜狐视频电视剧(搜狐电视剧下载安装)

搜狐视频APP下载好的视频想要导出到手机相册里方法如下1、打开手机搜狐视频软件,进入搜狐视频后我们点击右上角的“查找”,找到自已喜欢的视频。2、在“浏览器页面搜索”窗口中,输入要下载的视频的名称,然后...

pubg免费下载入口(pubg下载入口官方正版)
  • pubg免费下载入口(pubg下载入口官方正版)
  • pubg免费下载入口(pubg下载入口官方正版)
  • pubg免费下载入口(pubg下载入口官方正版)
  • pubg免费下载入口(pubg下载入口官方正版)
永久免费听歌网站(丫丫音乐网)

可以到《我爱音乐网》《好听音乐网》《一听音乐网》《YYMP3音乐网》还可以到《九天音乐网》永久免费听歌软件有酷狗音乐和天猫精灵,以前要跳舞经常要下载舞曲,我从QQ上找不到舞曲下载就从酷狗音乐上找,大多...

音乐格式转换mp3软件(音乐格式转换器免费版)

有两种方法:方法一在手机上操作:1、进入手机中的文件管理。2、在其中选择“音乐”,将显示出手机中的全部音乐。3、点击“全选”,选中所有音乐文件。4、点击屏幕右下方的省略号图标,在弹出菜单中选择“...

电子书txt下载(免费的最全的小说阅读器)

1.Z-library里面收录了近千万本电子书籍,需求量大。2.苦瓜书盘没有广告,不需要账号注册,使用起来非常简单,直接搜索预览下载即可。3.鸠摩搜书整体风格简洁清晰,书籍资源丰富。4.亚马逊图书书籍...

最好免费观看高清电影(播放免费的最好看的电影)

在目前的网上选择中,IMDb(互联网电影数据库)被认为是最全的电影网站之一。这个网站提供了各种类型的电影和电视节目的海量信息,包括剧情介绍、演员表、评价、评论等。其还提供了有关电影制作背后的详细信息,...

孤单枪手2简体中文版(孤单枪手2简体中文版官方下载)

要将《孤胆枪手2》游戏的征兵秘籍切换为中文,您可以按照以下步骤进行操作:首先,打开游戏设置选项,通常可以在游戏主菜单或游戏内部找到。然后,寻找语言选项或界面选项,点击进入。在语言选项中,选择中文作为游...

取消回复欢迎 发表评论: