我的项目使用 socketio
在Pythoni上发送接收数据时,添加 aiohttp
以帮助展示给网络
import asyncio
from aiohttp import web
sio = socketio.AsyncServer(async_mode='`aiohttp`')
app = web.Application()
sio.attach(app)
我遵循 https:/us-pycon-2019-tutorial.readthedocs.ioaiohttp_file_uploading.html。上传图片,但我不能上传视频。
def gen1():
# while True:
# if len(pm.list_image_display) > 1 :
image = cv2.imread("/home/duong/Pictures/Chess_Board.svg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# img = PIL.Image.new("RGB", (64, 64), color=(255,255,0))
image_pil = PIL.Image.fromarray(image)
fp = io.BytesIO()
image_pil.save(fp, format="JPEG")
content = fp.getvalue()
return content
async def send1():
print("11")
return web.Response(body=gen1(), content_type='image/jpeg')
谁能帮我在网上用 "aio http "显示视频?
谢谢
为了在aiohttp中流式传输视频,你可以打开一个StreamResponse来响应获取一个 img HTML节点。
@routes.get('/video')
async def video_feed(request):
response = web.StreamResponse()
response.content_type = 'multipart/x-mixed-replace; boundary=frame'
await response.prepare(request)
for frame in frames('/dev/video0'):
await response.write(frame)
return response
并以字节的形式发送你的帧。
def frames(path):
camera = cv2.VideoCapture(path)
if not camera.isOpened():
raise RuntimeError('Cannot open camera')
while True:
_, img = camera.read()
img = cv2.resize(img, (480, 320))
frame = cv2.imencode('.jpg', img)[1].tobytes()
yield b'--frame\r\nContent-Type: image/jpeg\r\n\r\n'+frame+b'\r\n'
然而这可能会对网络有要求,因为单独发送每个帧所需的比特率很高。对于实时流媒体的进一步压缩,你可能想使用WebRTC实现,如 aiortc.