videocapture库制作python视频高速传输程序
1,首先是视频数据[摄像头图像]的采集,通常可以使用vfw在vc或者vb下实现,这个库我用的不好,所以一直不怎么会用.现在我们用到的是python的videocapture库,这个库用起来很简单,如下:
from VideoCapture import Device
cam = Device()
cam.setResolution(320,240) #设置显示分辨率
cam.saveSnapshot('demo.jpg') #抓取并保存图片
这样,你就得到了一张图片.
当然,要实现比较高速的采集,每次都要保存图片是不大可能的,这样弄下来每次采集一张图片已经接近1秒钟,这个速度我们是无法忍受的.
更好的解决方式是直接这样: im = cam.getImage(),返回的是一个Image对象,是一个内存块,对它操作就快速多了.
2,如何传输图片?我不知道pplive这种视频是怎么传输的,我的想法很简单,每次传送一幅图片.
在本程序中,每次传送的RGB图像的大小为 160*120.这样,需要的数据量为:d = 160*120*3 = 56.25 kB
我选择80000B.
下面是发送端代码:
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,如何实时显示图片?
我使用了pygame作为实时图像显示界面,因为pygame是经过优化的高速图形库,不知道有没有使用directshow,我想应该用了吧..
关于pygame请参考 www.pygame.org
下面是接收端代码:
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()
程序终于完成了,测试一下效果怎么样把,为了方便,我把客户端和服务端都设成了本机,端口为1234.
运行程序,哇,简直不可思议,最高竟然达到230fps!(右边的终端)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
