Пример #1
0
    def work(img):
        #tmp = image.Image(size=(160, 160))
        #tmp = tmp.to_grayscale()
        ui.canvas = ui.canvas.to_grayscale()
        bak = img.cut(100, 60, 120, 120).to_grayscale()
        #img.draw_image(tmp, (0, 0))
        ui.canvas.draw_image(bak, (320 + 20, 0 + 20))
        #img1 = tmp
        #img1 = img1.to_grayscale(1)
        #tmp = img.resize(224, 224)
        #img1 = img.to_grayscale(1)  # convert to gray
        #tmp = img1.cut(20, 20, 120, 120)  # resize to Minist input 28x28
        #ui.canvas.draw_image(tmp, (320 + 20, 160 + 20))

        a = bak.invert()  # invert picture as Minist need
        a = bak.strech_char(1)  # preprocessing pictures, eliminate dark corner
        img.draw_image(bak.resize(80, 80),
                       (0, 0))  # display small 28x28 picture # alpha=25
        bak = bak.resize(28, 28)
        a = bak.pix_to_ai()  # generate data for ai
        fmap = kpu.forward(Minist.task, bak)  # run neural network model
        plist = fmap[:]  # get result (10 digit's probability)
        pmax = max(plist)  # get max probability
        try:
            max_index = plist.index(pmax)  # get the digit
        except Exception as e:
            print(e)

        Minist.score = pmax
        Minist.result = max_index

        #ui.canvas.draw_string(0, 300, "%d: %.3f" % (max_index, pmax), scale=2)  # show result

        img.draw_rectangle(80, 40, 160, 160, thickness=2, color=(0, 255, 0))
        img.draw_rectangle(80 + 20,
                           40 + 20,
                           120,
                           120,
                           thickness=2,
                           color=(255, 0, 0))

        if ui.weight > 240:

            size = 28 * 2
            x, y = 5, int(240) + 5 - size

            #watch conv0
            a = kpu.set_layers(Minist.task, 1)
            fmap = kpu.forward(Minist.task, bak)  # run neural network model
            for i in range(0, 16):
                tmp = kpu.fmap(fmap, i)
                tmpx2 = tmp.resize(14 * 2, 14 * 2)  # scale to display
                a = ui.canvas.draw_image(tmpx2, (i % 8) * 14 * 2 + x,
                                         y + size + 14 * 2 * int(i / 8))
            x, y = x + 10 + size * 4, y + 5 - size
            #watch conv1
            a = kpu.set_layers(Minist.task, 2)
            fmap = kpu.forward(Minist.task, bak)  # run neural network model
            for i in range(0, 32):
                tmp = kpu.fmap(fmap, i)
                tmpx2 = tmp.resize(7 * 2, 7 * 2)  # scale to display
                a = ui.canvas.draw_image(
                    tmpx2, (i % 16) * 7 * 2 + x,
                    y + size + 14 * 2 * 2 + 7 * 2 * int(i / 16))
            x, y = x + 15, y + 10
            #watch conv2
            a = kpu.set_layers(Minist.task, 8)
            fmap = kpu.forward(Minist.task, bak)  # run neural network model
            for i in range(0, 10):
                tmp = kpu.fmap(fmap, i)
                tmpx2 = tmp.resize(4 * 4, 4 * 4)  # scale to display
                a = ui.canvas.draw_image(tmpx2, i * 4 * 4 + x + i * 5,
                                         y + size + 14 * 2 * 2 + 7 * 2 * 2)

            #watch softmax
            a = kpu.set_layers(Minist.task, 11)
            fmap = kpu.forward(Minist.task, bak)
            plist = fmap[:]
            for i in range(0, 10):
                cc = int(plist[i] * 256)
                ui.canvas.draw_string(i * 4 * 4 + x + 3 + i * 5,
                                      y + 20 + size + 14 * 2 * 2 + 7 * 2 * 2,
                                      '%.0f' % (plist[i] * 100),
                                      scale=1,
                                      color=(255, 255, 255))
                #print(i, cc)

        del bak

        return img
Пример #2
0
    set_backlight(0)
    print('[info]: Exit by user operation')
    sys.exit()
initialize_camera()

task = kpu.load("/sd/model/mbnet751.kmodel")

print('[info]: Started.')
but_stu = 1

fore_color = rgb888_to_rgb565(119, 48, 48)
back_color = rgb888_to_rgb565(250, 205, 137)
border_color = (back_color >> 8) | ((back_color & 0xff) << 8)

img = sensor.snapshot()
a = kpu.set_layers(task, 29)
firstmap = kpu.forward(task, img)
firstdata = firstmap[:]

try:
    while (True):
        img = sensor.snapshot()
        fmap = kpu.forward(task, img)
        plist = fmap[:]
        dist = 0
        for i in range(768):
            dist = dist + (plist[i] - firstdata[i]) * (plist[i] - firstdata[i])
        if dist < 200:
            img.draw_rectangle(1, 46, 222, 132, color=31, thickness=3)
        img.draw_string(2, 47, "%.2f " % (dist))
        lcd.display(img)
Пример #3
0
task = kpu.load(0x300000)			#load model from flash address 0x200000

img_lcd=image.Image()
img_lcd = img_lcd.to_grayscale()
while True:
    img2 = sensor.snapshot()
    #lcd.display(img,oft=(0,0))		#display large picture
    img2=img2.to_grayscale()		#convert to gray
    img2=img2.resize(28,28)			#resize to mnist input 28x28
    a=img2.invert()					#invert picture as mnist need
    # a=img2.strech_char(1)			#preprocessing pictures, eliminate dark corner
    img2x2=img2.resize(28*2,28*2)	#scale to display
    a = img_lcd.draw_image(img2x2,0,0)#display small 28x28 picture
    a=img2.pix_to_ai();				#generate data for ai
    #watch conv0
    a=kpu.set_layers(task, 1)			
    fmap=kpu.forward(task,img2)		#run neural network model
    for i in range(0,16):
        tmp=kpu.fmap(fmap,i)
        tmpx2=tmp.resize(14*2,14*2) #scale to display
        a=img_lcd.draw_image(tmpx2,(i%8)*14*2,28*2+14*2*int(i/8))
    #watch conv1
    a=kpu.set_layers(task, 2)			
    fmap=kpu.forward(task,img2)		#run neural network model
    for i in range(0,32):
        tmp=kpu.fmap(fmap,i)
        tmpx2=tmp.resize(7*2,7*2)   #scale to display
        a=img_lcd.draw_image(tmpx2,(i%16)*7*2,28*2+14*2*2+7*2*int(i/16))
    #watch conv2
    a=kpu.set_layers(task, 8)			
    fmap=kpu.forward(task,img2)		#run neural network model