def __lazy_init(self): err_counter = 0 while 1: try: sensor.reset() # Reset sensor may failed, let's try sometimes break except Exception: err_counter = err_counter + 1 if err_counter == 20: lcd.draw_string(lcd.width() // 2 - 100, lcd.height() // 2 - 4, "Error: Sensor Init Failed", lcd.WHITE, lcd.RED) time.sleep(0.1) continue print("progress 1 OK!") sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) # QVGA=320x240 sensor.run(1) print("progress 2 OK!") self.task = kpu.load(0x300000) # Load Model File from Flash anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025) # Anchor data is for bbox, extracted from the training sets. print("progress 3 OK!") kpu.init_yolo2(self.task, 0.5, 0.3, 5, anchor) self.but_stu = 1 self.__initialized = True
def __init__(self, categoryname=[], samplesnumber=15, bool=False): self.row = global_value.row global_value.row = global_value.row + 1 try: del self.model except Exception: pass try: del self.classifier except Exception: pass self.classnumber = len(categoryname) self.samplesnumber = samplesnumber self.categoryname = categoryname self.img_copy = None self.cap_num = 0 self.train_status = 0 self.class_list = [] self.percent = 0 self.image_class = 0 if bool: self.model = kpu.load(0x514000) self.classifier = kpu.classifier(self.model, self.classnumber, self.samplesnumber) else: pass
def on_draw(self): if not self.__initialized: self.__lazy_init() try: while True: img = sensor.snapshot() # Take an image from sensor print("progress 4 OK!") # Run the detection routine bbox = kpu.run_yolo2(self.task, img) if bbox: for i in bbox: print(i) img.draw_rectangle(i.rect()) lcd.display(img) home_button = self.get_system().home_button # TODO led_w = self.get_system().led_w if home_button.value() == 0 and self.but_stu == 1: if led_w.value() == 1: led_w.value(0) else: led_w.value(1) self.but_stu = 0 if home_button.value() == 1 and self.but_stu == 0: self.but_stu = 1 except KeyboardInterrupt: a = kpu.deinit(task) sys.exit()
def free(): try: if FaceReco.is_load: tmp = kpu.deinit(FaceReco.task_fd) tmp = kpu.deinit(FaceReco.task_ld) tmp = kpu.deinit(FaceReco.task_fe) #t, FaceReco.task_fd = FaceReco.task_fd, None #del t #t, FaceReco.task_ld = FaceReco.task_ld, None #del t #t, FaceReco.task_fe = FaceReco.task_fe, None #del t t, FaceReco.img_face = FaceReco.img_face, None del t FaceReco.record_ftr = [] FaceReco.record_ftrs = [] button_io.home_button.disirq() FaceReco.start_processing = False FaceReco.is_load = False gc.collect() except Exception as e: print(e) # see py_kpu_deinit error will mp_raise_TypeError
def __init__(self, detector): self.object_detected = None self.FaceDetector = detector self.landmark_task = kpu.load(0x6BD000) a = kpu.set_outputs(self.landmark_task, 0, 1, 1, 10)
def main(labels=None, model_addr="/sd/m.kmodel", sensor_window=(224, 224), lcd_rotation=0, sensor_hmirror=False, sensor_vflip=False): sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.set_windowing(sensor_window) sensor.set_hmirror(sensor_hmirror) sensor.set_vflip(sensor_vflip) sensor.run(1) lcd.init(type=1) lcd.rotation(lcd_rotation) lcd.clear(lcd.WHITE) if not labels: with open('labels.txt', 'r') as f: exec(f.read()) if not labels: print("no labels.txt") img = image.Image(size=(320, 240)) img.draw_string(90, 110, "no labels.txt", color=(255, 0, 0), scale=2) lcd.display(img) return 1 try: img = image.Image("startup.jpg") lcd.display(img) except Exception: img = image.Image(size=(320, 240)) img.draw_string(90, 110, "loading model...", color=(255, 255, 255), scale=2) lcd.display(img) task = kpu.load(model_addr) try: while (True): img = sensor.snapshot() t = time.ticks_ms() fmap = kpu.forward(task, img) t = time.ticks_ms() - t plist = fmap[:] pmax = max(plist) max_index = plist.index(pmax) img.draw_string(0, 0, "%.2f : %s" % (pmax, labels[max_index].strip()), scale=2) img.draw_string(0, 200, "t:%dms" % (t), scale=2) lcd.display(img) except Exception as e: raise e finally: kpu.deinit(task)
def compute_features(self, register=False): img, src_point, self.roi = self.LandmarkDetector.detect_landmarks( 0.4, return_img=True) self.max_score = 0 self.id = -1 if src_point: # align face to standard position a = img.pix_to_ai() T = image.get_affine_transform(src_point, self.dst_point) a = image.warp_affine_ai(img, self.img_face, T) a = self.img_face.ai_to_pix() if register: reg_img = image.Image('logo.jpg') a = reg_img.draw_image( self.img_face, (lcd.width() // 2 - 68, lcd.height() // 2 - 20)) a = reg_img.draw_string(30, lcd.height() // 2 - 48, "Registring face", color=(0, 255, 0), scale=2, mono_space=False) a = lcd.display(reg_img) del (reg_img) time.sleep(2) a = self.img_face.pix_to_ai() # calculate face feature vector fmap = kpu.forward(self.fe_task, self.img_face) #print(fmap[:]) vector = list(map(lambda x: x / 256, fmap[:])) self.feature = kpu.face_encode(vector) for id in self.db.keys(): entry = _unhexlify(self.db[id]['vector']) score = kpu.face_compare(entry, self.feature) if score > self.max_score: self.max_score = score name = self.db[id]['name'] self.id = id if not self.max_score > self.threshold: name = 'Unknown' a = img.draw_rectangle(self.roi, color=(0x1c, 0xa2, 0xff), thickness=2) a = img.draw_string(self.roi[0], self.roi[1] - 14, ("%s %%: %.2f" % (name, self.max_score)), color=(255, 255, 255), scale=1.5, mono_space=False) a = lcd.display(img) del (img)
def __init__(self, out_range=10, ignore_limit=0.02, hmirror=False, vflip=False, lcd_rotation=2, lcd_mirror=True): self.pitch = 0 self.roll = 0 self.out_range = out_range self.ignore = ignore_limit self.task_fd = kpu.load(0x300000) # face model addr in flash anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025) kpu.init_yolo2(self.task_fd, 0.5, 0.3, 5, anchor) lcd.init() lcd.rotation(lcd_rotation) lcd.mirror(lcd_mirror) sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) if hmirror: sensor.set_hmirror(1) if vflip: sensor.set_vflip(1)
def load(): if FaceReco.is_load == False: FaceReco.model = kpu.load(0x2C0000) # Load Model File from Flash # Anchor data is for bbox, extracted from the training sets. kpu.init_yolo2(FaceReco.model, 0.5, 0.3, 5, (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)) FaceReco.is_load = True
def load(): if MaybeIs.is_load == False: #print(MaybeIs.load) MaybeIs.task = kpu.load(0x5C0000) #task = kpu.load("/sd/0x5C0000_20class.kmodel") kpu.init_yolo2(MaybeIs.task, 0.5, 0.3, 5, (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)) MaybeIs.is_load = True
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 free(): #print(HowMany.free) try: if HowMany.is_load: kpu.deinit(HowMany.task) HowMany.is_load = False except Exception as e: print(e) # see py_kpu_deinit error will mp_raise_TypeError
def free(): #print(MaybeIs.free) try: if MaybeIs.is_load: kpu.deinit(MaybeIs.task) MaybeIs.is_load = False except Exception as e: print(e) # see py_kpu_deinit error will mp_raise_TypeError
def cleanup(self): self.close_recorder() try: uos.umount(self._ramdisk_mount_point) except OSError as e: print(e) if self._task: kpu.deinit(self._task) self._task = None
def updateKpu(): global g_cFiler global g_selCnt global g_cWav global g_task global g_powArr info = g_cFiler.getInfoList()[g_selCnt] if (g_task == None): g_task = _resetTask() if (g_task == None): g_cWav.play('/sd/snd/sys_ng.wav') g_cWav.wait() img = sensor.snapshot() if (info.modelType == 'yolo2'): plist = [] for id in range(0, len(info.classList)): plist.append(0.0) code_obj = kpu.run_yolo2(g_task, img) if code_obj: # object detected for i in code_obj: rect_size = i.w() * i.h() if rect_size > 10: print(len(plist)) print(i.classid()) plist[i.classid()] = 0.95 else: fmap = kpu.forward(g_task, img, False) plist = fmap[:] colArr = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (5, 5, 5), (0, 255, 255), (255, 255, 0), (128, 128, 128), (50, 200, 50)] for id in range(0, len(plist)): if (plist[id] > 0.9): g_powArr[id] = min((g_powArr[id] + plist[id] - 0.9) * 5.0, 100.0) else: g_powArr[id] -= 10.0 g_powArr[id] = max(g_powArr[id], 0.0) img.draw_rectangle((10, 50 + 10 * id, int(g_powArr[id] * 1), 8), colArr[id & 7], 10, True) if (g_powArr[id] >= 100.0): g_powArr[id] = 0.0 info = g_cFiler.getInfoList()[g_selCnt] labels = info.classList wavPath = info.dirName + '/' + labels[id] + '.wav' lcd.draw_string(0, 20, wavPath) g_cWav.play('/sd/models/' + wavPath) g_cWav.wait() a = lcd.display(img) g_cButton.update()
def run_kpu(): while True: print("KPU : fetch data from Camera") img = sensor.snapshot() kpu_img = img.copy((0, 60, 160, 60)) print("KPU : run kpu") a = kpu.forward(task, kpu_img) print("KPU : fetch data from kpu") output_steer = kpu.get_output(task, 0) output_throttle = kpu.get_output(task, 1) print("KPU : Data", output_steer, output_throttle) time.sleep_ms(1)
def __init__(self, FileName, Label, bool=False): self.row = global_value.row global_value.row = global_value.row + 1 self.percent = 0 self.image_class = 0 self.file_name = FileName self.labels = Label if bool: self.image_objects_task = kpu.load(self.file_name) a = kpu.set_outputs(self.image_objects_task, 0, 1, 1, len(self.labels)) else: pass
def init_kpu(threshold=0.3): classes = ["person"] task = kpu.load( 0x300000 ) #change to "/sd/name_of_the_model_file.kmodel" if loading from SD card a = kpu.set_outputs( task, 0, 7, 7, 30 ) #the actual shape needs to match the last layer shape of your model(before Reshape) anchor = (0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828) a = kpu.init_yolo2( task, threshold, 0.3, 5, anchor ) #tweak the second parameter if you're getting too many false positives return task
def __init__(self): self.object_detected = None self.percent = 0 self.classes = ['face'] self.anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025) self.x_center = 0 self.y_center = 0 self.object_detection_task = kpu.load(0x659000) a = kpu.set_outputs(self.object_detection_task, 0, 7, 7, 30) a = kpu.init_yolo2(self.object_detection_task, 0.1, 0.1, 5, self.anchor)
def detect_landmarks(self, threshold, return_img=False): img, roi = self.FaceDetector.detect_objects(threshold, True) self.kpts = [[-1, -1] for x in range(5)] if roi: face_cut = img.cut(*roi) face_cut_128 = face_cut.resize(128, 128) a = face_cut_128.pix_to_ai() fmap = kpu.forward(self.landmark_task, face_cut_128) plist = fmap[:] le = (roi[0] + int(plist[0] * roi[2] + 5), roi[1] + int(plist[1] * roi[3] + 5)) re = (roi[0] + int(plist[2] * roi[2]), roi[1] + int(plist[3] * roi[3] + 5)) n = (roi[0] + int(plist[4] * roi[2]), roi[1] + int(plist[5] * roi[3])) lm = (roi[0] + int(plist[6] * roi[2]), roi[1] + int(plist[7] * roi[3])) rm = (roi[0] + int(plist[8] * roi[2]), roi[1] + int(plist[9] * roi[3])) self.kpts = [le, re, n, lm, rm] del (face_cut) del (face_cut_128) if return_img: return img, self.kpts, roi a = img.draw_cross(le[0], le[1], color=(0, 255, 0), size=5, thickness=3) a = img.draw_cross(re[0], re[1], color=(0, 255, 0), size=5, thickness=3) a = img.draw_cross(n[0], n[1], color=(0, 255, 0), size=5, thickness=3) a = img.draw_cross(lm[0], lm[1], color=(0, 255, 0), size=5, thickness=3) a = img.draw_cross(rm[0], rm[1], color=(0, 255, 0), size=5, thickness=3) a = lcd.display(img) return img, None, None
def Init_Task(): global labels global objects objects = kpu.load("/sd/laji.kmodel") f = open("anchors.txt", "r") anchor_txt = f.read() L = [] for i in anchor_txt.split(","): L.append(float(i)) anchor = tuple(L) f.close() a = kpu.init_yolo2(objects, 0.6, 0.3, 5, anchor) f = open("classes.txt", "r") labels_txt = f.read() labels = labels_txt.split(",") f.close()
def measure_fps(model_file): task = kpu.load(model_file) kpu.set_outputs(task, 0, 1, 1, 2) clock = time.clock() fps_ = [] for i in range(20): img = sensor.snapshot() clock.tick() fmap = kpu.forward(task, img) lcd.display(img, oft=(0, 0)) fps_.append(clock.fps()) average_fps = sum(fps_) / len(fps_) print(average_fps) global fps_result fps_result = average_fps _ = kpu.deinit(task)
def free(): try: if FaceReco.is_load: tmp = kpu.deinit(FaceReco.model) FaceReco.is_load = False except Exception as e: print(e) # see py_kpu_deinit error will mp_raise_TypeError
def free(): mem = gc.mem_free() b = gc.mem_free() gc.collect() a = gc.mem_free() print("gc %d -> %d %d" % (b, a, len(feature_list))) print(kpu.memtest())
def get_target_err(self): img = sensor.snapshot() code = kpu.run_yolo2(self.task_fd, img) if code: max_area = 0 max_i = 0 for i, j in enumerate(code): a = j.w() * j.h() if a > max_area: max_i = i max_area = a img = img.draw_rectangle(code[max_i].rect()) self.pitch = (code[max_i].y() + code[max_i].h() / 2) / 240 * self.out_range * 2 - self.out_range self.roll = (code[max_i].x() + code[max_i].w() / 2) / 320 * self.out_range * 2 - self.out_range # limit if abs(self.pitch) < self.out_range * self.ignore: self.pitch = 0 if abs(self.roll) < self.out_range * self.ignore: self.roll = 0 img = img.draw_cross(160, 120) lcd.display(img) return (self.pitch, self.roll) else: img = img.draw_cross(160, 120) lcd.display(img) return (0, 0)
def main(): ## main task, graph, counter, uart = init(threshold=0.5, patience=4) frame_idx = 0 try: while (True): # get image img = sensor.snapshot().rotation_corr(z_rotation=0.0) # detect boxes a = img.pix_to_ai() code = kpu.run_yolo2(task, img) # set frame currF = Frame(frame_idx, img, code) # calc track diff_idx = graph.track(currF) # counting counter.vanish_update(graph.F_list[-1].bboxes, graph.F_list[-2].bboxes, graph.is_decrease) counter.count() # display on IDE #img = currF.draw_frames() #img = img.copy((32, 32, 160, 160)) #img.draw_string(0 ,0, str(counter.counter[LEFT])+","+str(counter.counter[RIGHT]), #color=(0,255,0), scale=3) #a = lcd.display(img) # to Gray msg = str(counter.counter[LEFT])+ DIV + \ str(counter.counter[RIGHT])+ DIV + str(currF.num_object) _ = uart.write(msg) #_ = uart.write(img) print(counter.counter) # finalize frame_idx += 1 time.sleep(0.05) except Exception as e: # need delete kpu_task when keyboard interrupt a = kpu.deinit(task) del task gc.collect() print(e)
def load_model_file(self, filename=''): self.model = kpu.load(0x514000) classifier = kpu.classifier.load(self.model, filename) self.classifier = classifier[0] f = open(filename.split('.')[0] + '.names', 'r') self.class_list = ujson.loads(f.read()) #print(self.class_list) f.close()
def work(img): img.pix_to_ai() # Run the detection routine FaceReco.bbox = kpu.run_yolo2(FaceReco.model, img) if FaceReco.bbox: for i in FaceReco.bbox: # print(i) img.draw_rectangle(i.rect())
def inference(model_file): task = kpu.load(model_file) kpu.set_outputs(task, 0, 1, 1, 2) clock = time.clock() while (True): img = sensor.snapshot() clock.tick() fmap = kpu.forward(task, img) fps = clock.fps() plist = fmap[:] pmax = max(plist) max_index = plist.index(pmax) a = lcd.display(img, oft=(0, 0)) lcd.draw_string( 0, 128, "%.2f:%s " % (pmax, labels[max_index].strip())) _ = kpu.deinit(task)
def get_feature(task): img = sensor.snapshot() img.draw_rectangle(1, 46, 222, 132, color=(255, 0, 0), thickness=3) lcd.display(img) feature = kpu.forward(task, img) print('get_feature') gc.collect() return np.array(feature[:])