用python获取esp-32 cam的视频流

开始之前ESP32cam要烧写MicroPython固件。

烧写方法自行百度。

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('uuu', '12345678')
    
    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.43.61",9090)) # 向服务器发送图像数据
    time.sleep(0.1)
 
 
 
 
 

esp32cam代码部分,复制后保存即可。注意ip wifi账号密码改成自己的!

服务端代码

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.43.61", 9090))
while True:
    data, IP = s.recvfrom(100000)
    print("!1")
    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

 

从esp32cam获取的效果

 

评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NoBug.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值