回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

【Maixduino AI试用】智能家居监控 简单

头像 云天 2021.10.29 545 0

功能:主控使用Maixduino套件+扩展板+旋钮,连接物联网Easy Iot,实现物联网报警,回看监控人脸图像。

 

project-image
代码
# MindPlus
# maixduino
from network_esp32 import wifi
from simple import MQTTClient
from board import board_info
from pin import Pin
import KPU as kpu
import sensor
import image
import time
import lcd


def camera_init():
  sensor.reset()
  sensor.set_pixformat(sensor.RGB565)
  sensor.set_framesize(sensor.QVGA)
  sensor.skip_frames(10)
  sensor.run(1)


if wifi.isconnected() == False:
    for i in range(5):
        try:
            wifi.reset(is_hard=True)
            print('try AT connect wifi...')
            wifi.connect("sxs", "smj080823")
            if wifi.isconnected():
                break
        except Exception as e:
            print(e)

mqtt = MQTTClient("sxs001", "182.254.130.180", 1883, "RNZYsQIWg", "gHWLswIWgz", keepalive=0)
try:
    mqtt.connect()
    print('Connected')
except:
    print('Disconnected')
while not (wifi.isconnected()):
  pass
lcd.init(freq=15000000, color=65535, invert=0)
camera_init()
sensor.set_vflip(0)
sensor.set_windowing((320, 240))
classes =  ['face', 'no_face']
anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)
task = kpu.load("/sd/facedetect.kmodel")
kpu.init_yolo2(task,0.3,0.3,5,anchor)
YuZhi = 0
XuHao = 0
j = 0
while True:
  print(str(Pin.analog_read(board_info.PINA0)))
  if (Pin.analog_read(board_info.PINA0) > 500):
    img = image.Image((str("/sd/face") + str((str((str(j))) + str(".jpg")))))
    lcd.display(img)
    time.sleep(3)
    if (j > 0):
      j = (j - 1)
    else:
      j = XuHao-1
  else:
    img = sensor.snapshot()
    lcd.display(img)
    code = kpu.run_yolo2(task,img)
    if bool(code):
      if (YuZhi > 10):
        YuZhi = 0
        for i in code:
          img = img.draw_rectangle(i.rect(),(0,0,255),1,0)
          lcd.display(img)
          lcd.draw_string(i.x(), i.y(), str((classes[i.classid()])), 248, 65535)
          j = XuHao
          img.crop(x_scale=1.0, y_scale=1.0)
          img.save((str("/sd/face") + str((str((str(XuHao))) + str(".jpg")))))
          print(img.width())
          XuHao = (XuHao + 1)
          mqtt.publish1( "5n92uqBMg","Someone broke in")
      else:
        YuZhi = (YuZhi + 1)
project-image

备注:代码由Mind+积木块编写,但积木块中缺少缩放模块,保存图像时出现内存不足问题。所以利用“自动生成“代码,在”手动编辑“中增加img.crop,再上传。

project-image
project-image

需在保存图像前增加:img.crop

         img.crop(x_scale=1.0, y_scale=1.0)
         img.save((str("/sd/face") + str((str((str(XuHao))) + str(".jpg")))))
         print(img.width())

评论

user-avatar