logo

关于OpenMV文件写入的问题

2019-08-01 08:57
2
_2zqg
82526cc3-d62f-480d-84d0-cc667dd1a853.png
while(True):
    clock.tick()
    img = sensor.snapshot()
    f = open("test.txt")
    for tag in img.find_apriltags(families=tag_families): # defaults to TAG36H11 without "families".
        img.draw_rectangle(tag.rect(), color = (255, 0, 0))
        img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0))
        print_args = (family_name(tag), tag.id(), (180 * tag.rotation()) / math.pi)
        id = tag.id()
        f.write(id)
2019-08-01 12:25
I
网红小智智 星瞳科技

请提供全部的代码,否则我没办法测试。

2019-08-01 12:33
2
_2zqg

import sensor, image, time, math, pyb

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) # we run out of memory if the resolution is much bigger...
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)  # must turn this off to prevent image washout...
sensor.set_auto_whitebal(False)  # must turn this off to prevent image washout...
clock = time.clock()

tag_families = 0
tag_families |= image.TAG16H5 # comment out to disable this family
tag_families |= image.TAG25H7 # comment out to disable this family
tag_families |= image.TAG25H9 # comment out to disable this family
tag_families |= image.TAG36H10 # comment out to disable this family
tag_families |= image.TAG36H11 # comment out to disable this family (default family)
tag_families |= image.ARTOOLKIT # comment out to disable this family

def family_name(tag):
    if(tag.family() == image.TAG16H5):
        return "TAG16H5"
    if(tag.family() == image.TAG25H7):
        return "TAG25H7"
    if(tag.family() == image.TAG25H9):
        return "TAG25H9"
    if(tag.family() == image.TAG36H10):
        return "TAG36H10"
    if(tag.family() == image.TAG36H11):
        return "TAG36H11"
    if(tag.family() == image.ARTOOLKIT):
        return "ARTOOLKIT"

while(True):
    clock.tick()
    img = sensor.snapshot()
    f = open("test.txt")
    for tag in img.find_apriltags(families=tag_families): # defaults to TAG36H11 without "families".
        img.draw_rectangle(tag.rect(), color = (255, 0, 0))
        img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0))
        print_args = (family_name(tag), tag.id(), (180 * tag.rotation()) / math.pi)
        #print("Tag Family %s, Tag ID %d, rotation %f (degrees)" % print_args)
        id = tag.id()
        f.write(id)        
    #print(clock.fps())
2
_2zqg

@kidswong999 麻烦了

2019-08-01 18:19
I
网红小智智 星瞳科技

需要把id从数字变为字符串,然后写入文件。

f.write(str(id))

PS:你这个代码。。。只open,不close啊。

2019-08-01 20:24
2
_2zqg
285c22be-0b4a-44bc-8666-ced96e8f7243.png
2019-08-01 20:29
I
网红小智智 星瞳科技

f = open("test.txt",mode='wt')

https://cn.bing.com/search?q=python3%E5%86%99%E5%85%A5%E6%96%87%E4%BB%B6&qs=n&form=QBLH&sp=-1&pq=python3%E5%86%99%E5%85%A5wen&sc=0-12&sk=&cvid=D6664F461FB04FA08698FA20A8A4091F

2019-08-01 20:40
2
_2zqg

@kidswong999 感谢