def seeotheritems(): #7second delay global taskfe global a global task global yolonum global anchor classes = [ 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor' ] anchored = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52) kpu.deinit(taskfe) kpu.deinit(task) tasktw = kpu.load("/sd/model/20class.kmodel") uart_B.write(" loaded 20 ") kpu.init_yolo2(tasktw, 0.5, 0.3, 5, anchored) imgother = sensor.snapshot() imgother.pix_to_ai() detectcode = kpu.run_yolo2(tasktw, imgother) if detectcode: led_r.value(0) led_b.value(0) for i in detectcode: imgother = imgother.draw_rectangle(i.rect()) for i in detectcode: imgother = imgother.draw_string(i.x(), i.y(), str(classes[i.classid()]), color=(255, 250, 250)) imgother = imgother.draw_string(i.x(), i.y() + 12, '%f1.3' % i.value(), color=(255, 250, 250)) imgother.save("/sd/yoloimages/" + str(yolonum) + ".jpg", quality=70) utime.sleep_ms(50) yolonum += 1 uart_B.write(" |Yolo|> " + str(classes[i.classid()]) + " <||") f = open("/sd/printoutput.txt", "a+") f.write("Yolo detected: " + str(classes[i.classid()]) + "\n\r") f.close() del (imgother) kpu.deinit(tasktw) del (tasktw) gc.collect() uart_B.write(" killed ") task = kpu.load("/sd/facedetect.kmodel") taskfe = kpu.load("/sd/model/FE.smodel") utime.sleep_ms(10) led_r.value(1) led_b.value(1) kpu.init_yolo2(task, 0.5, 0.3, 5, anchor) uart_B.write(" restarted ")
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 main(model_addr=0x300000, lcd_rotation=0, sensor_hmirror=False, sensor_vflip=False): sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) 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) task = kpu.load(model_addr) anchors = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025) kpu.init_yolo2(task, 0.5, 0.3, 5, anchors) # threshold:[0,1], nms_value: [0, 1] try: while(True): img = sensor.snapshot() t = time.ticks_ms() objects = kpu.run_yolo2(task, img) t = time.ticks_ms() - t if objects: for obj in objects: img.draw_rectangle(obj.rect()) img.draw_string(0, 200, "t:%dms" %(t), scale=2) lcd.display(img) except Exception as e: sys.print_exception(e) finally: kpu.deinit(task)
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 __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 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 __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 __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 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 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 load(): if MoblieNet.is_load == False: #print(MoblieNet.load) MoblieNet.task = kpu.load(0x720000) #task = kpu.load("/sd/0x720000_mbnet75.kmodel") with open(os.getcwd() + '/res/labels.txt', 'r') as f: #with open('labels.txt', 'r') as f: MoblieNet.labels = f.readlines() MoblieNet.is_load = True
def load(): if FaceReco.is_load == False: FaceReco.task_fd = kpu.load(0x2C0000) FaceReco.task_ld = kpu.load(0x580000) FaceReco.task_fe = kpu.load(0x340000) a = kpu.init_yolo2(FaceReco.task_fd, 0.5, 0.3, 5, anchor) FaceReco.img_face = image.Image(size=(128, 128)) a = FaceReco.img_face.pix_to_ai() FaceReco.start_processing = False from Maix import GPIO button_io.home_button.irq( FaceReco.set_key_state, GPIO.IRQ_RISING, GPIO.WAKEUP_NOT_SUPPORT) FaceReco.is_load = True
def __init__(self): self._m_fd = kpu.load(0x200000) self._m_ld = kpu.load(0x300000) self._m_fe = kpu.load(0x400000) self._anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025) self._dst_point = [(44, 59), (84, 59), (64, 82), (47, 105), (81, 105)] self.names = [] self.features = [] _ = kpu.init_yolo2(self._m_fd, 0.5, 0.3, 5, self._anchor) self.img_face = image.Image(size=(128, 128)) _ = self.img_face.pix_to_ai() sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) # sensor.set_hmirror(1) sensor.set_vflip(1) self.show_img_timeout = 5 self._show_img_t = -5
def main(anchors, labels=None, model_addr="/sd/m.kmodel"): sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.set_windowing((224, 224)) sensor.run(1) lcd.init(type=1) 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) kpu.init_yolo2(task, 0.5, 0.3, 5, anchors) # threshold:[0,1], nms_value: [0, 1] try: while (True): img = sensor.snapshot() t = time.ticks_ms() objects = kpu.run_yolo2(task, img) t = time.ticks_ms() - t if objects: for obj in objects: pos = obj.rect() img.draw_rectangle(pos) img.draw_string(pos[0], pos[1], "%s : %.2f" % (labels[obj.classid()], obj.value()), scale=2) img.draw_string(0, 200, "t:%dms" % (t), scale=2) lcd.display(img) except Exception as e: sys.print_exception(e) finally: kpu.deinit(task)
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 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 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 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 measure_latency(model_file): task = kpu.load(model_file) kpu.set_outputs(task, 0, 1, 1, 2) clock = time.clock() latency_ = [] for i in range(20): img = sensor.snapshot() clock.tick() t1 = time.ticks_us() fmap = kpu.forward(task, img) t2 = time.ticks_diff(time.ticks_us(), t1) / 1000 lcd.display(img, oft=(0, 0)) latency_.append(t2) average_latency = sum(latency_) / len(latency_) print(average_latency) global latency_result latency_result = average_latency _ = kpu.deinit(task)
def __init__(self, FileName, Classes, Anchor, bool=False): self.row = global_value.row global_value.row = global_value.row + 1 self.object_detected = None self.percent = 0 self.file_name = FileName self.classes = Classes self.anchor = Anchor self.x_center = 0 self.y_center = 0 if bool: self.object_detected_task = kpu.load(self.file_name) a = kpu.set_outputs(self.object_detected_task, 0, 7, 7, 5 * (5 + len(self.classes))) a = kpu.init_yolo2(self.object_detected_task, 0.1, 0.3, 5, self.anchor) else: pass
def find_face(): lcd.init() sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.set_vflip(1) sensor.run(1) task = kpu.load(0x300000) anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025) kpu.init_yolo2(task, 0.5, 0.3, 5, anchor) while (True): img = sensor.snapshot() code = kpu.run_yolo2(task, img) if code: for i in code: img.draw_rectangle(i.rect()) lcd.display(img) kpu.deinit(task)
def main(model_addr=0x300000, lcd_rotation=0, sensor_hmirror=False, sensor_vflip=False): try: sensor.reset() except Exception as e: raise Exception( "sensor reset fail, please check hardware connection, or hardware damaged! err: {}" .format(e)) sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) 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) anchors = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025) try: task = None task = kpu.load(model_addr) kpu.init_yolo2(task, 0.5, 0.3, 5, anchors) # threshold:[0,1], nms_value: [0, 1] while (True): img = sensor.snapshot() t = time.ticks_ms() objects = kpu.run_yolo2(task, img) t = time.ticks_ms() - t if objects: for obj in objects: img.draw_rectangle(obj.rect()) img.draw_string(0, 200, "t:%dms" % (t), scale=2) lcd.display(img) except Exception as e: raise e finally: if not task is None: kpu.deinit(task)
def __init__(self, lm_detector): self.id = -1 self.max_score = 0 self.threshold = 85 self.LandmarkDetector = lm_detector self.db = _read_db() offset_x = 0 offset_y = -15 self.dst_point = [(44 + offset_x, 59 + offset_y), (84 + offset_x, 59 + offset_y), (64 + offset_x, 82 + offset_y), (47 + offset_x, 105), (81 + offset_x, 105)] self.img_face = image.Image(size=(128, 128)) a = self.img_face.pix_to_ai() self.fe_task = kpu.load(0x708000) a = kpu.set_outputs(self.fe_task, 0, 1, 1, 128)
def _resetTask(): global g_selCnt global g_cFiler global g_task info = g_cFiler.getInfoList()[g_selCnt] fullPath = info.dirName + '/' + info.modelName fullPath = '/sd/models/' + fullPath + '.kmodel' try: g_task = kpu.load(fullPath) if (info.modelType == 'yolo2'): 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. kpu.init_yolo2(g_task, 0.5, 0.3, 5, anchor) except: lcd.draw_string(0, 20, "Err:" + info.modelName + " not find.", lcd.WHITE, lcd.RED) g_task = None return g_task
def checkCamera(): global x, y x = 0 y = 0 showInfo("camera starting", True) img = image.Image() try: sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time=2000) lcd.rotation(2) except: showInfo("camera init failed", False) return else: showInfo("camera init done", True) try: task = kpu.load(0x300000) anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025) a = kpu.init_yolo2(task, 0.5, 0.3, 5, anchor) while True: img = sensor.snapshot() code = kpu.run_yolo2(task, img) if code: for i in code: a = img.draw_rectangle(i.rect(), color=lcd.BLUE) new = img.copy(roi=(0, 0, 239, 239)) lcd.display(new) except: print('kup load fialed') while True: img = sensor.snapshot() new = img.copy(roi=(0, 0, 239, 239)) lcd.display(new)
def main(labels = None, model_addr="/sd/m.kmodel", lcd_rotation=0, sensor_hmirror=False, sensor_vflip=False): gc.collect() sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.set_windowing((224, 224)) 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: raise Exception("no labels.txt") 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\n%s" %(pmax, labels[max_index].strip()), scale=2, color=(255, 0, 0)) img.draw_string(0, 200, "t:%dms" %(t), scale=2, color=(255, 0, 0)) lcd.display(img) except Exception as e: sys.print_exception(e) finally: kpu.deinit(task)
#tested with frimware 5-0.22 import sensor,image,lcd import KPU as kpu lcd.init() sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.set_windowing((224, 224)) sensor.set_vflip(1) sensor.run(1) classes = ["racoon"] task = kpu.load(0x200000) #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, 0.3, 0.3, 5, anchor) #tweak the second parameter if you're getting too many false positives while(True): img = sensor.snapshot().rotation_corr(z_rotation=90.0) a = img.pix_to_ai() code = kpu.run_yolo2(task, img) if code: for i in code: a=img.draw_rectangle(i.rect(),color = (0, 255, 0)) a = img.draw_string(i.x(),i.y(), classes[i.classid()], color=(255,0,0), scale=3) a = lcd.display(img) else: a = lcd.display(img) a = kpu.deinit(task)
import sensor, image, lcd, utime # import 相关库 from fpioa_manager import * import KPU as kpu import time, uos from Maix import FPIOA, GPIO from fpioa_manager import fm, board_info from Maix import GPIO from machine import UART task_fd = kpu.load(0x200000) # 从flash 0x200000 加载人脸检测模型 task_ld = kpu.load(0x300000) # 从flash 0x300000 加载人脸五点关键点检测模型 task_fe = kpu.load(0x400000) # 从flash 0x400000 加载人脸196维特征值模型 clock = time.clock() # 初始化系统时钟,计算帧率 key_pin = 16 # 设置按键引脚 FPIO16 fpioa = FPIOA() fpioa.set_function(key_pin, FPIOA.GPIO7) key_gpio = GPIO(GPIO.GPIO7, GPIO.IN) last_key_state = 1 key_pressed = 0 # 初始化按键引脚 分配GPIO7 到 FPIO16 def check_key(): # 按键检测函数,用于在循环中检测按键是否按下,下降沿有效 global last_key_state global key_pressed val = key_gpio.value() if last_key_state == 1 and val == 0: key_pressed = 1 else: key_pressed = 0 last_key_state = val