编写一段openmv+esp8266进行行人识别的代码
时间: 2024-01-16 19:02:52 浏览: 88
好的,下面是一段openmv esp8266进行行人识别的代码:
```python
import sensor, image, time, network, usocket, sys
import sensor, image, time
SSID='你的WiFi名称' #WiFi名称
KEY='你的WiFi密码' #WiFi密码
net = network.WINC(mode=network.WINC.MODE_STA)
wifi = network.WLAN(network.STA_IF)
# 连接WiFi
wifi.active(True)
wifi.connect(SSID, KEY)
while wifi.isconnected() == False:
pass
# 设置sensor
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()
# 设置websocket链接, 获取边界框JSON
def get_boundary(connection):
is_recv = False
content = ""
while True:
data = connection.recv(1024)
if data:
# 开始接收数据
is_recv = True
content += str(data, encoding="utf-8")
elif is_recv:
# 数据接收完毕
return content
# 设置websocket链接, 发送图片数据
def send_pic(connection, payload):
# 发送至CNN数据预处理
jsonStr = str(payload)
# 边界框JSON
boundary = "\r\n--" + "openmv_firmware" + "\r\nContent-Disposition: form-data; name=\"file\"\r\n\r\n"+ jsonStr + "\r\n--" + "openmv_firmware" + "\r\nContent-Disposition: form-data; name=\"image\"; "
connection.send(bytes(boundary, 'utf8'))
# 传输图像数据
img = sensor.snapshot()
img_arr = image.image_to_jpeg(img)
connection.send(bytes("Content-Type:image/jpeg \r\n\r\n", 'utf8'))
connection.send(img_arr)
# 结束符
connection.send(bytes("\r\n--" + "openmv_firmware" + "--\r\n", 'utf8'))
while True:
try:
addr = usocket.getaddrinfo("192.168.0.100", 12345)[0][-1]
s = usocket.socket()
s.connect(addr)
print("已链接:", addr)
print("开始识别!")
while True:
clock.tick()
frame = sensor.snapshot()
# 调整图像亮度和对比度,可以提高行人识别成功率
frame.contrast(1.5)
frame.brightness(0)
# 识别行人
objs = frame.find_features(image.HAAR_LBP_GRADIENTS, 0.5, scale_factor=1.2, stride=16, \
roi=(10, 10, frame.width() - 20, frame.height() - 20))
for obj in objs:
# 获取边角坐标
x, y, w, h = obj
payload = {"x_tl": x, "y_tl": y, "x_br": x + w, "y_br": y + h}
# 连接websocket
send_pic(s, payload)
# 获取HTTP边界框JSON
response = get_boundary(s)
print(response)
except Exception as e:
# 如果出现异常,自动重连
print("遇到异常,开始重连...")
print(e)
time.sleep(2)
```
希望对你有所帮助!
阅读全文