K210电赛创意

from fpioa_manager import fm
from machine import UART
from board import board_info
from Maix import FPIOA,GPIO
import utime
import sensor,image,lcd
import KPU as kpu
import time
import os

fm.register(9,fm.fpioa.UART2_TX,force=True)
fm.register(10,fm.fpioa.UART2_RX,force=True)
fm.register(11, fm.fpioa.GPIO0)
led_r = GPIO(GPIO.GPIO0,GPIO.OUT)

uart_A = UART(UART.UART2,9600,8,0,1,read_buf_len=4096)

lcd.init(invert=True)
lcd.rotation(2)
sensor.reset() #初始化摄像头
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_hmirror(1) #镜像
sensor.set_vflip(1)   #翻转
img = image.Image()

while True:
    img.draw_string(10, 20, "A. Face recognition\n\n\nB. Mask recognition\n\n\nC. LCDShow temperature", scale=2)
    lcd.display(img)
    utime.sleep_ms(10)
    img.clear()
    mode_selection = uart_A.read()
    if mode_selection != None:
        mode_str = mode_selection.decode()
        print(mode_str)
        if mode_str[0:1] == 'a':
               #运行人脸识别程序
                   task_fd = kpu.load("/sd/FD.smodel")
                   task_ld = kpu.load("/sd/KP.smodel")
                   task_fe = kpu.load("/sd/FE.smodel")
                   clock = time.clock()  #初始化时钟
                   key_pin=16 #设置引脚
                   fpioa = FPIOA()
                   fpioa.set_function(key_pin,FPIOA.GPIO7)
                   key_gpio=GPIO(GPIO.GPIO7,GPIO.IN)
                   last_key_state=1
                   key_pressed=0 #初始化引脚
                   def check_key(): #是否按下
                       global last_key_state
                       global key_pressed
                       val=key_gpio.value()
                       if last_key_state == 1 and val == 0:
                           key_pressed=1
                       else:
                           key_pressed=0
                       last_key_state = val
                   anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025) #anchor for face detect 用于人脸检测的Anchor
                   dst_point = [(44,59),(84,59),(64,82),(47,105),(81,105)] #standard face key point position 标准正脸的5关键点坐标 分别为 左眼 右眼 鼻子 左嘴角 右嘴角
                   kpu.init_yolo2(task_fd, 0.5, 0.3, 5, anchor) #初始化模型
                   img_face=image.Image(size=(128,128)) #设置128 * 128图片
                   img_face.pix_to_ai()
                   record_ftr=[]
                   record_ftrs=[]
                   fll = True
                   names = ['Number1', 'Number2', 'Number3', 'Number4', 'Number5', 'Number6', 'Number7', 'Number8', 'Number9' , 'Number10'] # 人名标签,与上面列表特征值一一对应。
                   while fll: # 主循环
                       check_key() #按键检测
                       img = sensor.snapshot() #获取一张图片
                       clock.tick() #计算帧率
                       code = kpu.run_yolo2(task_fd, img) #运行人脸检测模型,获取人脸坐标位置
                       if code: #如果检测到人脸
                           for i in code: # 迭代坐标框
                               img.draw_rectangle(i.rect(),color=(0,0,255),scale=3) # 在屏幕显示人脸方框
                               face_cut=img.cut(i.x(),i.y(),i.w(),i.h()) # 裁剪人脸部分图片到 face_cut
                               face_cut_128=face_cut.resize(128,128) # 将裁出的人脸图片 缩放到128 * 128像素
                               a=face_cut_128.pix_to_ai() # 将猜出图片转换为kpu接受的格式
                               # Landmark for face 5 points
                               fmap = kpu.forward(task_ld, face_cut_128) # 运行人脸5点关键点检测模型
                               plist=fmap[:] # 获取关键点预测结果
                               le=(i.x()+int(plist[0]*i.w() - 10), i.y()+int(plist[1]*i.h())) # 计算左眼位置, 这里在w方向-10 用来补偿模型转换带来的精度损失
                               re=(i.x()+int(plist[2]*i.w()), i.y()+int(plist[3]*i.h())) # 右眼位置
                               nose=(i.x()+int(plist[4]*i.w()), i.y()+int(plist[5]*i.h())) #鼻子位置
                               lm=(i.x()+int(plist[6]*i.w()), i.y()+int(plist[7]*i.h())) #左嘴角位置
                               rm=(i.x()+int(plist[8]*i.w()), i.y()+int(plist[9]*i.h())) #右嘴角位置
                               src_point = [le, re, nose, lm, rm] # 图片中 5 坐标的位置
                               T = image.get_affine_transform(src_point, dst_point) # 根据获得的5点坐标与标准正脸坐标获取仿射变换矩阵
                               image.warp_affine_ai(img, img_face, T) #对原始图片人脸图片进行仿射变换,变换为正脸图像
                               img_face.ai_to_pix() # 将正脸图像转为kpu格式
                               #a = img.draw_image(img_face, (128,0))
                               del(face_cut_128) # 释放裁剪人脸部分图片
                               # calculate face feature vector
                               fmap = kpu.forward(task_fe, img_face) # 计算正脸图片的196维特征值
                               #a = kpu.deinit(task_fe)
                               feature=kpu.face_encode(fmap[:]) #获取计算结果
                               reg_flag = False
                               scores = [] # 存储特征比对分数
                               for j in range(len(record_ftrs)): #迭代已存特征值
                                   score = kpu.face_compare(record_ftrs[j], feature) #计算当前人脸特征值与已存特征值的分数
                                   scores.append(score) #添加分数总表
                               max_score = 0
                               index = 0
                               for k in range(len(scores)): #迭代所有比对分数,找到最大分数和索引值
                                   if max_score < scores[k]:
                                       max_score = scores[k]
                                       index = k
                               if max_score > 65: # 如果最大分数大于65, 可以被认定为同一个人
                                   img.draw_string(i.x(),i.y(), ("%s" %names[index]), color=(0,255,0),scale=2) # 显示人名 与 分数
                               else:
                                       led_r.value(0)
                                       utime.sleep_ms(30)
                                       led_r.value(1)
                                       utime.sleep_ms(30)
                                       img.draw_string(i.x(),i.y(), ("Stranger! ! !"), color=(255,0,0),scale=2) #显示未知 与 分数
                               if key_pressed == 1: #如果检测到按键
                                   key_pressed = 0 #重置按键状态
                                   record_ftr = feature
                                   record_ftrs.append(record_ftr) #将当前特征添加到已知特征列表
                               break
                       lcd.display(img) #刷屏显示
                       mode_selection = uart_A.read()
                       if mode_selection != None:
                           mode_str = mode_selection.decode('UTF-8')
                           print(mode_str)
                           if mode_str[0:1] == '*':
                               fll = False
                               lcd.clear()
                               img.clear()
                               #lcd.clear()
                   kpu.deinit(task_fd)
                   kpu.deinit(task_ld)
                   kpu.deinit(task_fe)
                   kpu.memtest()

        elif mode_str[0:1] == 'c':
                pass
                #fff = 70
                #while fff:
                    #img.draw_string(100,100, ("Temperature: %s.%s C" %(mode_str[1:3],mode_str[3:5])), color=(255,200,0),scale=2)
                    #lcd.display(img)
                    #utime.sleep_ms(10)
                    #img.clear()
                    #fff = fff-1
                #img.clear()
                #lcd.clear()
        elif mode_str[0:1] == 'b':
                    color_R = (255, 0, 0)
                    color_G = (0, 255, 0)
                    color_B = (0, 0, 255)
                    class_IDs = ['no_mask', 'mask']
                    def drawConfidenceText(image, rol, classid, value):
                        text = ""
                        _confidence = int(value * 100)
                        if classid == 1:
                            text = 'OK' #+ str(_confidence) + '%'
                            image.draw_string(rol[0], rol[1], text, color=color_G, scale=2.5)
                        else:
                            text = 'WARNING'# + str(_confidence) + '%'
                            image.draw_string(rol[0], rol[1], text, color=color_R, scale=2.5)
                    sensor.run(1)
                    task = kpu.load("/sd/mask.kmodel")


                    anchor = (0.1606, 0.3562, 0.4712, 0.9568, 0.9877, 1.9108, 1.8761, 3.5310, 3.4423, 5.6823)
                    kpu.init_yolo2(task, 0.5, 0.3, 5, anchor)
                    #img = image.Image()

                    clock = time.clock()
                    while (True):
                        clock.tick()
                        img = sensor.snapshot()
                        code = kpu.run_yolo2(task, img)
                        if code:
                            totalRes = len(code)

                            for item in code:
                                confidence = float(item.value())
                                itemROL = item.rect()
                                classID = int(item.classid())

                                #if confidence < 0.52:
                                   # _ = img.draw_rectangle(itemROL, color=color_B, tickness=5)
                                    #continue

                                if classID == 1 and confidence > 0.65:
                                    img.draw_rectangle(itemROL, color_G, tickness=4)
                                    if totalRes == 1:
                                        drawConfidenceText(img, (0, 0), 1, confidence)
                                else:
                                    img.draw_rectangle(itemROL, color=color_R, tickness=8)
                                    if totalRes == 1:
                                        drawConfidenceText(img, (0, 0), 0, confidence)
                                        led_r.value(0)
                                        utime.sleep_ms(30)
                                        led_r.value(1)
                                        utime.sleep_ms(30)
                        lcd.display(img)
                        print(clock.fps())
                        mode_selection = uart_A.read()
                        if mode_selection != None:
                            mode_str = mode_selection.decode('UTF-8')
                            #mode_selection = char(mode_selection
                            print(mode_str)
                            if mode_str[0:1] == '*':
                                kpu.deinit(task)
                                kpu.memtest()
                                img.clear()
                                lcd.clear()
                                break
uart_A.deinit()
del uart_A

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于 WPF 中使用 Teigha ImageDef 的示例,以下是一个简单的代码示例: 1. 首先,确保你已经安装了 Teigha.NET,并将其添加为项目的引用。 2. 在 XAML 文件中,添加一个 Image 控件用于显示图像: ```xaml <Image x:Name="imageControl" /> ``` 3. 在代码文件中,使用以下代码加载和显示图像: ```csharp using System; using System.IO; using System.Windows; using System.Windows.Media.Imaging; using Teigha.GraphicsSystem; using Teigha.Runtime; namespace YourNamespace { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void LoadImage() { var rasterImage = new OdGiRasterImage(); // 你可以修改以下路径为你自己的图像路径 var imagePath = @"C:\path\to\your\image.jpg"; if (File.Exists(imagePath)) { // 使用 Teigha.NET 加载图像文件 rasterImage.load(imagePath); // 将图像转换为 WPF 的 BitmapSource var bitmapSource = BitmapSource.Create( rasterImage.pixelWidth(), rasterImage.pixelHeight(), 96, 96, ConvertPixelFormat(rasterImage.colorDepth()), null, rasterImage.scanLines(), rasterImage.scanLinesStride()); // 在 Image 控件中显示图像 imageControl.Source = bitmapSource; } else { MessageBox.Show("指定的图像文件不存在!"); } } private PixelFormat ConvertPixelFormat(int colorDepth) { switch (colorDepth) { case 8: return PixelFormats.Gray8; case 24: return PixelFormats.Bgr24; case 32: return PixelFormats.Bgra32; default: return PixelFormats.Default; } } private void Window_Loaded(object sender, RoutedEventArgs e) { LoadImage(); } } } ``` 这个示例演示了如何使用 Teigha.NET 加载图像文件,并将其显示在 WPF 中的 Image 控件中。你可以根据自己的需要进行修改和扩展。记得将代码中的 `YourNamespace` 替换为你自己的命名空间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值