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 __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 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 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 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 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 _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 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 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 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, 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 __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 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)
#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)
uart = UART(UART.UART1, 115200, 8, None, 1, timeout=1000, read_buf_len=4096) #配置uart lcd.init() # 初始化lcd lcd.rotation(2) sensor.reset() #初始化sensor 摄像头 sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.set_hmirror(0) #设置摄像头镜像 sensor.set_vflip(0) #设置摄像头翻转 sensor.run(1) #使能摄像头 anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025) #anchor for face detect 用于人脸检测的Anchor dst_point = [ (44, 59), (84, 59), (64, 82), (47, 105), (81, 105) ] #standard face key point position 标准正脸的5关键点坐标 分别为 左眼 右眼 鼻子 左嘴角 右嘴角 a = kpu.init_yolo2(task_fd, 0.5, 0.3, 5, anchor) #初始化人脸检测模型 img_lcd = image.Image() # 设置显示buf img_face = image.Image(size=(128, 128)) #设置 128 * 128 人脸图片buf a = img_face.pix_to_ai() # 将图片转为kpu接受的格式 record_ftr = [] #空列表 用于存储当前196维特征 record_ftrs = [] #空列表 用于存储按键记录下人脸特征, 可以将特征以txt等文件形式保存到sd卡后,读取到此列表,即可实现人脸断电存储。 names = [] # 人名标签,与上面列表特征值一一对应。 with open("/sd/recordftr3.txt", "r") as f: while (1): line = f.readline() if not line: break name = line[0:line.index('#')] line = line[line.index('#') + 1:] record_ftrs.append(eval(line)) names.append(name)
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 sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) #QVGA=320x240 sensor.run(1) 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. kpu.init_yolo2(task, 0.5, 0.3, 5, anchor) but_stu = 1 try: while (True): img = sensor.snapshot() # Take an image from sensor bbox = kpu.run_yolo2(task, img) # Run the detection routine if bbox: bbox.sort(reverse=True, key=lambda x: x.rect()[2] * x.rect()[3]) c = bbox[0].rect()[0] + (bbox[0].rect()[2] / 2) print(str(c) + ' ' + str(len(bbox))) if c < 120: print('>>>') u_send(200) if c > 160:
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) fm.register(board_info.PIN15, fm.fpioa.UART1_TX) fm.register(board_info.PIN17, fm.fpioa.UART1_RX) uart_A = UART(UART.UART1, 115200, 8, None, 1, timeout=1000, read_buf_len=4096) classes = ["racoon"] task = kpu.load(0x600000) 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) 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) uart_A.write(str(i.rect())) a = lcd.display(img)
import camera import KPU as kpu if __name__ == "__main__": import time last = time.ticks_ms() classes = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'] while True: try: HowManyTask = kpu.load(0x5C0000) #task = kpu.load("/sd/0x5C0000_20class.kmodel") kpu.init_yolo2(HowManyTask, 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)) while True: #print(time.ticks_ms() - last) last = time.ticks_ms() img = camera.obj.get_image() HowManyThings = kpu.run_yolo2(HowManyTask, img) if HowManyThings: for pos in range(len(HowManyThings)): i = HowManyThings[pos] img.draw_rectangle(320 - (i.x() + i.w()), i.y(), i.w(), i.h()) img.draw_string(320 - (i.x() + i.w()), i.y(), '%.2f:%s' % (i.value(), classes[i.classid()]), color=(0, 255, 0)) ## gc.collect() # have bug when reply 3 lcd.display(img) except KeyboardInterrupt as e: pass finally:
def main(): servo_freq = 50 # Hz servo_vert = Servo(pin=10, freq=servo_freq, min_duty=7, max_duty=11.5, timer=Timer.TIMER0, channel=Timer.CHANNEL0, initial_pos=0.5) servo_hor = Servo(pin=11, freq=servo_freq, min_duty=2.8, max_duty=11.5, timer=Timer.TIMER0, channel=Timer.CHANNEL1) lcd.init(freq=15000000) lcd.direction(lcd.YX_LRDU) sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.set_vflip(1) # 20 class yolo classes = [ 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor' ] # target_class = 14 # person # target_class = 7 # cat target_class = 4 # bottle # target_class = 19 # monitor task = kpu.load(0x500000) anchor = (1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52) # For face detector yolo # 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) # target_class = 0 a = kpu.init_yolo2(task, 0.1, 0.3, 5, anchor) sensor.run(1) while (True): img = sensor.snapshot() code = kpu.run_yolo2(task, img) target_boxes = [] if code: target_boxes = [d for d in code if d.classid() == target_class] if target_boxes: # change turret position target = max(target_boxes, key=lambda d: d.value()) a = img.draw_rectangle(target.rect()) a = lcd.display(img) for i in code: lcd.draw_string(target.x(), target.y() + 12, '%f1.3' % target.value(), lcd.RED, lcd.WHITE) target_center_x = target.x() + target.w() // 2 target_center_y = target.y() + target.h() // 2 servo_hor.pos += 0.00015 * (sensor.width() // 2 - target_center_x) servo_vert.pos -= 0.0005 * (sensor.height() // 2 - target_center_y) # print('hor', servo_hor.pos) # print('vert', servo_vert.pos) else: a = lcd.display(img) kpu.deinit(task)
class Maix_dock_device: TOF10120_addr = 0x52 VL53L0X_addr = 0x29 memaddr = 0xc2 nbytes = 1 VL53L0X_REG_IDENTIFICATION_MODEL_ID = 0xc0 VL53L0X_REG_IDENTIFICATION_REVISION_ID = 0xc2 VL53L0X_REG_RESULT_INTERRUPT_STATUS = 0x13 VL53L0X_REG_RESULT_RANGE_STATUS = 0x14 VL53L0X_REG_SYSRANGE_START = 0x00 pcf8591_addr = 0x48 #pcf8591_addr = 0x4f fm.register(1, fm.fpioa.GPIO0) fm.register(2, fm.fpioa.GPIO1) fm.register(3, fm.fpioa.GPIO2) fm.register(13, fm.fpioa.GPIO3) fm.register(14, fm.fpioa.GPIO4) fm.register(15, fm.fpioa.GPIOHS1) fm.register(17, fm.fpioa.GPIOHS2) UVC = GPIO(GPIO.GPIO0, GPIO.OUT) LED_dis = GPIO(GPIO.GPIO1, GPIO.OUT) BEE = GPIO(GPIO.GPIO2, GPIO.OUT) TEMP_1 = GPIO(GPIO.GPIO3, GPIO.OUT) TEMP_2 = GPIO(GPIO.GPIO4, GPIO.OUT) KEY_start = GPIO(GPIO.GPIOHS1, GPIO.IN, GPIO.IRQ_FALLING) #key = GPIO(GPIO.GPIOHS0, GPIO.IN, GPIO.PULL_NONE) KEY_lock = GPIO(GPIO.GPIOHS2, GPIO.IN, GPIO.PULL_NONE) tempBorder_times = 0 #lcd.init() sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.run(1) task = kpu.load( 0x300000 ) # you need put model(face.kfpkg) in flash at address 0x300000 # task = kpu.load("/sd/face.kmodel") 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) i2c = I2C(I2C.I2C0, mode=I2C.MODE_MASTER, freq=100000, scl=9, sda=10) # software i2c i2c_extend = I2C(I2C.I2C1, mode=I2C.MODE_MASTER, freq=100000, scl=11, sda=12) # software i2c def __init__(self): print("Maix_dock_device class") self.infrared_range_left = 0 self.infrared_range_right = 0 self.temperature_sensor_left = 0 self.temperature_sensor_right = 0 self.ad48v = 0 self.face_detection = 0 self.readAdd_times = 0 def set_face_detection(self): img = sensor.snapshot() code = kpu.run_yolo2(self.task, img) if code: for i in code: #print(i) a = img.draw_rectangle(i.rect()) print("find face") self.face_detection = 1 else: print("no face") self.face_detection = 0 def get_face_detection(self): return self.face_detection def get_infrared_range_left(self): return self.infrared_range_left def get_infrared_range_right(self): return self.infrared_range_right def get_ad48v(self): return self.ad48v def get_temperature_sensor_left(self): return self.temperature_sensor_left def get_temperature_sensor_left(self): return self.temperature_sensor_left def set_UVC_OUT(self, value): self.UVC.value(value) def set_led(self, value): self.LED_dis.value(value) def get_temperature_sensor_left(self): return 20.1 def get_keyValue_start(self): if (self.KEY_start.value() == 1): time.sleep_ms(10) if (self.KEY_start.value() == 1): #print("KEY_start close") return 1 else: #print("KEY_start open") return 0 def show_deviceAddr(self): devices = self.i2c.scan() print(devices) devices2 = self.i2c_extend.scan() print(devices2) def set_infrared_range_left(self): try: self.i2c.writeto_mem(0x29, 0x00, chr(1)) time.sleep_ms(10) #time.sleep_ms(40) data_i2c = self.i2c.readfrom_mem(0x29, 0x1e, 2) data_i2c = data_i2c[0] << 8 | data_i2c[1] if data_i2c != 20: self.infrared_range_left = data_i2c return data_i2c else: print("data_i2c == 20") return None except OSError as err: if err.args[0] == errno.EIO: print("i2c1 dis errno.EIO") return None else: print("i2c1 abnormal") return None def set_infrared_range_right(self): try: self.i2c_extend.writeto_mem(0x29, 0x00, chr(1)) time.sleep_ms(10) #time.sleep_ms(40) data_i2c = self.i2c_extend.readfrom_mem(0x29, 0x1e, 2) data_i2c = data_i2c[0] << 8 | data_i2c[1] if data_i2c != 20: self.infrared_range_right = data_i2c return data_i2c else: print("data_i2c == 20") return None except OSError as err: if err.args[0] == errno.EIO: print("i2c2 errno.EIO") return None else: print("i2c2 abnormal") return None def set_ad48v_chl(self, chn): try: if chn == 0: self.i2c.writeto(self.pcf8591_addr, chr(0x40)) self.i2c.readfrom(self.pcf8591_addr, 1) ad_value = self.i2c.readfrom(self.pcf8591_addr, 1) ad_value = ad_value[0] * 58 / 255 self.ad48v = ad_value - 1.2 return ad_value if chn == 1: self.i2c.writeto(self.pcf8591_addr, chr(0x41)) self.i2c.readfrom(self.pcf8591_addr, 1) ad_value = self.i2c.readfrom(self.pcf8591_addr, 1) ad_value = 3.3 * ad_value[0] / 255 self.ad48v = -61.44 * ad_value + 174.4 return ad_value if chn == 2: self.i2c.writeto(self.pcf8591_addr, chr(0x42)) self.i2c.readfrom(self.pcf8591_addr, 1) ad_value = self.i2c.readfrom(self.pcf8591_addr, 1) ad_value = 3.3 * ad_value[0] / 255 self.ad48v = -61.44 * ad_value + 174.4 return ad_value if chn == 3: self.i2c.writeto(self.pcf8591_addr, chr(0x43)) except OSError as err: if err.args[0] == errno.EIO: print("i2c1 ad errno.EIO") def set_ad48v(self, chn): self.set_ad48v_chl(chn) def set_tof10120_left(self): try: data_i2c = self.i2c.readfrom_mem(self.TOF10120_addr, 0x00, 2) dis_left = data_i2c[0] * 256 + data_i2c[1] print("dis_left", '%d' % dis_left) except OSError as err: if err.args[0] == errno.EIO: print("i2c1 tof10120_left errno.EIO") def set_tof10120_right(self): try: data_i2c = self.i2c.readfrom_mem(self.TOF10120_addr + 1, 0x00, 2) dis_right = data_i2c[0] * 256 + data_i2c[1] print("dis_left", '%d' % dis_right) except OSError as err: if err.args[0] == errno.EIO: print("i2c1 tof10120_right errno.EIO") def uvc_autoControl(self): tempBorder_times = 0 if self.readAdd_times == 0: self.readAdd_times += 1 self.show_deviceAddr() self.set_infrared_range_left() utime.sleep_ms(200) self.set_ad48v(0) utime.sleep_ms(200) self.set_infrared_range_right() utime.sleep_ms(200) self.set_face_detection() if self.get_keyValue_start(): if ((self.get_face_detection() == 0) and (self.get_ad48v(0) > 36)): if ((tempBorder_times <= 3) and ((self.get_infrared_range_left() < 500) or (self.get_infrared_range_right() < 500))): self.set_UVC_OUT(1) self.set_led(1) print("uvc_out: 1") else: self.set_UVC_OUT(0) self.set_led(0) print("uvc_out: 0") print("key_start_down!") else: print("key_start_up!") self.set_UVC_OUT(0) self.set_led(0) print("infrared_range_left = ", self.get_infrared_range_left()) print("infrared_range_right = ", self.get_infrared_range_right()) print("ad48v = ", self.get_ad48v()) print("face_detection = ", self.get_face_detection()) self.set_ad48v(1) tempBorder1 = self.get_ad48v() print("temp_left = ", tempBorder1) self.set_ad48v(2) tempBorder2 = self.get_ad48v() print("temp_right = ", tempBorder2) if (tempBorder1 > 70 or tempBorder2 > 70): tempBorder_times += 1 if (tempBorder_times > 3): tempBorder_times = 4 else: tempBorder_times = 0
key_gpio.irq(set_key_state, GPIO.IRQ_RISING, GPIO.WAKEUP_NOT_SUPPORT) lcd.init() sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.set_hmirror(1) sensor.set_vflip(1) sensor.run(1) anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025) # anchor for face detect dst_point = [(44, 59), (84, 59), (64, 82), (47, 105), (81, 105)] # standard face key point position a = kpu.init_yolo2(task_fd, 0.5, 0.3, 5, anchor) img_lcd = image.Image() img_face = image.Image(size=(128, 128)) a = img_face.pix_to_ai() record_ftr = [] record_ftrs = [] names = [ 'Mr.1', 'Mr.2', 'Mr.3', 'Mr.4', 'Mr.5', 'Mr.6', 'Mr.7', 'Mr.8', 'Mr.9', 'Mr.10' ] ACCURACY = 85 while (1): img = sensor.snapshot() clock.tick()
def load(): if FaceDetect.is_load == False: FaceDetect.model = kpu.load(0x2C0000) # Load Model File from Flash # Anchor data is for bbox, extracted from the training sets. kpu.init_yolo2(FaceDetect.model, 0.5, 0.3, 5, anchor) FaceDetect.is_load = True
img_w = sensor.width() img_h = sensor.height() sensor_ID = sensor.get_id() if (sensor_ID == 30530): sensor_ID_str = 'OV7740' print("image sensor is " + str(sensor_ID_str) + ", with size " + str(img_w) + " x " + str(img_h)) sensor.skip_frames(time=600) #sensor.run(1) #https://maixpy.sipeed.com/zh/libs/Maix/kpu.html task = kpu.load(0x300000) # Load Model File from Flash #you need put model(face.kfpkg) in flash at address 0x300000 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(task, 0.5, 0.3, 5, anchor) # kpu 网络对象,threshold,box_iou 门限,锚点数,锚点参数与模型参数一致 fps = 0 but_stu = 1 autosave_cnt = 0 face_duration_cnt = 0 clock = time.clock() gc.collect() RGB_LED_OFF() try: while (True): clock.tick() img = sensor.snapshot() # Take an image from sensor bbox = kpu.run_yolo2(task, img) # Run the detection routine if bbox: RGB_LED_RED() for i in bbox:
f = open(file_name,"a") str_list_target = ["{:0.4f}".format(x) for x in data] str_target = ','.join(str_list_target) f.write(str_target) f.close() print("save to dataset success") def send_sheet(face_id): return #=== AI Models ===# task_face_detect = kpu.load(0x200000) task_face_encode = kpu.load(0x300000) kpu.set_outputs(task_face_encode,0,1,1,128) 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_face_detect, 0.5, 0.3, 5, anchor) #====== config ======# face_threshold = 15 dataset_filename = "faces.csv" #====================# #=== SETUP ===# #clear_dataset(dataset_filename,face_dataset) face_dataset = read_dataset(dataset_filename); corgi85.IFTTT_init("corgi_detect","0hI55mQkUiimG6RIjpWhp") #=== wait wifi connect ===# while corgi85.wifi_check() == 0: print("WIFI Connecting ...") time.sleep(1)
import sensor,image,lcd,time import KPU as kpu sensor.reset(freq=24000000,dual_buff=True) sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.set_windowing((320, 224)) sensor.set_vflip(1) sensor.run(1) lcd.init(type=2, freq=20000000, color=lcd.BLACK) #lcd.rotation(2) classes = ["face", "face_mask"] task = kpu.load(0x400000) a = kpu.set_outputs(task, 0, 10,7,35) anchor = (0.212104,0.261834, 0.630488,0.706821, 1.264643,1.396262, 2.360058,2.507915, 4.348460,4.007944) a = kpu.init_yolo2(task, 0.5, 0.5, 5, anchor) while(True): timestamp = time.ticks_ms() img = sensor.snapshot() a = img.pix_to_ai() faces = kpu.run_yolo2(task, img) if faces: for face in faces: if face.classid() == 0 : a=img.draw_rectangle(face.rect(),color = (255, 0, 0),thickness=5) elif face.classid() == 1 : a=img.draw_rectangle(face.rect(),color = (0,255, 0),thickness=5) a = img.draw_string(70,10,"FPS : %.2f" % (1000/(time.ticks_ms()-timestamp)),color=(0,255,0),scale=2) a = lcd.display(img) a = kpu.deinit(task)
sensor.skip_frames(time=2000) # Wait for settings take effect. sensor.set_vflip(1) sensor.set_hmirror(1) clock = time.clock() # Create a clock object to track the FPS. print('loading face detect model') task_detect_face = kpu.load(0x300000) # Charge face detect model into KPU print('loading face expresion classify model') task_classify_face = kpu.load( 0x500000) # Charge face classification model into KPU a = kpu.set_outputs(task_classify_face, 0, 1, 1, 2) 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_detect_face, 0.5, 0.3, 5, anchor) labels = ['happy', 'sad'] # Facial expression labels print('configuration complete') while (True): clock.tick() # Update the FPS clock. img = sensor.snapshot() # Take a picture and return the image. detected_face = kpu.run_yolo2(task_detect_face, img) if detected_face: for i in detected_face: face = img.cut(i.x(), i.y(), i.w(), i.h()) face_128 = face.resize(128, 128) a = face_128.pix_to_ai()