Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to control camera to record video

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to use Python to control camera recording video". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use Python to control camera recording video" can help you solve your doubts.

Effect:

To realize the function of photo reader with Python code, there are 7 steps:

Step 1: import the required dependent libraries.

There are three main libraries used here:

(1) tkinter library: used for interface making

(2) PIL library: for image processing

(3) cv2 library: for video capture

Impor tcv2impor ttkinterfrom PIL import Image, ImageTk

Step 2: define the form that controls the video recorded by the camera.

The main purpose is to define a form that controls the recording of video by the camera.

Root = tkinter. Tk () root. Title ("video camera") root. Geometry (800x500') root. Resi zable (2550, 2550)

Step 3: define and initialize the video display control.

It is mainly to define and initialize a display control box for video display.

1 abeltkinter. Label (root) label. Pack (padx=10, pady=10)

Step 4: define and initialize the form menu.

(1) define and initialize menu.

The main function is to define the open video submenu under the file menu, and then associate the control event with the open video submenu: turn on the camera video recording function.

Definit_ frame (root): menu-tkinter. Menu (root) submenul = tkinter. Menu (menu, tearoff=0) submenu1. Add_ command (1abelF 'fJFTtT, command=open_ vi deo) menu. Add_ cascade (labe1=' x f4', menuFsubmenul) submenu2= tkinter. Menu (menu, tearoff=O) submenu2. Add_ command (label=' # FHs4') menu. Addcascade (label-' # G Ej', menu- submenu2) root. Confi g (menuFmenu) root. Config (cursor= "arrow:") l

(2) define the function to enable camera video recording.

The main function is to turn on the camera and read the frame for writing and storage.

Def open_ video (): flag,image F camera. Read O if flag: video_ out_ save. Write (image) cv2. WaitKey (10) cv2image = cv2. CvtColor (image,cv2. COLOR_ BGR2RGBA) curr ent_ i mage Image. Fromarray (cv2image) imgtk = ImageTk. PhotoImage (image=current_ image) label. Imgtk = imgtk labe 1. Config (image= imgtk) root. After (1m OpenX _ video)

Step 5: define and initialize the camera.

Step 6: define and initialize the save path and storage write variables for video recorded by the camera.

Save_ path = r'c: Users Admini strator Desktop/video_ out_ save. Mp4video_ fourcc = cv2. VideoWriter_ fourcc (* 'XVID') 1video _ out_ save = cv2. VideoWriter (save_ path, video_ fourcc, 20.0, (640,480))

Step 7: run the form's main loop.

The above is to use Python code to achieve a method to control camera recording video, the following is a specific code for everyone to communicate!

Import cv2 import tkinter from PIL import Image, ImageTk def open_ video): flag, image = camera. Read) if flag: video_ out_ save. Write (image) cv2. WaitKey (10) cv2image = cv2. CvtColor (image, cv2. COLOR BGR2RGBA) current_ image = Image. Fromarray (cv2image) imgtk = ImageTk. PhotoImage (image=current_ image) 1abel. Ingtk = imgtk 1abel. Config (image=imgtk) root. After (1, open vildeo) def init frame (root): menu=tkinter. Menu (root) submenul = tkinter. Menu (menu, tearoff=O) submenul. Add_ command (1abelined 'fT7ZA#O', command=open vi deo) menu. Add cascade (1abe1'X 1', menuFsubmenul) submenu2 = tkinter. Menu (menu, tearoff-O) submenu2. Add_ command (1abe1rabbit'# FH4) menu. Add_ cascade (1abe1customers'# E} ", menu=submenu2) root. Config (menu-menu) root. Config (cursor- arrow) if_ name_ = = main root = tkinter. Tk 0 root. Title ("I") root. Geometry (800x500'root. Resizable (2550, 2550) 1abel = tkinter. Labe1 (root) 1abel. Pack (padx=10, pady=10) init_ frame (root) camera = cv2. VideoCapture (0) save_ path = r'C: Users Administrator Desktop/video_ out_ save. Mp4'video_ fourcc = cv2. VideoWriter fourcc (* 'XVID') video_ out save = cv2. VideoWriter (save path, video_ fourcH;XEroot. Mainloop 0 has read this article, "how to use Python to control camera recording video" article has been introduced, want to master the knowledge of this article still need to be hands-on practice to understand, if you want to know more related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report