作品要求:
一、作品设计与模块选择
1.可以感应人脸
人脸识别模块选用openmv模块,因为opencv用起来相对复杂。 用官方人脸识别示例代码进行修改,得出以下代码: 获取照片:
像素点是92,112的,并且是灰度图,pgm格式,存在singtown中,然后在singtown文件夹中再新建n个名为s1,s2,s3…sn的子文件夹,其中n为整个图像库中的人数。
#
#
import sensor, image, pyb
RED_LED_PIN = 1
BLUE_LED_PIN = 3
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.B128X128)
sensor.set_windowing((92,112))
sensor.skip_frames(10)
sensor.skip_frames(time = 2000)
num = 1
n = 20
while(n):
pyb.LED(RED_LED_PIN).on()
sensor.skip_frames(time = 3000)
pyb.LED(RED_LED_PIN).off()
pyb.LED(BLUE_LED_PIN).on()
print(n)
sensor.snapshot().save("singtown/s%s/%s.pgm" % (num, n) )
n -= 1
pyb.LED(BLUE_LED_PIN).off()
print("Done! Reset the camera to see the saved image.")
其原理是根据图片和现在拍摄的相似度去判断识别人脸
LBP人脸识别代码:
NUM_SUBJECTS = 1 #图像库中不同人数,一共1人 你要识别几张脸,就改为几
import sensor, time, image, pyb
from pyb import UART
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.B128X128)
sensor.set_windowing((92,112))
sensor.skip_frames(10)
sensor.skip_frames(time = 5000)
uart = UART(3, 9600)
NUM_SUBJECTS = 1
NUM_SUBJECTS_IMGS = 20
def min(pmin, a, s):
global num
if a<pmin:
pmin=a
num=s
return pmin
while(1):
img = sensor.snapshot()
d0 = img.find_lbp((0, 0, img.width(), img.height()))
img = None
pmin = 999999
num=0
for s in range(1, NUM_SUBJECTS+1):
dist = 0
for i in range(2, NUM_SUBJECTS_IMGS+1):
img = image.Image("singtown/s%d/%d.pgm"%(s, i))
d1 = img.find_lbp((0, 0, img.width(), img.height()))
dist += image.match_descriptor(d0, d1)
print("Average dist for subject %d: %d"%(s, dist/NUM_SUBJECTS_IMGS))
pmin = min(pmin, dist/NUM_SUBJECTS_IMGS, s)
print(pmin)
print(num)
if(num==1 and pmin<=5000):
print("adan")
uart.write("adan")
实际上可以用openmv进行操作显示屏和获取温湿度,速度等等操作,因为它本身就是个STM32单片机,但是操作起来相对麻烦,所以选用了arduino进行通信控制,openmv只当一个视觉识别模块处理。
2.感应到人脸时开启显示屏显示
屏幕我选用oled显示屏,oled显示屏示例代码(简洁):
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
void setup() {
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Hello, world!");
display.setTextColor(BLACK, WHITE);
display.println(3.141592);
display.setTextSize(2);
display.setTextColor(WHITE);
display.print("0x");
display.println(0xDEADBEEF, HEX);
display.display();
}
void loop() {
}
根据oled显示代码,加个串口中断,然后在串口中断执行显示,我们的效果就出来了:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
volatile int flag;
String shuju;
int xs_biaozhi;
unsigned long delay_data;
void setup() {
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
flag = 0;
shuju = "0";
Serial.begin(9600);
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Hello, world!");
display.display();
delay(1000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(" ");
display.display();
delay(1000);
}
void loop() {
use_delay();
uart_chuli();
}
void uart_chuli()
{
if (flag == 1) {
Serial.println(shuju);
uart_dispose();
shuju = "";
flag = 0;
}
}
void use_delay()
{
if (xs_biaozhi == 1)
{
delay_data--;
if (delay_data == 0)
{
xs_biaozhi = 0;
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(" ");
display.display();
}
}
}
void serialEvent() {
while (Serial.available() > 0)
{
shuju = shuju + char(Serial.read());
delay(2);
flag = 1;
}
}
void uart_dispose() {
if (shuju == "adan")
{
display.setTextSize(2);
display.setCursor(64, 32);
display.println(shuju);
display.display();
delay(100);
xs_biaozhi = 1;
delay_data=50000;
}
}
3.显示温度、速度等
加入DHT11模块然后在oled进行温度显示 然后加入霍尔编码器或者光电编码器就可以测小车速度了
这些都太简单了,懒得做,可以自己自行添加。
实验效果
|