サーマルカメラ(サーモ AI デバイス TiD) Python D6T-44L-06編

オムロン MEMS非接触温度センサ D6T-44L-06の温度データを、I2C通信で取得します。

***
1. [紹介編](https://qiita.com/MH-SS/items/c3d12abe871bfd62d1df)
2. [センサ編](https://qiita.com/MH-SS/items/61021e58b3a5a7f81b3d)
3. [センサケース編](https://qiita.com/MH-SS/items/ab142942c702eb6e545f)
4. [Raspberry Pi編](https://qiita.com/MH-SS/items/73f09db1ad1890c90987)
5. Python編  
    5.1 [Form編](https://qiita.com/MH-SS/items/035682853d18dc2074f2)  
    5.2 [オムロン 非接触温度センサ D6T-44L-06編](https://qiita.com/MH-SS/items/f9301bde143de482a17a)  
    5.3 [Pololu 測距センサ VL53L0X編](https://qiita.com/MH-SS/items/df3c33a6256a533d4eb5)  
    5.4 [BOSCH 温湿度・気圧センサ BME280](https://qiita.com/MH-SS/items/aff10e7b4d84c1b03714)  
    5.5 [シャットダウン・再起動スイッチ編](https://qiita.com/MH-SS/items/82db17524553e56d7605)  
    5.6 [OpenCV編](https://qiita.com/MH-SS/items/bcc9092bb9b6977014a1)  
    5.7 [高速化編](https://qiita.com/MH-SS/items/154dd994d73b4c93854f)  

***

![d6t.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/569469/6198d76f-2486-2668-6f94-a070b4c3dae9.jpeg)

Raspberry Pi上のPythonでI2Cを制御する為に、smbusモジュールを使用します。
smbusモジュールがインストールされていない場合、下記コマンドでインストールしてください。

```
pip install smbus
```

<!-- d6t_44l_06.pyはパッケージとしてimportして使用しますが、テスト用に単体で温度データを取得できます。
<a href="https://mhss.server-on.net/a656d/index/tid3/development/python/d6t/d6t_44l_06.zip" target="_blank">d6t_44l_06.py (ZIPで圧縮済)</a> -->

```python
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
D6T-44L-06 script.
==================
Temperature measure 5 times/second.
Human-detectable distance: MAX 7[m]
5.0[V] I2C(0Ah)
4x4 array 5~45[deg]
***
2020/9/7
* Remake def pixels().
* Previous def result[0] is device temperature.
* Sometime temperature is 41degC over.
* Max temperature set 40degC.
"""
import threading
import time

import numpy as np
i2c_enable = False
try:
    import smbus
    i2c_enable = True
except:
    i2c_enable = False

ADDRESS = 0x0a
REGISTER = 0x4c


class Sensor():
    def __init__(self, *args, **kwargs):
        self.avg_count = 10
        self.enable = False
        self._previous_result = None
        self._shutdowning = False
        self.temperature = 0.0

        self.i2c_enable = i2c_enable
        self._pixels_data = None

        if self.i2c_enable:
            self.enable = True
            self._i2c = smbus.SMBus(1)

        thread = threading.Thread(
            name='D6T-44L-06_Measuring',
            target=self.thread,
        )
        thread.daeom = True
        thread.start()

    @property
    def array(self):
        return (self.x, self.y)

    def avg(self, pixels):
        if self._pixels_data is None:
            self._pixels_data = [pixels]
        elif len(self._pixels_data) < self.avg_count:
            self._pixels_data = np.append(
                self._pixels_data, [pixels], axis=0)
        elif len(self._pixels_data) == self.avg_count:
            self._pixels_data = np.delete(
                self._pixels_data, 0, axis=0)
            self._pixels_data = np.append(
                self._pixels_data, [pixels], axis=0)

    def __del__(self):
        self._shutdowning = True

    @property
    def high(self):
        return 45.0

    @property
    def low(self):
        return 5.0

    @property
    def name(self):
        return 'D6T-44L-06'

    @property
    def pixels(self):
        result = None
        if self._pixels_data is not None:
            result = np.average(self._pixels_data, axis=0)
        return result

    def stop(self):
        self._shutdowning = True

    def thread(self):
        if self._shutdowning:
            return

        # for parent Thermo class.
        result = None

        if self.enable:
            result = []
            try:
                data = self._i2c.read_i2c_block_data(
                    ADDRESS,
                    REGISTER,
                    32)
            except:
                self.i2c_enable = False
                self.enable = False
                return

            self.temperature = (data[1]*256 + data[0]) / 10.0

            data.pop(0)
            data.pop(0)

            if data[1] < 250 :
                for i in range(4):
                    for j in range(4):
                        temperature \
                            = int((data[i*2*j+1])*256 + data[i*2*j]) / 10.0

                        if temperature > 41.0:
                            break

                        result.append(temperature)

            if len(result) != 16:
                result = self._previous_result
            else:
                self._previous_result = result

        self.avg(result)

        time.sleep(0.2)

        thread = threading.Thread(
            name='D6T-44L-06_Measuring',
            target=self.thread,
        )
        thread.daeom = True
        thread.start()

    @property
    def x(self):
        return 4

    @property
    def y(self):
        return 4

if __name__ == '__main__':
    import time
    sensor = Sensor()
    while True:
        tmp = sensor.pixels
        if tmp is not None:
            print(tmp)
            #print(max(tmp), min(tmp))
        time.sleep(1)
    pass
```

***
[YouTube: サーマルカメラ(サーモ AI デバイス TiD) Python編](https://www.youtube.com/watch?v=fgwBsISDtXo)
[web: サーモ AI デバイス TiD Python D6T編](https://mhss.server-on.net/?f401c=Index&ae504=tid3/development/python/d6t) (URLが変更されました)