gfplus001
Created July 29, 2024

ai home

AI Home geteway, include door lock, TV, light control ,temperature and humidity...etc, for safety home.

13
ai home

Things used in this project

Hardware components

UM790
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
LED (generic)
LED (generic)
×1
IR receiver (generic)
×1
Arduino UNO
Arduino UNO
×1
AMD Ryzen 9 7940HS
×1
Webcam, Logitech® HD Pro
Webcam, Logitech® HD Pro
×1

Software apps and online services

OpenCV
OpenCV
nanoFramework.Windows.Devices.Gpio
.NET nanoFramework nanoFramework.Windows.Devices.Gpio
Windows 10
Microsoft Windows 10
Visual Studio 2017
Microsoft Visual Studio 2017
Ryzen AI v1.1

Story

Read more

Schematics

arch

Code

dht

C#
#include "DHT.h"
#define DHTPIN 9 
#define DHTTYPE DHT11
//#define DHTTYPE DHT22   // DHT 22 如果用的是DHT22,就用這行
//#define DHTTYPE DHT21   // DHT 21
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  Serial.begin(9600);
  Serial.println("DHTxx test!");
  dht.begin();  //初始化DHT
  
} // setup()

void loop()
{
  delay(1000);
  float h = dht.readHumidity();   //取得濕度
  float t = dht.readTemperature();  //取得溫度C

  //顯示在監控視窗裡
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");
 
} // loop()

led

C#
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(8, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(8, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(8, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

rfid

C#
#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9          
#define SS_PIN          10  //就是模組上的SDA接腳

//S-Card HEX UID: 73 73 D5 0C
//W-Card HEX UID: C3 61 95 29
//P-Card HEX UID: 82 1C E5 10

MFRC522 mfrc522;   // 建立MFRC522實體

void setup() {

  Serial.begin(9600); 

  SPI.begin();        // 初始化SPI介面

  mfrc522.PCD_Init(SS_PIN, RST_PIN); // 初始化MFRC522卡
  Serial.print(F("Reader "));
  Serial.print(F(": "));
  mfrc522.PCD_DumpVersionToSerial(); // 顯示讀卡設備的版本
  
}


void loop() {

  // 檢查是不是一張新的卡
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
      // 顯示卡片內容
      Serial.print(F("Card HEX UID:"));
      dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); // 顯示卡片的UID
      Serial.println();
      Serial.print(F("PICC type: "));
      MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
      Serial.println(mfrc522.PICC_GetTypeName(piccType));  //顯示卡片的類型

      mfrc522.PICC_HaltA();  // 卡片進入停止模式
    }
}

/**
 * 這個副程式把讀取到的UID,用16進位顯示出來
 */
void dump_byte_array(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

irremote

C#
//https://forum.arduino.cc/t/irremote-h-updated-old-code-not-working-need-help/704322/3

#include <IRremote.h>

const int IR_RECEIVE_PIN = 7;

void setup() {
   Serial.begin(115200); //Start Serial monitor in 115200
   IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
}

void loop() {
  if (IrReceiver.decode()) {
    Serial.println(IrReceiver.decodedIRData.decodedRawData);
    delay(500);
  }
}

AITV

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //setting on vs-ui
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //no use
            if (e.KeyCode == Keys.Left)
            {
                //Some Other Code
            }
            else if (e.KeyCode == Keys.Right)
            {
                //Some Other Code
            }
            else if (e.KeyCode == Keys.Up)
            {
                //Some Other Code
            }
            else if (e.KeyCode == Keys.Down)
            {
                //Some Other Code
            }
            else if (e.KeyCode == Keys.Enter)
            {
                //Some Other Code
            }



        }


        private void button_up_Click(object sender, EventArgs e)
        {
            webBrowser1.Focus();
            SendKeys.Send("{UP}");
        }

        private void button_right_Click(object sender, EventArgs e)
        {
            webBrowser1.Focus();
            SendKeys.Send("{RIGHT}");
        }

        private void button_left_Click(object sender, EventArgs e)
        {
            webBrowser1.Focus();
            SendKeys.Send("{LEFT}");
        }

        private void button_enter_Click(object sender, EventArgs e)
        {
            webBrowser1.Focus();
            SendKeys.Send("{ENTER}");
        }

        private void button_down_Click(object sender, EventArgs e)
        {
            webBrowser1.Focus();
            SendKeys.Send("{DOWN}");

        }
        private void button_tab_Click(object sender, EventArgs e)
        {
            webBrowser1.Focus();
            SendKeys.Send("{TAB}");
        }

        // for irremote
        private SerialPort serial_port;
        private string port_name = "COM3";
        private int baud_rate = 9600;

        private string serial_buffer = "";

        public void OpenSerial()
        {
            serial_port = new SerialPort(port_name, baud_rate);

            try
            {
                serial_port.Open();
                if (!serial_port.IsOpen)
                {
                    Console.WriteLine("Fail to open " + port_name);
                    return;
                }
                else
                {
                    Console.WriteLine("Success to open " + port_name);
                }
            }
            catch (Exception e)
            {
                serial_port.Dispose();
                Console.WriteLine(e.Message);
            }
        }

        public void CloseSerial()
        {
            serial_buffer = "";
            serial_port.Dispose();
            Console.WriteLine("Close port: " + port_name);
        }

        public string ReadLines()
        {
            /*
             1:3125149440
             2:3108437760
             3:3091726080
             4:3141861120
             5:3208707840
             6:3158572800
             7:4161273600
             8:3927310080
             9:4127850240
             *:3910598400
             0:3860463360
             #:4061003520
             up:3877175040
             down:2907897600
             left:4144561920
             right:2774204160
             ok:3810328320
             */

            try
            {
                string s = serial_port.ReadExisting();
                string buffer = "";
                foreach (char c in s)
                {
                    buffer += c;
                    if (c == '\n')
                    {
                        serial_buffer += buffer;
                        buffer = "";
                    }
                }
                string ret = serial_buffer;
                serial_buffer = buffer;
                webBrowser1.Focus();
                if (ret.Length > 0 && ret[ret.Length - 1] == '\n')
                    switch (ret)
                    {
                        case "3125149440":
                            SendKeys.Send("{1}");
                            break;
                        case "3108437760":
                            SendKeys.Send("{2}");
                            break;
                        case "3091726080":
                            SendKeys.Send("{3}");
                            break;
                        case "3141861120":
                            SendKeys.Send("{4}");
                            break;
                        case "3208707840":
                            SendKeys.Send("{5}");
                            break;
                        case "3158572800":
                            SendKeys.Send("{6}");
                            break;
                        case "4161273600":
                            SendKeys.Send("{7}");
                            break;
                        case "3927310080":
                            SendKeys.Send("{8}");
                            break;
                        case "4127850240":
                            SendKeys.Send("{9}");
                            break;
                        case "3860463360":
                            SendKeys.Send("{0}");
                            break;
                        case "3910598400":
                            SendKeys.Send("{*}");
                            break;
                        case "4061003520":
                            //SendKeys.Send("{#}");
                            SendKeys.Send("{TAB}");
                            break;
                        case "3877175040":
                            SendKeys.Send("{UP}");
                            break;
                        case "2907897600":
                            SendKeys.Send("{DOWN}");
                            break;
                        case "4144561920":
                            SendKeys.Send("{LEFT}");
                            break;
                        case "2774204160":
                            SendKeys.Send("{RIGHT}");
                            break;
                        case "3810328320":
                            SendKeys.Send("{ENTRT}");
                            break;

                        default:
                            Console.WriteLine($"Unknow Error, please try again");
                            break;
                    }

                return ret;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return "";
            }
            return "";
        }

        public void Send(string s)
        {
            try
            {
                serial_port.Write(s);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public void ClearBuffer()
        {
            serial_port.DiscardInBuffer();
        }


    }
}

door onlock

Python
import cv2
import numpy as np
from openvino.runtime import Core
import datetime
from shapely.geometry import Polygon
import threading
import time
import serial
import tensorflow as tf

#rfid
def serialport():
    #S-Card HEX UID: 73 73 D5 0C
    #W-Card HEX UID: C3 61 95 29
    #P-Card HEX UID: 82 1C E5 10
    global rfid
    rfid = "invalid"
    ser = serial.Serial('COM3',9600,timeout=0.01)
    while True:
        data = ser.read_all()
        if data:
            rec_str = data
            #print(rec_str)
            if "HEX UID" in str(rec_str):
                try:
                    print(data.decode('utf-8'))
                except:
                    pass
                if "82 1C E5 10" in str(rec_str):
                    rfid = "valid(card):82 1C E5 10"
                    print("valid")
                else:
                    rfid = "invalid"
                    print("invalid")
th = threading.Thread(target = serialport)
th.start()



#FR
ie_fr = Core()
model_fr = ie_fr.read_model(model=".\\model\\fr\\openvino.xml")
model_fr.reshape([1,3,244,244])
compiled_model_fr = ie_fr.compile_model(model=model_fr, device_name="CPU")
input_layer_ir_fr = compiled_model_fr.input(0)


def fr(cut_img):

    image_fr = cut_img
    resized_image_fr = cv2.resize(image_fr, (244,244))
    resized_image_fr = cv2.cvtColor(resized_image_fr, cv2.COLOR_BGR2RGB)
    input_image_fr = np.expand_dims(resized_image_fr.transpose(2, 0, 1), 0)

    result_fr = compiled_model_fr([input_image_fr])
    result_fr = result_fr[0][0].tolist()
    idx_fr = result_fr.index(max(result_fr))
    labels_fr = ["valid","invalid"]
    
    preds_tf = tf.nn.softmax(result_fr)
    preds_tf = preds_tf.numpy().tolist()
    prob_tf = preds_tf[idx_fr]
    prob_tf = round(prob_tf*100,2)
    result = labels_fr[idx_fr]
    
    if labels_fr[idx_fr] =="valid":
        #for debug
        if prob_tf <95:
            result = "invalid"


    print("result:"+result+",prob:"+str(prob_tf))
    return result
    






#FD-main
def draw_bbox(bgr_image, resized_image, boxes,confidence):
    (real_y, real_x), (resized_y, resized_x) = bgr_image.shape[:2], resized_image.shape[:2]
    ratio_x, ratio_y = real_x / resized_x, real_y / resized_y
    rgb_image = bgr_image
    cv2.putText(rgb_image,"RFID:"+rfid,(10, 20),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(255,0,0),1)
    #cv2.putText(rgb_image,"FACE:"+fr_result,(10, 60),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(255,0,0),1)
    for box in boxes:
      
        image_id, label, conf, xmin, ymin, xmax, ymax = box
        xmin =xmin*real_x
        ymin =ymin*real_y
        xmax =xmax*real_x
        ymax =ymax*real_y
        new_box = [xmin, ymin, xmax, ymax,conf]
        if conf > confidence:
            (x_min, y_min, x_max, y_max) = [
                int(max(corner_position * ratio_y, 10)) if idx % 2 
                else int(corner_position * ratio_x)
                for idx, corner_position in enumerate(new_box[:-1])
            ]
            try:
                cut_img = rgb_image[int(ymin):int(ymax), int(xmin):int(xmax)]
                fr_result = fr(cut_img)
                cv2.putText(rgb_image,"FACE:"+fr_result,(10, 40),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(255,0,0),1)
            except:
                pass
            
            
            cv2.rectangle(rgb_image,(int(xmin), int(ymin)),(int(xmax), int(ymax)),(0,0,255),4)
            cv2.putText(rgb_image,str(int(conf*100))+"%",(int(xmin), int(ymin)-5),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(0,0,255),1)

    return rgb_image


ie = Core()
model_xml_path = "model\\fd\\face-detection-retail-0004.xml"
model = ie.read_model(model=model_xml_path)
model.reshape([1,3,300,300])
compiled_model = ie.compile_model(model=model, device_name="CPU")
input_layer_ir = compiled_model.input(0)
N, C, H, W  = (1,3,300,300)



cap = cv2.VideoCapture(0)
while cap.isOpened():
    ret, image = cap.read()
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break

    resized_image = cv2.resize(image, (300,300))
    resized_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB)
    input_image = np.expand_dims(resized_image.transpose(2, 0, 1), 0)

    result = compiled_model([input_image])

    boxes = result['detection_out'][0][0]
    image_detection = draw_bbox(image, resized_image, boxes,0.5)

    cv2.imshow('amdyes', image_detection)
    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Credits

gfplus001
1 project • 0 followers

Comments