資源簡介
學MaixPy之機器視覺,在MaixPy IDE開發環境,用Maix Dock K210開發板,實現動態識別色塊,包括紅、藍、綠、黃四個色。
代碼片段和文件信息
#MicroPython動手做(08)——零基礎學MaixPy之識別顏色
#實驗程序:find?red?blob?動態識別紅色塊
import?sensorimagelcdtime
from?fpioa_manager?import?*
from?Maix?import?GPIO
#import?_thread
from?machine?import?Timer
#?注冊芯片的外設和引腳的對應關系
fm.register(board_info.LED_R?fm.fpioa.GPIO1)???#?RED
fm.register(board_info.LED_G?fm.fpioa.GPIO2)???#?GREEN
fm.register(board_info.LED_B?fm.fpioa.GPIO3)???#?BLUE
#?定義?GPIO?對象
led_r=GPIO(GPIO.GPIO1GPIO.OUT)
led_g=GPIO(GPIO.GPIO2GPIO.OUT)
led_b=GPIO(GPIO.GPIO3GPIO.OUT)
led_r.value(1)
led_g.value(1)
led_b.value(1)
lcd.init(freq=15000000)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)?#format:?幀格式?推薦設置為RGB565格式
sensor.set_framesize(sensor.QVGA)?#framesize:?幀大小?推薦設置為QVGA格式
sensor.set_auto_gain(0)??#1?表示開啟自動增益?0?表示關閉自動增益?如果需要追蹤顏色,需要關閉自動增益
sensor.set_vflip(1)?#鏡頭正常反轉180°
sensor.run(1)?#1?表示開始抓取圖像?0?表示停止抓取圖像
lcd.set_backlight(0)
red_threshold??=??(60?68??50??62??28??42)????#(55?70?42?65?52?8)
green_threshold=??(85?95?-80?-49??55??70)????#(0?88?-42?-6?-9?13)
blue_threshold?=??(79?85?-24?-11?-27?-15)????#(0?80?-128?35?-128?-18)
yellow_threshold?=(69?80???5??21??55??73)???????#?(88?95?0?-44?93?48)
printfinish_Y?=?0
printfinish_R?=?0
printfinish_G?=?0
printfinish_B?=?0
while?True:
????img=sensor.snapshot()?#使用攝像頭拍攝一張照片
????blobs_R?=?img.find_blobs([red_threshold])
????blobs_G?=?img.find_blobs([green_threshold])
????blobs_B?=?img.find_blobs([blue_threshold])
????blobs_Y?=?img.find_blobs([yellow_threshold])
????if?blobs_R:
????????img.draw_string(60?100?“color?is?:?Red!“?scale=2)
????????if?printf
- 上一篇:計算機視覺 opencv 哈哈鏡
- 下一篇:python 采集京東商品數據
評論
共有 條評論