ESP32-cam网络开发(3)------传输图像数据(micropython)

一 : 服务器接收图片并显示

esp32客户端:

目录

一 : 服务器接收图片并显示

 二 : ESP32 安卓APP显示

 三 : 使用服务器中转图像数据


import socket
import network
import camera
import time

#连接wifi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
    print('connecting to network...')
    wlan.connect('family', 'family102308')
    
    while not wlan.isconnected():
        pass
print('网络配置:', wlan.ifconfig())


#摄像头初始化
try:
    camera.init(0, format=camera.JPEG)
except Exception as e:
    camera.deinit()
    camera.init(0, format=camera.JPEG)


#图像设置----------------------------  
  
## Other settings:
# flip up side down
camera.flip(1)
# left / right
camera.mirror(1)

# framesize
camera.framesize(camera.FRAME_HVGA)
# The options are the following:
# FRAME_96X96 FRAME_QQVGA FRAME_QCIF FRAME_HQVGA FRAME_240X240
# FRAME_QVGA FRAME_CIF FRAME_HVGA FRAME_VGA FRAME_SVGA
# FRAME_XGA FRAME_HD FRAME_SXGA FRAME_UXGA FRAME_FHD
# FRAME_P_HD FRAME_P_3MP FRAME_QXGA FRAME_QHD FRAME_WQXGA
# FRAME_P_FHD FRAME_QSXGA
# Check this link for more information: https://bit.ly/2YOzizz

# special effects
camera.speffect(camera.EFFECT_NONE)
# The options are the following:
# EFFECT_NONE (default) EFFECT_NEG EFFECT_BW EFFECT_RED EFFECT_GREEN EFFECT_BLUE EFFECT_RETRO

# white balance
camera.whitebalance(camera.WB_NONE)
# The options are the following:
# WB_NONE (default) WB_SUNNY WB_CLOUDY WB_OFFICE WB_HOME

# saturation
camera.saturation(0)
# -2,2 (default 0). -2 grayscale 

# brightness
camera.brightness(0)
# -2,2 (default 0). 2 brightness

# contrast
camera.contrast(0)
#-2,2 (default 0). 2 highcontrast

# quality
camera.quality(10)
# 10-63 lower number means higher quality

#图像设置----------------------------   


#socket UDP 的创建
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)

while True:
    buf = camera.capture() # 获取图像数据
    s.sendto(buf,("192.168.0.105",9090)) # 向服务器发送图像数据
    time.sleep(0.1)





服务端代码:

import socket
import cv2
import io
from PIL import Image
import numpy as np

s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)
s.bind(("192.168.0.105",9090))
while True:
    data,IP = s.recvfrom(100000)
    bytes_stream = io.BytesIO(data)
    image = Image.open(bytes_stream)
    img = np.asarray(image)
    cv2.imshow("12", img)
    if cv2.waitKey(1)==ord("q"):
        break


效果:

 二 : ESP32 安卓APP显示

应用 : 只有一个场景 , 有多人同时监控或一人进行监控

原理 : ESP32-cam采集到的图像数据直接发送到安卓端 , 此时esp32为 "服务端",此方式可以实现"一对多传输 与 多对一"

ESP32-cam网络开发(3)------传输图像数据(micropython)_第1张图片

 ESP32服务端代码

import socket
import network
import camera
import time

#连接wifi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
    print('connecting to network...')
    wlan.connect('family', 'family102308')
    
    while not wlan.isconnected():
        pass
print('网络配置:', wlan.ifconfig())


#摄像头初始化
try:
    camera.init(0, format=camera.JPEG)
except Exception as e:
    camera.deinit()
    camera.init(0, format=camera.JPEG)
    
#摄像头配置----------------------------------------------
## Other settings:
# flip up side down
camera.flip(1)
# left / right
camera.mirror(1)

# framesize
camera.framesize(camera.FRAME_HVGA)
# The options are the following:
# FRAME_96X96 FRAME_QQVGA FRAME_QCIF FRAME_HQVGA FRAME_240X240
# FRAME_QVGA FRAME_CIF FRAME_HVGA FRAME_VGA FRAME_SVGA
# FRAME_XGA FRAME_HD FRAME_SXGA FRAME_UXGA FRAME_FHD
# FRAME_P_HD FRAME_P_3MP FRAME_QXGA FRAME_QHD FRAME_WQXGA
# FRAME_P_FHD FRAME_QSXGA
# Check this link for more information: https://bit.ly/2YOzizz

# special effects
camera.speffect(camera.EFFECT_NONE)
# The options are the following:
# EFFECT_NONE (default) EFFECT_NEG EFFECT_BW EFFECT_RED EFFECT_GREEN EFFECT_BLUE EFFECT_RETRO

# white balance
camera.whitebalance(camera.WB_NONE)
# The options are the following:
# WB_NONE (default) WB_SUNNY WB_CLOUDY WB_OFFICE WB_HOME

# saturation
camera.saturation(0)
# -2,2 (default 0). -2 grayscale 

# brightness
camera.brightness(0)
# -2,2 (default 0). 2 brightness

# contrast
camera.contrast(0)
#-2,2 (default 0). 2 highcontrast

# quality
camera.quality(10)
# 10-63 lower number means higher quality
#摄像头配置----------------------------------------------

#socket UDP 的创建
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)
s.bind((wlan.ifconfig()[0],9090))# 连接wifi后将获取到的ip地址进行绑定
data,IP = s.recvfrom(1024) #接收客户端消息和IP地址

while True:
    buf = camera.capture() # 获取图像数据
    s.sendto(buf,IP) # 向客户端发送图像数据
    time.sleep(0.1)
    




 APP采用E4A来开发:

基本界面

ESP32-cam网络开发(3)------传输图像数据(micropython)_第2张图片

 代码段:

可以看到 代码量非常的少

ESP32-cam网络开发(3)------传输图像数据(micropython)_第3张图片

 效果:

 三 : 使用服务器中转图像数据

应用 : 多个场景需要同时进行监控 , 多人同时查看

原理 :  esp32获取图像数据后首先发送到服务器上 , 然后由服务器转发到对应的APP上 . 此方法可实现 "多对多"的数据传输.  

 esp32代码 : 

在发送数据的同时要将自身的 "标识符"一并发到服务器中 , 以便服务器区分那个是esp32那个是APP 

import socket
import network
import camera
import time

#连接wifi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
    print('connecting to network...')
    wlan.connect('family', 'family102308')
    
    while not wlan.isconnected():
        pass
print('网络配置:', wlan.ifconfig())


#摄像头初始化
try:
    camera.init(0, format=camera.JPEG)
except Exception as e:
    camera.deinit()
    camera.init(0, format=camera.JPEG)
    
#摄像头配置----------------------------------------------
## Other settings:
# flip up side down
camera.flip(1)
# left / right
camera.mirror(1)

# framesize
camera.framesize(camera.FRAME_HVGA)
# The options are the following:
# FRAME_96X96 FRAME_QQVGA FRAME_QCIF FRAME_HQVGA FRAME_240X240
# FRAME_QVGA FRAME_CIF FRAME_HVGA FRAME_VGA FRAME_SVGA
# FRAME_XGA FRAME_HD FRAME_SXGA FRAME_UXGA FRAME_FHD
# FRAME_P_HD FRAME_P_3MP FRAME_QXGA FRAME_QHD FRAME_WQXGA
# FRAME_P_FHD FRAME_QSXGA
# Check this link for more information: https://bit.ly/2YOzizz

# special effects
camera.speffect(camera.EFFECT_NONE)
# The options are the following:
# EFFECT_NONE (default) EFFECT_NEG EFFECT_BW EFFECT_RED EFFECT_GREEN EFFECT_BLUE EFFECT_RETRO

# white balance
camera.whitebalance(camera.WB_NONE)
# The options are the following:
# WB_NONE (default) WB_SUNNY WB_CLOUDY WB_OFFICE WB_HOME

# saturation
camera.saturation(0)
# -2,2 (default 0). -2 grayscale 

# brightness
camera.brightness(0)
# -2,2 (default 0). 2 brightness

# contrast
camera.contrast(0)
#-2,2 (default 0). 2 highcontrast

# quality
camera.quality(10)
# 10-63 lower number means higher quality
#摄像头配置----------------------------------------------

#socket UDP 的创建
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)
IP = ("192.168.0.105",9090)

while True:
    buf = camera.capture() # 获取图像数据
    
    s.sendto(b"esp32"+buf,IP) # 向特定的服务器发送图像数据 "esp32"为标识符 , 方便服务器区分那个是esp32那个是APP
    time.sleep(0.1)
    




服务器代码:

import socket
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)
s.bind(("192.168.0.105",9090)) #绑定自身IP与端口

app = [] #存放APP的IP地址
while True:
    data,IP = s.recvfrom(100000)
    if data.split(b":")[0] == b"e4a": #APP标识符
        app.append(IP)

    elif data.split(b":")[0] == b"esp32": #esp32标识符
        
         for i in app: #有多少个APP地址就发多少个
             s.sendto(data.split(b":",1)[1],i)

E4A代码图:

ESP32-cam网络开发(3)------传输图像数据(micropython)_第4张图片

 效果图与上一个一致:

你可能感兴趣的:(python)