步驟1:使用的材料
Raspberry Pi 3.~35 US $或EUR
AIY語音套件,標題焊接到HAT。 ~25US $或EUR
Adafruit TCS34725突破,焊頭焊接。 ~8美元或歐元
跳線電纜。
面包板(可選)
傳感器外殼:
- 二手“Dolce Gusto”咖啡膠囊
- 一個小圓形的2mm外匯(PVC泡沫板),直徑約37mm
- 一種非反射黑色材料,覆蓋外殼的內壁。我使用了自粘黑色橡膠泡沫。
可選:一個小開關來喚起測量值
幾滴塑料膠和一把切刀。
第2步:裝配和使用
使用AIY語音HAT的Raspberry Pi按照AIY手冊中的說明進行設置。在組裝之前,標題被焊接到HAT上的端口。對于傳感器的外殼,將“Dulce Gusto”咖啡膠囊清空,清潔,并用刀小心地取出底部的一部分。為此可以使用其他東西,咖啡膠囊的大小和形狀都合適。從一塊板上切下一塊2mm的外匯,然后將突破部分放在外匯板上,用毛氈筆標記位置,并在適當的位置切割突出部分的插槽。
現在,使用Velcro帶將外匯片粘在外殼上,并將傳感器外殼粘在外匯板上。然后用吸光的黑色材料覆蓋內壁,我使用自粘橡膠泡沫。黑色紙板應該也可以。現在,使用跨接電纜,HAT的I2C“3.3V”端口連接到傳感器上的“V in”,接地到Gnd,sda到sda和scl到scl。我用面包板連接兩個部件,但這不是必要的。
將AIY_TCS34725 python腳本放在src文件夾中,然后從dev終端運行腳本,輸入“sec/AIY_TCS34752.py”。您可能必須先使python腳本可執行。詢問時,將傳感器單元放在要測量的物體上,按下AIY設備中的按鈕并等待一兩秒鐘。
然后,根據測量的RGB和白色值,設備首先計算相應的色調值,然后根據該值估計顏色并通過AIY語音系統口頭傳達它們,例如: G。作為“暗紅色”,也給出了色調值。 RGB,色調和亮度(亮度,準確)值也會打印到屏幕上。
為簡化顏色標注過程,RGB值將轉換為HSV(色調,飽和度,值)格式。這允許將顏色注釋到特定角度范圍(即,餅圖切片),并且基于計算的色調值來選擇顏色。
您需要針對白色和黑色參考標準化您的設備。只需測量您可用的最白和最黑的紙張,分別進行測量,并將這些值作為最大值和最小值放入代碼中。只有最佳參考值才能提供良好的顏色識別。
一個基本問題是反射。如果你有一個有光澤或拋光表面的物體,它將反射LED發出的大量光線,看起來比它實際上要輕得多。您可以使用一張薄膜來散射光線,但您可能需要實施校正因子。
對于半透明物體,將它們放在白紙上可能很方便,否則反射光量將變小,物體報告為“黑色”。
如果要測量發光物體的顏色,應通過連接“關閉”來關閉突破口上的LED。 LED“端口突破到”地面“。現在相應地設置標準化值。
另一個普遍問題是對象的照明。突破上的暖白色LED發出不連續的光譜。因此,某些顏色可能在RGB光譜中過高或過低。
結果
步驟3:代碼
代碼是對AIY語音手冊中的代碼修改和TCS34725傳感器代碼的組合作者。
我還試過使用Adafruit的TCS34725 python代碼,但是運行這個以及其他一些使用外部庫和AIY HAT的代碼有問題。歡迎任何幫助。
如前所述,顏色標注基于RGB到色調值的轉換。您必須根據白色和黑色崇敬材料的實驗測量設置標準化設置。相應地填寫R,G和B min或max的絕對值。
該腳本使用新版本的“say”命令,可以調節音量和音高。如果您需要更新audio.py和tty驅動程序文件或從腳本中刪除“音量和音高部分”。
#!/usr/bin/env python3
# This script is an adaption of the servo_demo.py script for the AIY voice HAT,
# optimized for the color recognition uing the Afafruit TCS34725 breakout
import aiy.audio
import aiy.cloudspeech
import aiy.voicehat
#from gpiozero import LED # could be helpful for an external LED on servo-port
#from gpiozero import Button # could be helpful for an external button on servo-port
import time
import smbus
bus = smbus.SMBus(1)
import colorsys
def hue2color(hue): # color interpretation based on the calculated hue values
if ((hue》 12) and (hue《 26)): # i.e. between 12° and 40°。 All settings may require optimization
color=“orange”
return color
elif ((hue》 25) and (hue《 70)):
color=“yellow”
return color
elif ((hue》 69) and (hue《 165)):
color=“green”
return color
elif ((hue》 164) and (hue《 195)): # 180 +/- 15
color=“cyan”
return color
elif ((hue》 194) and (hue《 270)):
color=“blue”
return color
elif ((hue》 269) and (hue《 320)):
color=“magenta”
return color
elif ((hue》 319) or (hue《 20)):
color=“red”
return color
else: print (“something went wrong”)
def tcs34725(): # measurement and interpretation.
# The measurement is performed by the Bradspi TCS34725 script:
# https://bradsrpi.blogspot.com/2013/05/tcs34725-rg.。.
bus.write_byte(0x29,0x80|0x12)
ver = bus.read_byte(0x29)
# version # should be 0x44
if ver == 0x44:
print (“Device found ”)
bus.write_byte(0x29, 0x80|0x00) # 0x00 = ENABLE register
bus.write_byte(0x29, 0x01|0x02) # 0x01 = Power on, 0x02 RGB sensors enabled
bus.write_byte(0x29, 0x80|0x14) # Reading results start register 14, LSB then MSB
data = bus.read_i2c_block_data(0x29, 0)
clear = clear = data[1] 《《 8 | data[0]
red = data[3] 《《 8 | data[2]
green = data[5] 《《 8 | data[4]
blue = data[7] 《《 8 | data[6]
crgb = “Absolute counts: C: %s, R: %s, G: %s, B: %s ” % (clear, red, green, blue)
print (crgb)
time.sleep(1)
else:
print (“Device not found ”)
# normalization and transformation of the measured RGBW values
col=“”
# Maximum values Normalization factors, must be defined experimentally
# e.g. vs. a white sheet of paper. Check and correct from time to time.
max_bright = 5750
max_red = 1930
max_green = 2095
max_blue = 1980
# Background/Minimum values normalization factors, must be defined experimentally
# e.g. vs. black sheet of paper. Check and correct from time to time.
min_bright = 750
min_red = 340
min_green = 245
min_blue = 225
# normalized values, between 0 and 1
rel_bright = ((clear - min_bright)/(max_bright - min_bright))
rel_red = ((red - min_red)/(max_red - min_red))
rel_green = ((green - min_green)/(max_green - min_green))
rel_blue = ((blue - min_blue)/(max_blue - min_blue))
hsv_col = colorsys.rgb_to_hsv(rel_red, rel_green, rel_blue)
hue = hsv_col[0]*359
if rel_bright 》 0.9: col = “white” # if very bright -》 white
elif rel_bright 《 0.1: col = “black” # if very dark -》 black
else: col = hue2color(hue) # color selection by hue values
# print(“relative values bright, red, green, blue:”)
# print (rel_bright, rel_red, rel_green, rel_blue)
# print(“HSV values (hue, saturation, value):”, hsv_col)
# print (“hue in ° ”,hue)
return [col, rel_bright, rel_red, rel_green, rel_blue, hue]
def main():
button = aiy.voicehat.get_button() # change Button status
led = aiy.voicehat.get_led() # change Button-LED status
aiy.audio.get_recorder().start()
# buttoni= Button(5) # distance sensor or other external button, connected to servo3/GPIO 05
aiy.audio.say(“Hello!”, lang=“en-GB”, volume=50, pitch=100) # volume and pitch require November 2017 revision of audio.py and _tty.py driver!
aiy.audio.say(“To start, move the sensor above the object. Then press the blue button”, lang=“en-GB”, volume=50, pitch = 100)
print(“To activate color measurement place sensor above object, then press the blue button”)
while True:
led.set_state(aiy.voicehat.LED.ON)
button.wait_for_press() # for external button, replace button by buttoni
led.set_state(aiy.voicehat.LED.BLINK)
aiy.audio.say(“Measuring”, lang=“en-GB”, volume=50, pitch = 100)
result = tcs34725() # evokes measurement and interpretation
col = result[0] # color, as text
hue = str(int(result[5])) # hue in °, as text
r_red = str(int(result[2]*255)) # R value, as text
r_green = str(int(result[3]*255)) # G value, as text
r_blue = str(int(result[4]*255)) # B value, as text
r_bright = str(int(result[1]*100)) # W value, as text
led.set_state(aiy.voicehat.LED.OFF)
if col == “white” or col==“black”:
bright = “”
elif (result[1] 》0.69): #brightness/lightness of color
bright =“light”
elif (result[1] 《0.25):
bright =“dark”
else :
bright =“medium”
# communiating the results
color_text =(“The color of the object is ” + bright + “ ” + col)
print (color_text)
aiy.audio.say(color_text, lang=“en-GB”, volume=75, pitch=100)
hue_text = (“The hue value is ”+ hue+ “ degrees”)
print (hue_text)
aiy.audio.say(hue_text, lang=“en-GB”, volume=75, pitch = 100)
if __name__ == ‘__main__’:
main()
-
色彩傳感器
+關注
關注
0文章
12瀏覽量
5818
發布評論請先 登錄
相關推薦
評論