logo

用openmv做模板识别特别卡啊

2022-06-25 23:15
8
dymb

应该怎么解决 啊大佬

import time, sensor, image
from image import SEARCH_EX, SEARCH_DS

# Reset sensor
sensor.reset()

# Set sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QQVGA)
# You can set windowing to reduce the search image.
#sensor.set_windowing(((640-80)//2, (480-60)//2, 80, 60))
sensor.set_pixformat(sensor.GRAYSCALE)
from pyb import UART
uart = UART(3, 115200)
# Load template.
# Template should be a small (eg. 32x32 pixels) grayscale image.
template1 = image.Image("/1.pgm")
template2 = image.Image("/2.pgm")
template3 = image.Image("/3.pgm")
template4 = image.Image("/4.pgm")
template5 = image.Image("/5.pgm")
template6 = image.Image("/6.pgm")
template7 = image.Image("/7.pgm")
template8 = image.Image("/8.pgm")
template9 = image.Image("/9.pgm")


template14 = image.Image("/m.pgm")



template18 = image.Image("/1L2.pgm")
template19 = image.Image("/1L3.pgm")

template21 = image.Image("/1R2.pgm")
template22 = image.Image("/1R3.pgm")


template25 = image.Image("/2L2.pgm")
template26 = image.Image("/2L3.pgm")


template29 = image.Image("/2R2.pgm")
template30 = image.Image("/3L1.pgm")
template31 = image.Image("/3L2.pgm")

template33 = image.Image("/3R1.pgm")
template34 = image.Image("/3R2.pgm")


template37 = image.Image("/4L1.pgm")
template38 = image.Image("/4L2.pgm")

template40 = image.Image("/4R1.pgm")
template41 = image.Image("/4R1.pgm")

flag=0
flag1=0



clock = time.clock()


# Run template matching
while (True):
    clock.tick()
    img = sensor.snapshot()



    # find_template(template, threshold, [roi, step, search])
    # ROI: The region of interest tuple (x, y, w, h).
    # Step: The loop step used (y+=step, x+=step) use a bigger step to make it faster.
    # Search is either image.SEARCH_EX for exhaustive search or image.SEARCH_DS for diamond search
    #
    # Note1: ROI has to be smaller than the image and bigger than the template.
    # Note2: In diamond search, step and ROI are both ignored.
    r1 = img.find_template(template1, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r2 = img.find_template(template2, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r3 = img.find_template(template3, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r4 = img.find_template(template4, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r5 = img.find_template(template5, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r6 = img.find_template(template6, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r7 = img.find_template(template7, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r8 = img.find_template(template8, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r9 = img.find_template(template9, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))

    
    r14= img.find_template(template14, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))



    r18= img.find_template(template18, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r19= img.find_template(template19, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))

    r22= img.find_template(template22, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))


    r25= img.find_template(template25, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r26= img.find_template(template26, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))


    r29= img.find_template(template29, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r30= img.find_template(template30, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r31= img.find_template(template31, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r33= img.find_template(template33, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))


    
    r37= img.find_template(template37, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r38= img.find_template(template38, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))

    r40= img.find_template(template40, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r41= img.find_template(template41, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
 
    if flag1==0:
        if r14:
            uart.write("s")
            flag1=1
       
        
    if flag==0:
        if r1:
            img.draw_rectangle(r1)
            flag=1
            uart.write("1")

        if r2:
            img.draw_rectangle(r2)
            flag=2
            uart.write("2")

        if r3:
            img.draw_rectangle(r3)
            flag=3
            uart.write("3")

        if r4:
            img.draw_rectangle(r4)
            flag=4
            uart.write("4")

        if r5:
            img.draw_rectangle(r5)
            uart.write("5")


        if r6:
            img.draw_rectangle(r6)
            uart.write("6")

        if r7:
            img.draw_rectangle(r7)
            uart.write("7")

        if r8:
             img.draw_rectangle(r8)
             uart.write("8")

        if r9:
             img.draw_rectangle(r9)
             uart.write("9")
        
        
        
            

    if flag==1:

        if r14:
            if r18 or r19:
                uart.write("l")
            if r22:
                uart.write("r")
            else:
                uart.write("m")
                import time
                time.sleep(2)
                flag1=0            
        
            
            
    if flag==2:
        if r14:
            if r25 or r26:
                img.draw_rectangle("l")
            if r29:
                uart.write("r")
            else:
                uart.write("m") 
                import time
                time.sleep(2)
                flag1=0
        
            
    if flag==3:
        if r14:
            if r30 or r31:
                img.draw_rectangle("l")
            if r33:
                uart.write("r")
            else:
                uart.write("m") 
                import time
                time.sleep(2)
                flag1=0
            
    if flag==4:
        if r14:
            if r37 or r38:
                uart.write("l")
            if r40 or r41:
                uart.write("r")
            else:
                uart.write("m")
                import time
                time.sleep(2)
                flag1=0 
        



        print(clock.fps())
2022-06-26 19:11
I
网红小智智 星瞳科技

你调用那么多次,当然慢啊。

2022-06-28 12:24
8
dymb

@kidswong999 那那您知道怎么让他变不卡吗,我看好像说是释放内存,但是怎么释放 啊

2022-06-30 12:46
I
网红小智智 星瞳科技

调用一次就耗费一次的时间,和内存没关系。

没有解决办法,只能减少调用的次数。