Opencv reads camera and video data

  Hello everyone, I am the blogger of csdn: lqj_ myself

This is my personal blog homepage:

lqj_My blog_CSDN blog-WeChat applet, front-end, python field blogger lqj_ I am good at WeChat applet, front-end, python, etc. https://blog.csdn.net/lbcyllqj?spm=1011.2415 .3001.5343哔哩哔哩 Welcome to pay attention:Xiao Miao front end

Xiao Miao's front-end personal space_哔哩哔哩_bilibili

This article mainly talks about the field of artificial intelligence vision (opencv) of python . This article has successfully included the column of python artificial intelligence vision (opencv) from entry to actual combat:

https://blog.csdn.net/lbcyllqj/category_12200666.htmlicon-default.png?t=N176https://blog.csdn.net/lbcyllqj/category_12200666.html

video capture

·Videos are composed of pictures, and each frame of the video is a picture, usually 30 frames, which means that 30 pictures are displayed in one second;

cv2.VideoCapture can capture the camera and use numbers to represent different devices, such as: 0, 1;

·If it is a video file, you can specify the path;

open video file

vc = cv2.VideoCapture(xxx.mp4) #xxx.mp4 is your video directory to read

turn on the camera

vc = cv2.VideoCapture(0) #here 0 represents the camera of the machine, for example, if you use a notebook, then 0 represents the built-in camera of our notebook

First create and import cv library and create window

import cv2 #导入cv2库

cv2.namedWindow('video', cv2.WINDOW_NORMAL)  # 创建一个窗口名字为window
cv2.resizeWindow('video', 800, 600)  # 更改窗口的大小

Loop through each frame of the camera

We can use while to judge the loop

while True:
    #读取一帧数据,返回标记和这一帧数据,True表示读到了数据,False表示没读到数据
    ret,frame = cap.read()
    #可以根据ret做个判断
    if not ret:
        #没读到数据,直接退出
        break

Display Data

Note that the frame below is the picture set of each frame obtained from the MP4 we imported

    #显示数据
    cv2.imshow('video',frame)

Bind keyboard response events

    key = cv2.waitKey(10)
    if key & 0xFF == ord('q'):#键盘q键退出程序
        break

Release windows and resources

cap.release()
cv2.destroyAllWindows()

Notice

Bad try:

1.cap = cv2.VideoCapture(0) If there is only one camera, when we call a non-existent camera, such as: cv2.VideoCapture(1), the window will flash back

Explanation: Because we call camera 1, but this camera does not exist in our machine, so if we write the code according to the above process, it will be directly judged as False at runtime, and directly cap.release()cv2.destroyAllWindows() releases the window !

2. key = cv2.waitKey(10), () can only be an integer, if we play the delay event after a decimal, the package will report errors and other problems!

Complete learning code

import cv2 #导入cv2库

cv2.namedWindow('video', cv2.WINDOW_NORMAL)  # 创建一个窗口名字为window
cv2.resizeWindow('video', 800, 600)  # 更改窗口的大小

cap = cv2.VideoCapture(0)
while True:
    #读取一帧数据,返回标记和这一帧数据,True表示读到了数据,False表示没读到数据
    ret,frame = cap.read()
    #可以根据ret做个判断
    if not ret:
        #没读到数据,直接退出
        break
    #显示数据
    cv2.imshow('video',frame)
    key = cv2.waitKey(10)
    if key & 0xFF == ord('q'):
        break
#释放资源
cap.release()
cv2.destroyAllWindows()


Guess you like

Origin blog.csdn.net/lbcyllqj/article/details/129000738