2024年最新事件分发面试题,Get超炫技能:如何使用 Python 执行运动检测?,一线互联网公司面试进阶全攻略

收集整理了一份《2024年最新Python全套学习资料》免费送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img



既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Python知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来

如果你需要这些资料,可以添加V无偿获取:hxbc188 (备注666)
img

正文

for cur in cont:

   if cv2.contourArea(cur) < 10000:  

       continue  

   var_motion = 1  

   (cur_x, cur_y,cur_w, cur_h) = cv2.boundingRect(cur)  

   

   # To create a rectangle of green color around the moving object 

   cv2.rectangle(cur_frame, (cur_x, cur_y), (cur_x + cur_w, cur_y + cur_h), (0, 255, 0), 3)  

from the frame adding the motion status

motionTrackList.append(var_motion)

motionTrackList = motionTrackList[-2:]

Adding the Start time of the motion

if motionTrackList[-1] == 1 and motionTrackList[-2] == 0:

   motionTime.append(datetime.now())  

Adding the End time of the motion

if motionTrackList[-1] == 0 and motionTrackList[-2] == 1:

   motionTime.append(datetime.now())  

In the gray scale displaying the captured image

cv2.imshow("The image captured in the Gray Frame is shown below: ", gray_frame)

To display the difference between inital static frame and the current frame

cv2.imshow("Difference between the inital static frame and the current frame: ", differ_frame)

To display on the frame screen the black and white images from the video

cv2.imshow("Threshold Frame created from the PC or Laptop Webcam is: ", thresh_frame)

Through the colour frame displaying the contour of the object

cv2.imshow(“From the PC or Laptop webcam, this is one example of the Colour Frame:”, cur_frame)

Creating a key to wait

wait_key = cv2.waitKey(1)

With the help of the ‘m’ key ending the whole process of our system

if wait_key == ord(‘m’):

   # adding the motion variable value to motiontime list when something is moving on the screen 

   if var_motion == 1:  

       motionTime.append(datetime.now())  

   break 

#### 完成代码


关闭循环后,我们会将 dataFrame 和 motionTime 列表中的数据添加到 CSV 文件中,最后关闭视频。



At last we are adding the time of motion or var_motion inside the data frame

for a in range(0, len(motionTime), 2):

dataFrame = dataFrame.append({“Initial” : time[a], “Final” : motionTime[a + 1]}, ignore_index = True)

To record all the movements, creating a CSV file

dataFrame.to_csv(“EachMovement.csv”)

Releasing the video

video.release()

Now, Closing or destroying all the open windows with the help of openCV

cv2.destroyAllWindows()


### **流程总结**


我们已经创建了代码;现在让我们再次简要讨论该过程。


首先,我们使用设备的网络摄像头拍摄视频,然后将输入视频的初始帧作为参考,并不时检查下一帧。如果找到与第一个不同的帧,则存在运动。这将在绿色矩形中标记。


### **组合代码**


我们已经在不同的部分看到了代码。现在,让我们结合起来:



Importing the Pandas libraries

import pandas as panda

Importing the OpenCV libraries

import cv2

Importing the time module

import time

Importing the datetime function of the datetime module

from datetime import datetime

Assigning our initial state in the form of variable initialState as None for initial frames

initialState = None

List of all the tracks when there is any detected of motion in the frames

motionTrackList= [ None, None ]

A new list ‘time’ for storing the time when movement detected

motionTime = []

Initialising DataFrame variable ‘dataFrame’ using pandas libraries panda with Initial and Final column

dataFrame = panda.DataFrame(columns = [“Initial”, “Final”])

starting the webCam to capture the video using cv2 module

video = cv2.VideoCapture(0)

using infinite loop to capture the frames from the video

while True:

Reading each image or frame from the video using read function

check, cur_frame = video.read()

Defining ‘motion’ variable equal to zero as initial frame

var_motion = 0

From colour images creating a gray frame

gray_image = cv2.cvtColor(cur_frame, cv2.COLOR_BGR2GRAY)

To find the changes creating a GaussianBlur from the gray scale image

gray_frame = cv2.GaussianBlur(gray_image, (21, 21), 0)

For the first iteration checking the condition

we will assign grayFrame to initalState if is none

if initialState is None:

   initialState = gray_frame  

   continue  

Calculation of difference between static or initial and gray frame we created

differ_frame = cv2.absdiff(initialState, gray_frame)

the change between static or initial background and current gray frame are highlighted

thresh_frame = cv2.threshold(differ_frame, 30, 255, cv2.THRESH_BINARY)[1]

thresh_frame = cv2.dilate(thresh_frame, None, iterations = 2)

For the moving object in the frame finding the coutours

cont,_ = cv2.findContours(thresh_frame.copy(),

                  cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  

for cur in cont:

   if cv2.contourArea(cur) < 10000:  

       continue  

   var_motion = 1  

   (cur_x, cur_y,cur_w, cur_h) = cv2.boundingRect(cur)  

   

   # To create a rectangle of green color around the moving object 

   cv2.rectangle(cur_frame, (cur_x, cur_y), (cur_x + cur_w, cur_y + cur_h), (0, 255, 0), 3)  

from the frame adding the motion status

motionTrackList.append(var_motion)

motionTrackList = motionTrackList[-2:]

Adding the Start time of the motion

if motionTrackList[-1] == 1 and motionTrackList[-2] == 0:

   motionTime.append(datetime.now())  

Adding the End time of the motion

if motionTrackList[-1] == 0 and motionTrackList[-2] == 1:

   motionTime.append(datetime.now())  

In the gray scale displaying the captured image

cv2.imshow("The image captured in the Gray Frame is shown below: ", gray_frame)

To display the difference between inital static frame and the current frame

cv2.imshow("Difference between the inital static frame and the current frame: ", differ_frame)

To display on the frame screen the black and white images from the video

cv2.imshow("Threshold Frame created from the PC or Laptop Webcam is: ", thresh_frame)

Through the colour frame displaying the contour of the object

cv2.imshow(“From the PC or Laptop webcam, this is one example of the Colour Frame:”, cur_frame)

Creating a key to wait

wait_key = cv2.waitKey(1)

With the help of the ‘m’ key ending the whole process of our system

if wait_key == ord(‘m’):

   # adding the motion variable value to motiontime list when something is moving on the screen 

   if var_motion == 1:  

       motionTime.append(datetime.now())  

   break 

At last we are adding the time of motion or var_motion inside the data frame

for a in range(0, len(motionTime), 2):

dataFrame = dataFrame.append({“Initial” : time[a], “Final” : motionTime[a + 1]}, ignore_index = True)

To record all the movements, creating a CSV file

dataFrame.to_csv(“EachMovement.csv”)

Releasing the video

video.release()

Now, Closing or destroying all the open windows with the help of openCV

cv2.destroyAllWindows()


### **结果**


运行上述代码后得出的结果将与下面看到的类似。


在这里,我们可以看到视频中男人的动作已经被追踪到了。因此,可以相应地看到输出。


然而,在这段代码中,跟踪将在移动对象周围的矩形框的帮助下完成,类似于下面可以看到的。这里要注意的一件有趣的事情是,该视频是一个安全摄像机镜头,已经对其进行了检测。


![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/4686f58f9065481eaa4e51e88a106c06.png)  
 1712805148911)


### **结论**


* Python 编程语言是一种开源库丰富的语言,可为用户提供许多应用程序。
* 当一个物体静止并且没有速度时,它被认为是静止的,而恰恰相反,当一个物体没有完全静止时,它被认为是运动的。
* OpenCV 是一个开源库,可以与许多编程语言一起使用,通过将它与 python 的 panda/NumPy 库集成,我们可以充分利用 OpenCV 的特性。
* 主要思想是每个视频只是许多称为帧的静态图像的组合,并且帧之间的差异用于检测.


![图片](https://img-blog.csdnimg.cn/img_convert/6ed5250ae55ea191d90e8e40646962cc.png)


## 关于Python学习指南


学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后给大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!


包括:Python激活码+安装包、Python web开发,Python爬虫,Python数据分析,人工智能、自动化办公等学习教程。带你从零基础系统性的学好Python!


#### 👉Python所有方向的学习路线👈


Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。**(全套教程文末领取)**


![在这里插入图片描述](https://img-blog.csdnimg.cn/3c4ee87941694f3789398db3d52a2637.png#pic_center)


#### 👉Python学习视频600合集👈


观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。


![在这里插入图片描述](https://img-blog.csdnimg.cn/64c89bf6293d4699bf7ee8f34b9e69fd.png#pic_center)


#### 温馨提示:篇幅有限,已打包文件夹,获取方式在:文末


#### 👉Python70个实战练手案例&源码👈


光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。


学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!



### 一、Python所有方向的学习路线



Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。



![](https://i-blog.csdnimg.cn/blog_migrate/0426b4c2e3d69852ceb846faa8ff365f.png)



### 二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。



![](https://img-blog.csdnimg.cn/img_convert/8c4513c1a906b72cbf93031e6781512b.png)



### 三、全套PDF电子书



书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。

![](https://i-blog.csdnimg.cn/blog_migrate/ad7909a4730b780656545eb9552a5320.png)



### 四、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。



![](https://img-blog.csdnimg.cn/afc935d834c5452090670f48eda180e0.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA56iL5bqP5aqb56eD56eD,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)



### 五、实战案例



光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。



![](https://i-blog.csdnimg.cn/blog_migrate/16e27ee7dfd6e25a4f43c8cc6ef06aa7.png)



### 六、面试资料

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。



![](https://i-blog.csdnimg.cn/blog_migrate/3443f3d934f11d6666b3767ac0897763.png)  

![](https://img-blog.csdnimg.cn/img_convert/d2d978bb523c810abca3abe69e09bc1a.png)




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**如果你需要这些资料,可以添加V无偿获取:hxbc188 (备注666)**
![img](https://img-blog.csdnimg.cn/img_convert/05d3e49b243fbfc5040f3db6dce1c559.png)

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。



![](https://i-blog.csdnimg.cn/blog_migrate/3443f3d934f11d6666b3767ac0897763.png)  

![](https://img-blog.csdnimg.cn/img_convert/d2d978bb523c810abca3abe69e09bc1a.png)




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**如果你需要这些资料,可以添加V无偿获取:hxbc188 (备注666)**
[外链图片转存中...(img-6w8SZ2Wa-1713855372083)]

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
  • 29
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值