import sensor, image, time,math,pyb
from pyb import UART,LED
import jsonimport sensor, image, time,math,pyb
from pyb import UART,LED
import json
import ustruct
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
wine_red_threshold = (52, 27, 8, 127, -30, 25) # 酒红色阈值示例
pomegranate_red_threshold = (0, 63, 41, 127, -128, 127) # 石榴红阈值示例
blue_threshold = (48, 64, -50, 2, -65, -15) # 蓝色阈值
green_threshold = (92, 59, -75, -14, 2, 35) # 绿色阈值
clock = time.clock()
uart = UART(3,19200) #定义串口3变量
uart.init(19200, bits=8, parity=None, stop=1) # init with given parameters
def find_max(blobs): #定义寻找色块面积最大的函数
max_size=0
for blob in blobs:
if blob.pixels() > max_size:
max_blob=blob
max_size = blob.pixels()
return max_blob
def sending_data(cx,cy,cw,ch):
global uart;
#frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];
#data = bytearray(frame)
data = ustruct.pack("<bbhhhhb", #格式为俩个字符俩个短整型(2字节)
0x2C, #帧头1
0x12, #帧头2
int(cx), # up sample by 4 #数据1
int(cy), # up sample by 4 #数据2
int(cw), # up sample by 4 #数据1
int(ch), # up sample by 4 #数据2
0x5B)
uart.write(data); #必须要传入一个字节数组
# 主循环
while(True):
img = sensor.snapshot()
# 检测酒红色并输出坐标
blobs_wine_red = img.find_blobs([wine_red_threshold], pixels_threshold=200, area_threshold=200)
if blobs_wine_red:
for blob in blobs_wine_red:
rect = blob.rect()
top_center_x = rect[0] + rect[2] // 2
top_center_y = rect[1]
print(f"Wine Red Top Center: ({top_center_x}, {top_center_y})")
# 检测石榴红、蓝色和绿色并输出对应数字
detected_colors = []
# 检测石榴红
blobs_pomegranate_red = img.find_blobs([pomegranate_red_threshold], pixels_threshold=200, area_threshold=200)
if blobs_pomegranate_red:
detected_colors.append(3)
# 检测蓝色
blobs_blue = img.find_blobs([blue_threshold], pixels_threshold=200, area_threshold=200)
if blobs_blue:
detected_colors.append(2)
# 检测绿色
blobs_green = img.find_blobs([green_threshold], pixels_threshold=200, area_threshold=200)
if blobs_green:
detected_colors.append(1)
# 输出检测到的颜色编号
if detected_colors:
print("Detected Colors (Numbers):", detected_colors)
# 延迟以减少处理速度
time.sleep_ms(10000)
import ustruct
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
wine_red_threshold = (52, 27, 8, 127, -30, 25) # 酒红色阈值示例
pomegranate_red_threshold = (0, 63, 41, 127, -128, 127) # 石榴红阈值示例
blue_threshold = (48, 64, -50, 2, -65, -15) # 蓝色阈值
green_threshold = (92, 59, -75, -14, 2, 35) # 绿色阈值
clock = time.clock()
uart = UART(3,19200) #定义串口3变量
uart.init(19200, bits=8, parity=None, stop=1) # init with given parameters
def find_max(blobs): #定义寻找色块面积最大的函数
max_size=0
for blob in blobs:
if blob.pixels() > max_size:
max_blob=blob
max_size = blob.pixels()
return max_blob
def sending_data(cx,cy,cw,ch):
global uart;
#frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];
#data = bytearray(frame)
data = ustruct.pack("<bbhhhhb", #格式为俩个字符俩个短整型(2字节)
0x2C, #帧头1
0x12, #帧头2
int(cx), # up sample by 4 #数据1
int(cy), # up sample by 4 #数据2
int(cw), # up sample by 4 #数据1
int(ch), # up sample by 4 #数据2
0x5B)
uart.write(data); #必须要传入一个字节数组
# 主循环
img = sensor.snapshot()
while(True):
# 检测酒红色并输出坐标
blobs_wine_red = img.find_blobs([wine_red_threshold], pixels_threshold=200, area_threshold=200)
if blobs_wine_red:
for blob in blobs_wine_red:
rect = blob.rect()
top_center_x = rect[0] + rect[2] // 2
top_center_y = rect[1]
print(f"Wine Red Top Center: ({top_center_x}, {top_center_y})")
# 检测石榴红
blobs_pomegranate_red = img.find_blobs([pomegranate_red_threshold], pixels_threshold=200, area_threshold=200)
if blobs_pomegranate_red:
detected_colors.append(3)
# 检测蓝色
blobs_blue = img.find_blobs([blue_threshold], pixels_threshold=200, area_threshold=200)
if blobs_blue:
detected_colors.append(2)
# 检测绿色
blobs_green = img.find_blobs([green_threshold], pixels_threshold=200, area_threshold=200)
if blobs_green:
detected_colors.append(1)
# 输出检测到的颜色编号
if detected_colors:
print("Detected Colors (Numbers):", detected_colors)
# 延迟以减少处理速度
time.sleep_ms(10000)
# 检测石榴红、蓝色和绿色并输出对应数字
detected_colors = []
W
w6ta 发布的帖子
-
代码运行,连接电脑调试时连接没一会就断连了,摄像头模块内问题,不知道为什么?