1, the first is the video data [camera image] of the collection, usually can be used VFW VC or VB under the implementation, this library I use is not good, so has been not how to use. Now we're using the Python videocapture Library, which is simple to use, as follows:
Copy Code code as follows:
From videocapture import Device
Cam = Device ()
Cam.setresolution (320,240) #设置显示分辨率
Cam.savesnapshot (' demo.jpg ') #抓取并保存图片
That way, you get a picture.
Of course, to achieve relatively high speed collection, every time to save pictures is not possible, so get down every time to collect a picture has been nearly 1 seconds, this speed we can not tolerate.
A better solution is to do this directly: Im = Cam.getimage (), which returns an Image object, is a block of memory, and it operates much faster.
2. How do I transfer pictures? I don't know how pplive this video is transmitted, my idea is very simple, send a picture each time.
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 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 the picture in real time?
I used Pygame as a 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 Code code as follows:
Import socket
Import Image
Import os,sys,pygame
from Pygame.loca LS 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 completed, test the effect of how to put, in order to facilitate, I put the client and server are set to the local, port is 1234.
Run the program, Wow, it's incredible, the highest reaches 230fps! (Terminal on the right)