概述
树莓派通过socket传输图片流的Python实现
socket设置
客户端
这里将树莓派当做socket的客户端。代码如下:
"""set ip address"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.1.1', 8000))
服务器端
服务器端socket设置如下:
host = '0.0.0.0'
port = 8000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 定义socket类型
s.bind((host, port)) # 绑定需要监听的Ip和端口号,tuple格式
s.listen(5)
host设置空表示接受所有ip的访问。
listen()值表示等待的资源池为5。
具体实现
客户端
# -*- coding: UTF-8 -*-
import socket, os, struct
import time
from picamera import PiCamera
"""set ip address"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.123.174', 8000))
"""set camera"""
camera = PiCamera()
camera.resolution = (1920,1080)
camera.framerate = 60
"""get opencv-classifier"""
face_cascade = cv2.CascadeClassifier('/home/pi/opencv-3.3.0/data/lbpcascades/lbpcascade_frontalface.xml' )
while True:
camera.capture('/home/pi/class/0.jpg')
filepath = '/home/pi/class/0.jpg'
image = cv2.imread(filepath)
gray = cv2.cvtColor( image, cv2.COLOR_BGR2GRAY )
faces = face_cascade.detectMultiScale( gray )
if os.path.isfile(filepath) and len(faces):
fileinfo_size = struct.calcsize('128sl') # 定义打包规则
# 定义文件头信息,包含文件名和文件大小
fhead = struct.pack('128sl', os.path.basename(filepath), os.stat(filepath).st_size)
s.send(fhead)
print('client filepath: ', os.path.basename(filepath), os.stat(filepath).st_size)
# with open(filepath,'rb') as fo: 这样发送文件有问题,发送完成后还会发一些东西过去
fo = open(filepath, 'rb')
while True:
filedata = fo.read(1024)
if not filedata:
break
s.send(filedata)
#time.sleep(0.5)
fo.close()
print('send over...')
#time.sleep(0.5)
# s.close()
服务器端实现
import socket, time, struct, os, threading
import sqlite3
host = '0.0.0.0'
port = 8000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 定义socket类型
s.bind((host, port)) # 绑定需要监听的Ip和端口号,tuple格式
s.listen(5)
def conn_thread(connection, address):
con = sqlite3.connect('test.db')
location = ''
i = 0
while True:
try:
connection.settimeout(600)
fileinfo_size = struct.calcsize('128sl')
buf = connection.recv(fileinfo_size)
#print(buf)
if buf: # 如果不加这个if,第一个文件传输完成后会自动走到下一句
filename, filesize = struct.unpack('128sl', buf)
print("照片大小:"+str(filesize))
if filesize< 0 or filesize > 2432075:
# da = connection.recv()
continue
filename = filename.decode().strip('