Videocapture library to create a python high-speed video transmission program

Source: Internet
Author: User

1. First of all, it is the collection of [camera images] of video data. Generally, we can use vfw to implement it in vc or vb. I am not using this library well, so I have never used it very much. now we use the python videocapture library, which is easy to use as follows:

Copy codeThe Code is as follows:
From VideoCapture import Device
Cam = Device ()
Cam. setResolution (320,240) # Set the display resolution
Cam.saveSnapshot('demo.jpg ') # capture and save the image

In this way, you get an image.
Of course, to achieve high-speed collection, it is unlikely that an image will be saved every time. In this way, every time an image is collected for nearly one second, this speed is intolerable.
The better solution is directly like this: im = cam. getImage (), which returns an Image object and a memory block, and the operations on it are much faster.
2. How do I transmit images? I don't know how a video like pplive is transmitted. My idea is simple. Each time I send an image.
In this program, the size of each transmitted RGB image is 160*120. In this way, the required data volume is: d = 160*120*3 = 56.25 kB
I chose 80000B.
The sending code is as follows:

Copy codeThe Code is as follows:
Import socket
Import Image
From VideoCapture import Device
Cam = Device ()
Cam. setResolution (320,240)
Clisocket = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)
While 1:
Im = cam. getImage ()
Im = im. resize (160,120 ))
Da = im. tostring ()
Clisocket. sendto (da, ("127.0.0.1", 1234 ))
S. close ()

3. How to display images in real time?
I used pygame as the real-time image display interface. Because pygame is an optimized high-speed graphics library, I don't know if directshow is used. I think it should be used ..
For more information about pygame, see www.pygame.org.
The following is the acceptor code:

Copy codeThe Code is as follows:
Import socket
Import Image
Import OS, sys, pygame
From pygame. locals import *

Pygame. init ()
Screen = pygame. display. set_mode (160,120 ))
Pygame. display. set_caption ("web cam ")

Pygame. display. flip ()
Svrsocket = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)
Svrsocket. bind ("127.0.0.1", 1234 ))
Clock = pygame. time. Clock () # Calculate the Frame Rate
While 1:
Data, address = svrsocket. recvfrom (80000)
Camshot = pygame. image. frombuffer (data, (160,120), "RGB ")
For event in pygame. event. get ():
If event. type = pygame. QUIT: sys. exit ()
Screen. Bits (camshot, (0, 0 ))
Pygame. display. update ()
Print clock. get_fps () # print the Frame Rate on the terminal
Clock. tick ()

The program is finally completed. How can we test the effect? For convenience, I set the client and server as the local machine and the port is 1234.
Run the program. Wow, it's incredible. It's up to 230fps! (Terminal on the right)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.