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

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(',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(',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

你可能感兴趣的:(【智能小车】三、传输树莓派摄像头数据)