|
本项目转载自home-made-garbage,作者通过AI摄像头标记金鱼的坐标尝试判断金鱼是否在睡觉,代码分为UnitV和StickC部分,UnitV串口传输坐标,最终通过StickC的WiFi对外进行UDP广播。
关于金鱼的模型可以通过V-training进行学习,UnitV识别金鱼并通过串口发送其中心坐标(x,y),画面大小224x224。
UnitV端
[mw_shl_code=python,true]from machine import UART
from board import board_info
from fpioa_manager import fm
from Maix import GPIO
import sensor,image,lcd
import KPU as kpu
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_windowing((224, 224))
sensor.set_vflip(1)
sensor.set_hmirror(1)
sensor.run(1)
fm.register(35, fm.fpioa.UART1_TX, force=True)
fm.register(34, fm.fpioa.UART1_RX, force=True)
uart = UART(UART.UART1, 115200,8,0,0, timeout=1000, read_buf_len=4096)
classes = ["goldfish"]
task = kpu.load(0x600000)
#task = kpu.load("/sd/model246pic.kmodel")
anchor = (0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828)
a = kpu.init_yolo2(task, 0.3, 0.3, 5, anchor)
while(True):
img = sensor.snapshot()
code = kpu.run_yolo2(task, img)
if code:
for i in code:
a = img.draw_cross(i.x()+int(i.w()/2),i.y()+int(i.h()/2),color = (0, 255, 0),size=5,thickness=4)
data_packet = bytearray([0xFF,0xD8,0xEA,0x01,i.index(),i.x()+int(i.w()/2),i.y()+int(i.h()/2),0x00,0x00,0x00])
uart.write(data_packet)
a = lcd.display(img)
else:
a = lcd.display(img)
a = kpu.deinit(task)[/mw_shl_code]
M5StickC端接收UnitV的金鱼坐标并通过UDP发送
[mw_shl_code=arduino,true]#include <WiFi.h>
#include <WiFiUDP.h>
#include <M5StickC.h>
static WiFiUDP wifiUdp;
static const char *kRemoteIpadr = "192.168.0.255";
static const int kRmoteUdpPort = 9000;
const char* ssid = "WiFi-SSID";
const char* password = "password";
static const uint8_t packet_begin[3] = { 0xFF, 0xD8, 0xEA };
void setup() {
M5.begin();
M5.Axp.ScreenBreath(0);
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, 32, 33);
static const int kLocalPort = 7000;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
wifiUdp.begin(kLocalPort);
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if (Serial2.available()) {
uint8_t rx_buffer[10];
int rx_size = Serial2.readBytes(rx_buffer, 10);
if (rx_size == 10) {
if ((rx_buffer[0] == packet_begin[0]) && (rx_buffer[1] == packet_begin[1]) && (rx_buffer[2] == packet_begin[2])) {
Serial.print(rx_buffer[4]);
Serial.print(":");
Serial.print(rx_buffer[5]);
Serial.print(",");
Serial.println(rx_buffer[6]);
wifiUdp.beginPacket(kRemoteIpadr, kRmoteUdpPort);
wifiUdp.print(rx_buffer[4]);
wifiUdp.print(":");
wifiUdp.print(rx_buffer[5]);
wifiUdp.write(',');
wifiUdp.print(rx_buffer[6]);
wifiUdp.endPacket();
}
}
}
}
[/mw_shl_code]
根据坐标绘制图表,其中Y轴为移动距离
X轴为时间点
可以看出图表中1点到5点的移动量明显减少,证明金鱼此时在睡觉。
|
|