一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、学习软件
工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。
三、入门学习视频
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
# 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
完成代码
关闭循环后,我们会将 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()
### 最后
> **🍅 硬核资料**:关注即可领取PPT模板、简历模板、行业经典书籍PDF。
> **🍅 技术互助**:技术群大佬指点迷津,你的问题可能不是问题,求资源在群里喊一声。
> **🍅 面试题库**:由技术群里的小伙伴们共同投稿,热乎的大厂面试真题,持续更新中。
> **🍅 知识体系**:含编程语言、算法、大数据生态圈组件(Mysql、Hive、Spark、Flink)、数据仓库、Python、前端等等。
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化学习资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**