def inference(feature_list, task, img): x = -1 name = "" dist = 0 for x in [0, 80, 160]: for y in [0, 60, 120]: print(x, y) gc.collect() #img2 = img.copy((x,y,160,120)) img2 = img.copy((0, 0, 320, 240)) fmap = kpu.forward(task, img2) #p = np.array(fmap[:]) kpu.fmap_free(fmap) ''' # get nearest target name,dist,v = get_nearest(feature_list, p) print("name:" + name + " dist:" + str(dist) + " v:" + str(v) + " mem:" + str(gc.mem_free())) if dist < 200: break if dist < 200: break ''' #img2 = img.copy((x,y,160,120)) fmap = kpu.forward(task, img) p = np.array(fmap[:]) kpu.fmap_free(fmap) # get nearest target name, dist, v = get_nearest(feature_list, p) print("name:" + name + " dist:" + str(dist) + " v:" + str(v) + " mem:" + str(gc.mem_free())) print(x) return name, dist, x
def mnist_run(img, dx, dy, dis, x00=0, y00=80, nnn=2): if nnn == 4: x00 = x00 dy = dy img0 = img.copy((x00 + dis * nnn, y00 + nnn * 0, dx, dy)) #img0.mean(2, threshold=True, offset=1, invert=True) #A img0.median(2, percentile=0.3, threshold=True, offset=-3, invert=True) #img0.midpoint(2, bias=0.3, threshold=True, offset=0, invert=True) #img0.mode(2, threshold=True, offset=0, invert=True) #B #img0.binary([(110,255)], invert = True) for dx0 in range(dx): for dy0 in range(dy): a0 = img0.get_pixel(dx0, dy0) img.set_pixel(x00 + dis * nnn + dx0, y00 + nnn * 0 + dy0, a0) #img1 = img0.copy((1,1, dx-1, dy-1)) img1 = img0 img1 = img1.resize(28, 28) img1 = img1.to_grayscale(1) img1.pix_to_ai() fmap = kpu.forward(task, img1) plist = fmap[:] pmax = max(plist) max_index = plist.index(pmax) kpu.fmap_free(fmap) return max_index, pmax
def wizard(task): delay(1, '0') play_sound("/sd/oshaku/start_learn.wav") free() delay(1500, '0') for ang in ["0","45","90","135","non"]: play_sound("/sd/oshaku/" + str(ang) + ".wav") delay(1000, ang) for i in ["3","2","1"]: play_sound("/sd/oshaku/" + str(i) + ".wav") delay(700, ang) play_sound("/sd/oshaku/kacha.wav") img = sensor.snapshot() feature = get_feature(task, img) free() feature_list.append([ang,feature]) save(feature_file, feature_list) drawHeader(img, "Point the camera") drawAngle(img, ang) drawFooter(img, "Saved " + ang + " degrees") lcd.display(img) free() play_sound("/sd/oshaku/set.wav") kpu.fmap_free(feature)
feature_100 = [] save(feature_file, feature_list) br.play_sound("/sd/reset.wav") else: br.play_sound("/sd/camera.wav") feature = get_feature(task) feature_list.append([name,feature[:]]) if name=='0': feature_0.append([name,feature[:]]) if name=='100': feature_100.append([name,feature[:]]) save(feature_file, feature_list) br.play_sound("/sd/set.wav") gc.collect() # print(gc.mem_free()) kpu.fmap_free(feature) print("[QR]: " + name) continue # inference fmap = kpu.forward(task, img) plist=fmap[:] clock.tick() if len(feature_0)>0 and len(feature_100)>0: p = plist f0 = feature_0[0] f100 = feature_100[0] dist = 100.0 * get_dist(f0[1],f100[1],p) dist_str = "%.1f"%(dist) print("[DISTANCE]: " + dist_str) img.draw_string(2,47, dist_str,scale=3)
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) dist_int = int(dist) print(dist_int) img_size1 = (dist_int & 0xFF0000) >> 16 img_size2 = (dist_int & 0x00FF00) >> 8 img_size3 = (dist_int & 0x0000FF) >> 0 data_packet = bytearray([ 0xFF, 0xD8, 0xEA, 0x01, img_size1, img_size2, img_size3, 0x00, 0x00, 0x00 ]) uart_Port.write(data_packet) if but_b.value() == 0: firstmap = kpu.forward(task, img) firstdata = firstmap[:] kpu.fmap_free(fmap) except KeyboardInterrupt: kpu.deinit(task) sys.exit()
color=(0, 144, 255), thickness=10) img.draw_string(50, 55, "Class:%d" % (class_num, ), color=(255, 255, 255), scale=1) lcd.display(img) img.pix_to_ai() #print(img) feature = get_feature(task, img) del img l, qvec = quantize_vector(feature[:]) feature_list.append((l, qvec, class_num)) gc.collect() kpu.fmap_free(feature) lcd.clear() for l1, qvec1, class_num in feature_list: similarities = "" for l2, qvec2, _ in feature_list: dist = get_cos_distance(l1, qvec1, l2, qvec2) similarities += "%0.2f " % (dist, ) print(class_num, similarities) sp_device = None if should_connect_spike_prime: show_message("Connecting to LPF2 Hub...", x=100, bg_color=lcd.BLACK)