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

使用Python和OpenCV实现树莓派/PC实时摄像头数据共享,主要分为服务器和客户端两部分。

服务器Demo如下:

  1. #服务器端
  2. import socket
  3. import threading
  4. import struct
  5. import time
  6. import cv2
  7. import numpy
  8. class Carame_Accept_Object:
  9. def __init__(self,S_addr_port=("",8880)):
  10. self.resolution=(640,480) #分辨率
  11. self.img_fps=15 #每秒传输多少帧数
  12. self.addr_port=S_addr_port
  13. self.Set_Socket(self.addr_port)
  14. #设置套接字
  15. def Set_Socket(self,S_addr_port):
  16. self.server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  17. self.server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) #端口可复用
  18. self.server.bind(S_addr_port)
  19. self.server.listen(5)
  20. #print("the process work in the port:%d" % S_addr_port[1])
  21. def check_option(object,client):
  22. #按格式解码,确定帧数和分辨率
  23. info=struct.unpack('lhh',client.recv(8))
  24. if info[0]>888:
  25. object.img_fps=int(info[0])-888 #获取帧数
  26. object.resolution=list(object.resolution)
  27. # 获取分辨率
  28. object.resolution[0]=info[1]
  29. object.resolution[1]=info[2]
  30. object.resolution = tuple(object.resolution)
  31. return 1
  32. else:
  33. return 0
  34. def RT_Image(object,client,D_addr):
  35. if(check_option(object,client)==0):
  36. return
  37. camera=cv2.VideoCapture(0) #从摄像头中获取视频
  38. img_param=[int(cv2.IMWRITE_JPEG_QUALITY),object.img_fps] #设置传送图像格式、帧数
  39. while(1):
  40. time.sleep(0.1) #推迟线程运行0.1s
  41. _,object.img=camera.read() #读取视频每一帧
  42. object.img=cv2.resize(object.img,object.resolution)
  43. #按要求调整图像大小(resolution必须为元组)
  44. _,img_encode=cv2.imencode('.jpg',object.img,img_param) #按格式生成图片
  45. img_code=numpy.array(img_encode) #转换成矩阵
  46. object.img_data=img_code.tostring() #生成相应的字符串
  47. try:
  48. #按照相应的格式进行打包发送图片
  49. client.send(struct.pack("lhh",len(object.img_data),object.resolution[0],
  50. object.resolution[1])+object.img_data)
  51. except:
  52. camera.release() #释放资源
  53. return
  54. if __name__ == '__main__':
  55. camera=Carame_Accept_Object()
  56. while(1):
  57. client,D_addr=camera.server.accept()
  58. clientThread=threading.Thread(None,target=RT_Image,args=(camera,client,D_addr,))
  59. clientThread.start()

 客户端Demo如下:

  1. #客户端
  2. import socket
  3. import cv2
  4. import threading
  5. import struct
  6. import numpy
  7. class Camera_Connect_Object:
  8. def __init__(self,D_addr_port=["",8880]):
  9. self.resolution=[640,480]
  10. self.addr_port=D_addr_port
  11. self.src=888+15 #双方确定传输帧数,(888)为校验值
  12. self.interval=0 #图片播放时间间隔
  13. self.img_fps=100 #每秒传输多少帧数
  14. def Set_socket(self):
  15. self.client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  16. self.client.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
  17. def Socket_Connect(self):
  18. self.Set_socket()
  19. self.client.connect(self.addr_port)
  20. print("IP is %s:%d" % (self.addr_port[0],self.addr_port[1]))
  21. def RT_Image(self):
  22. #按照格式打包发送帧数和分辨率
  23. self.name=self.addr_port[0]+" Camera"
  24. self.client.send(struct.pack("lhh", self.src, self.resolution[0],
  25. self.resolution[1]))
  26. while(1):
  27. info=struct.unpack("lhh",self.client.recv(8))
  28. buf_size=info[0] #获取读的图片总长度
  29. if buf_size:
  30. try:
  31. self.buf=b"" #代表bytes类型
  32. temp_buf=self.buf
  33. while(buf_size): #读取每一张图片的长度
  34. temp_buf=self.client.recv(buf_size)
  35. buf_size-=len(temp_buf)
  36. self.buf+=temp_buf #获取图片
  37. data = numpy.fromstring(self.buf, dtype='uint8')
  38. #按uint8转换为图像矩阵
  39. self.image = cv2.imdecode(data, 1) #图像解码
  40. gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
  41. cv2.imshow(self.name, self.image) #展示图片
  42. except:
  43. pass;
  44. finally:
  45. if(cv2.waitKey(10)==27): #每10ms刷新一次图片,按‘ESC’(27)退出
  46. self.client.close()
  47. cv2.destroyAllWindows()
  48. break
  49. def Get_Data(self,interval):
  50. showThread=threading.Thread(target=self.RT_Image)
  51. showThread.start()
  52. if __name__ == '__main__':
  53. camera=Camera_Connect_Object()
  54. camera.addr_port[0]=input("Please input IP:")
  55. camera.addr_port=tuple(camera.addr_port)
  56. camera.Socket_Connect()
  57. camera.Get_Data(camera.interval)

运行效果还是比较流畅的,因为传输网络视频流,所以画质欠佳。

树莓派视频传输改进方式见:https://blog.csdn.net/m0_38106923/article/details/82628061 

树莓派视频传输最优方法:https://blog.csdn.net/m0_38106923/article/details/86562451

关注公众号,发送关键字:Java车牌识别,获取项目源码。