【智能小车】三、传输树莓派摄像头数据

1. picamera的使用
import cv2
import time
from picamera.array import PiRGBArray
from picamera import PiCamera

class Camera(object):
    def __init__(self):
        pass
    def Camera_Init(self):
        self.camera = PiCamera()
        self.framerate = 32
        self.camera = PiCamera()     #使用picamera模块中的PiCamera方法创建返回一个camera方法
        #设置参数
        self.camera.resolution = (640, 480)      #图像的长和宽
        self.camera.hflip = True     #水平翻转
        self.camera.vflip = True     #垂直翻转
        self.camera.rotation = 0     # 是否对图像进行旋转
        rawCapture = PiRGBArray(camera, self.camera.resolution)
        time.sleep(2)
        return self.camera,rawCapture
    def Camera_Cleanup(self):
        self.camera.close()
     
if __name__ == '__main__':
    try:
        video = Camera()
        camera, rawCapture = video.Camera_Init()
        for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
            image = frame.array
            cv2.imshow("Frame", image)
            key = cv2.waitKey(1)
            rawCapture.truncate(0)
            if key == ord("q"):
                video.Camera_Cleanup()
                break
    except KeyboardInterrupt:
        video.Camera_Cleanup()
2. 使用TCP传输摄像头数据
import socket
import time
import threading
import io
import struct
import picamera

def streaming(connection,client_address):
    while True:
         with picamera.PiCamera() as camera:
            camera.resolution = (640,480)
            camera.framerate = 15
            time.sleep(2)
            start = time.time()
            stream = io.BytesIO()

            for foo in camera.capture_continuous(stream, 'jpeg', use_video_port = True):
                connection.write(struct.pack('<L',stream.tell()))
                connection.flush()
                stream.seek(0)
                connection.write(stream.read())
                if time.time() - start > 600:
                    break
                stream.seek(0)
                stream.truncate()
            connection.write(struct.pack('<L',0))
   
    if __name__ == '__main__':
	    # host, port        server ip:host
	    video_port = 8000
	    server_socket = socket.socket()  # 创建套接字
	    server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,True)   #设置端口复用,使程序退出后端口马上释放
	    server_socket.bind(("", video_port))  # 绑定地址到套接字
	    server_socket.listen(2)  # 开始TCP监听
	    while True:
	        connection, client_address = server_socket.accept()
	        connection = connection.makefile('wb')
	        thd = threading.Thread(target = streaming, args = (connection,client_address))
	        thd.start()
3. pc读取摄像头数据
import numpy as np
import cv2
import socket
import io

# create socket and bind host
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('192.168.199.154', 8000))
connection = client_socket.makefile('wb')

try:
    stream_bytes = b' '
    while True:
        stream_bytes += connection.read(1024)  # 读接收到的数据
        first = stream_bytes.find(b'\xff\xd8')
        last = stream_bytes.find(b'\xff\xd9')  # 读完一帧
        if first != -1 and last != -1:
            jpg = stream_bytes[first:last + 2]
            stream_bytes = stream_bytes[last + 2:]
            image = cv2.imdecode(np.frombuffer(
                jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
            cv2.imshow('image', image)
            if cv2.waitKey(1) & 0xFF == ord('q'):  # 退出
                break
finally:
    connection.close()
    client_socket.close()

参考链接:AutoRCCar

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值