Videocapture Library makes Python video high-speed transfer program

Source: Internet
Author: User
1, the first is the video data [camera image] acquisition, usually can use VFW in VC or VB implementation, this library I use not good, so has been not how to use. Now we are using Python's videocapture library, this library is very simple to use, as follows:

Copy the Code code as follows:


From videocapture import Device
Cam = Device ()
Cam.setresolution (320,240) #设置显示分辨率
Cam.savesnapshot (' demo.jpg ') #抓取并保存图片

In this way, you get a picture.
Of course, to achieve a relatively high-speed acquisition, it is not possible to save the picture every time, so that each time to collect a picture is nearly 1 seconds, this speed we are unbearable.
A better solution is to do this directly: Im = Cam.getimage (), return an Image object, is a block of memory, and it is much quicker to operate on it.
2. How do I transfer pictures? I don't know how pplive this video is transmitted, my idea is simple, every time I send a picture.
In this program, the size of each transmitted RGB image is 160*120. In this way, the amount of data required is: D = 160*120*3 = 56.25 KB
I choose 80000B.
Here is the send-side code:

Copy the Code code 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 pictures in real time?
I use Pygame as the real-time image display interface, because Pygame is an optimized high-speed graphics library, do not know if there is no use of DirectShow, I think it should be used.
For Pygame please refer to www.pygame.org
Here is the receive-side code:

Copy the Code code 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 () #计算帧速
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.blit (Camshot, (0,0))
Pygame.display.update ()
Print Clock.get_fps () #在终端打印帧速
Clock.tick ()

The program finally finished, test the effect how to put, in order to facilitate, I have the client and the server is set to the local, the port is 1234.
Run the program, wow, it's unbelievable, the highest reached 230fps! (Right-hand terminal)

  • 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.