我认为你没有写入数据,是因为没有识别到色块。
写文件的时候断电容易造成文件系统故障。
分开保存会好一些:
import sensor, image, time
# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 10)
sensor.set_auto_whitebal(False) # 关闭自动白平衡
sensor.set_auto_gain(False) # 关闭自动增益
# 定义颜色识别参数
thresholds = [(0, 94, 51, 13, -65, 80)] # 根据实际情况调整
# 打开文件准备写入数据
num = 0
while(True):
img = sensor.snapshot()
# 查找目标颜色
blobs = img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200)
with open("object_center_%d.txt" % num, "w") as file: # 使用绝对路径保存到根目录
if blobs:
# 找到目标,计算中心坐标
for blob in blobs:
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
file.write("({}, {})\n".format(blob.cx(), blob.cy()))
num += 1
print(num)