def classify_image(self, string):
     img = snapshot()
     self.img_copy = img.resize(224, 224)
     a = self.img_copy.pix_to_ai()
     a = img.draw_string(0,
                         rows[self.row],
                         string,
                         color=(255, 255, 255),
                         scale=1.5,
                         mono_space=False)
     a = lcd.display(img)
     del (img)
     gc.collect()
Example #2
0
    def detect_objects(self, threshold, return_img=False):
        detected = False
        img = snapshot()

        img_copy = img.resize(224, 224)
        a = img_copy.pix_to_ai()
        code = kpu.run_yolo2(self.object_detection_task, img_copy)

        if code:
            for i in code:
                if i.value() >= threshold:
                    detected = True

                    new_x, new_y = int(i.x() * 1.07), int(i.y() * 1.42)
                    roi = (new_x, new_y, int(i.w() * 1.07), int(i.h() * 1.42))
                    percent = i.value()
                    object_detected = self.classes[i.classid()]

                    if not return_img:
                        a = img.draw_rectangle(roi,
                                               color=(0x1c, 0xa2, 0xff),
                                               thickness=2)
                        a = img.draw_string(new_x,
                                            new_y - 14,
                                            ("%s %%: %.2f" %
                                             (object_detected, percent)),
                                            color=(255, 255, 255),
                                            scale=1.5,
                                            mono_space=False)

            self.x_center, self.y_center, self.area, self.percent, roi = _find_max(
                code)

        del (img_copy)

        if not detected:
            self.object_detected = None
            self.percent = -1
            if return_img:
                return img, None

        if return_img:
            return img, roi
        else:
            a = lcd.display(img)

        del (img)
 def classify_image(self, threshold):
     img = snapshot()
     img_copy = img.resize(224, 224)
     a = img_copy.pix_to_ai()
     fmap = kpu.forward(self.image_objects_task, img_copy)
     plist = fmap[:]
     pmax = max(plist)
     self.percent = pmax
     if self.percent >= threshold:
         max_index = plist.index(pmax)
         a = img.draw_string(8,
                             rows[self.row],
                             ("Result: %s %%: %.2f" %
                              (self.labels[max_index].strip(), pmax)),
                             color=(255, 255, 255),
                             scale=1.5,
                             mono_space=False)
         self.image_class = self.labels[max_index].strip()
     a = lcd.display(img)
     del (img)
     del (img_copy)
    def class_predict(self, THRESHOLD=50):
        img = snapshot()
        self.img_copy = img.resize(224, 224)
        #kpu.memtest()
        a = self.img_copy.pix_to_ai()
        res_index = -1
        try:
            res_index, min_dist = self.classifier.predict(self.img_copy)
            self.percent = round(min_dist, 0)
            #print(" percent:",self.percent)
            #print("res_index: ", res_index)
            #print("{:.2f}".format(min_dist))
        except Exception as e:
            print("predict err:", e)
        if res_index >= 0 and self.percent >= THRESHOLD:
            self.image_class = self.categoryname[self.class_list[res_index]]
            #print("predict result:", self.image_class)

        a = lcd.display(img)
        del (img)
        #del(img_copy)
        gc.collect()
    def record_samples_training(self):
        self.classify_image(("Samples taken: %d" % (self.cap_num - 3)))
        if self.cap_num < self.classnumber + self.samplesnumber:
            index = self.classifier.add_sample_img(self.img_copy)
            self.cap_num += 1

        if self.train_status == 0:
            if self.cap_num >= self.classnumber + self.samplesnumber:
                print("start train")
                img = snapshot()
                a = img.draw_string(lcd.width() // 2 - 68,
                                    lcd.height() // 2 - 4,
                                    "Training...",
                                    color=(0, 255, 0),
                                    scale=3,
                                    mono_space=False)
                lcd.display(img)
                del (img)
                #del(self.img_copy)
                gc.collect()
                time.sleep(2)
                self.classifier.train()
                print("train end")
                self.train_status = 1