【树莓派】安装OpenCV以及实时视频传输

功能基于opencvnumpyzmqbase64这几个库来实现,将摄像头采集到的画面实时传输到其它地方,用于显示图像或交给上位机进行机器视觉处理。

本教程中硬件主要用到

一台PC和一个安装有摄像头的树莓派

实验所用的代码如下:

RPiCam:

#!/usr/bin/env/python3

# File name   : server.py

# Description : for FPV video and OpenCV functions

# Website : www.adeept.com

# Author  : William(Based on Adrian Rosebrock's OpenCV code on pyimagesearch.com)

# Date: 2019/11/21



import cv2

import zmq

import base64

import picamera

from picamera.array import PiRGBArray



IP = '192.168.3.11'



camera = picamera.PiCamera()

camera.resolution = (640, 480)

camera.framerate = 20

rawCapture = PiRGBArray(camera, size=(640, 480))



context = zmq.Context()

footage_socket = context.socket(zmq.PAIR)

footage_socket.connect('tcp://%s:5555'%IP)

print(IP)



for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):

frame_image = frame.array

encoded, buffer = cv2.imencode('.jpg', frame_image)

jpg_as_text = base64.b64encode(buffer)

footage_socket.send(jpg_as_text)

rawCapture.truncate(0)



PC:

#!/usr/bin/python3

# File name   : PC.py

# Author  : William

# Date: 2019/12/23



import cv2

import zmq

import base64

import numpy as np



context = zmq.Context()

footage_socket = context.socket(zmq.PAIR)

footage_socket.bind('tcp://*:5555')



while True:

frame = footage_socket.recv_string()

img = base64.b64decode(frame)

npimg = np.frombuffer(img, dtype=np.uint8)

source = cv2.imdecode(npimg, 1)

cv2.imshow("Stream", source)

cv2.waitKey(1)
跳至工具栏