日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

树莓派/PC实现实时摄像头数据共享(Python—picamera)

發(fā)布時間:2023/11/27 生活经验 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 树莓派/PC实现实时摄像头数据共享(Python—picamera) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

上次實驗使用Python—OpenCV實現(xiàn),發(fā)現(xiàn)傳輸效果并不是很理想,接下來使用Python和picamera實現(xiàn)樹莓派/PC實時攝像頭數(shù)據(jù)共享,主要也可分為服務器和客戶端兩部分。

服務器(PC/樹莓派)Demo如下:

import numpy as np
import cv2
import socket
class VideoStreamingTest(object):def __init__(self, host, port):self.server_socket = socket.socket()self.server_socket.bind((host, port))self.server_socket.listen(0)self.connection, self.client_address = self.server_socket.accept()self.connection = self.connection.makefile('rb')self.host_name = socket.gethostname()self.host_ip = socket.gethostbyname(self.host_name)self.streaming()def streaming(self):try:print("Host: ", self.host_name + ' ' + self.host_ip)print("Connection from: ", self.client_address)print("Streaming...")print("Press 'q' to exit")# need bytes herestream_bytes = b' 'while True:stream_bytes += self.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'):breakfinally:self.connection.close()self.server_socket.close()if __name__ == '__main__':# host, porth, p = "192.168.0.4", 8000VideoStreamingTest(h, p)

客戶端(樹莓派)Demo如下:

import io
import socket
import struct
import time
import picamera
# create socket and bind host
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('192.168.2.104', 8000))
connection = client_socket.makefile('wb')try:with picamera.PiCamera() as camera:camera.resolution = (320, 240)      # pi camera resolutioncamera.framerate = 15               # 15 frames/sectime.sleep(2)                       # give 2 secs for camera to initilizestart = time.time()stream = io.BytesIO()# send jpeg format video streamfor 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:breakstream.seek(0)stream.truncate()connection.write(struct.pack('<L', 0))
finally:connection.close()client_socket.close()

無論是運行速度還是畫質(zhì)都是比較理想的。

樹莓派與PC視頻數(shù)據(jù)傳輸?最優(yōu)方法:https://blog.csdn.net/m0_38106923/article/details/86562451

總結

以上是生活随笔為你收集整理的树莓派/PC实现实时摄像头数据共享(Python—picamera)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。