示例#1
0
class DrawGame:
    def __init__(self, arm):
        self.arm = arm
        print("Begginging drawing recognition game!")
        self.v = Vision()
        self.model = load_model("nn_hog.h5")
        self.categories = ('airplane', 'backpack', 'cactus', 'dog', 'ear',
                           'face', 'garden', 'hamburger', 'icecream', 'jacket')

    def run_game(self, times=3):
        current = 0

        # Robot draws bounds.
        self.arm.move_back()
        # FormatConvert.drawFromFile("bounds.txt", self.arm, speed=3)
        self.arm.move_away()
        # Small wait
        time.sleep(1)
        # Do game loop
        while current < times:
            # Human draws
            # Wait until human is "finished"
            input("Press enter when finished...")

            # When human is finished, detect category
            img = self.v._getImage(self.v.cam1)
            img_to_predict = self.preprocessing(img)

            # Get drawing of a category

            # Robot draws the category

            # Tiny wait
            time.sleep(1)
        print("Game over!")

    def preprocessing(self, img):
        print("Preprocessing img")
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        img = cv2.medianBlur(img, 5)
        _, img = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY_INV)
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
        img = cv2.dilate(img, kernel)
        img = np.rot90(img, 1)
        img = img[605:795, 280:470]
        img = cv2.resize(img, (28, 28))
        cv2.imshow('frame', img)
        cv2.waitKey(1)
        return img
示例#2
0
    def saveImgFromCamera(self, n, cat, fname_start):
        # dp = DataPrep(max_data_n=100)
        categories = (
            'airplane',
            'backpack',
            'cactus',
            'dog',
            'ear',
            'face',
            'garden',
            'hamburger',
            'icecream',
            'jacket',
            'kangaroo',
            'ladder',
            'mailbox',
            'nail',
            'ocean',
            'paintbrush',
            'rabbit',
            'sailboat',
            'table',
            'underwear',
            'vase',
            'windmill',
            'yoga',
            'zebra',
        )
        print('.')
        v = Vision()
        print('.')
        print("Beginning saving doodles from image.")
        print("First: Drawing bounds for doodle.")
        #        boundFile = "bounds.txt"

        print("Now for the drawing part!")
        for a in range(n):
            for i in cat:
                # self.arm.move_back()
                # FormatConvert.drawFromFile(boundFile, self.arm, speed=3)
                # self.arm.move_away()
                print("\nPlease draw ", categories[i], "...")
                input()
                img = v._getImage(v.cam1)
                img = v._getImage(v.cam1)
                img = v._getImage(v.cam1)
                img = v._getImage(v.cam1)
                img = v._getImage(v.cam1)
                img = v._getImage(v.cam1)
                img = v._getImage(v.cam1)
                img = v._getImage(v.cam1)
                img = v._getImage(v.cam1)
                img = v._getImage(v.cam1)
                img = v._getImage(v.cam1)
                img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                _, img = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY_INV)
                img = np.rot90(img, 3)
                img = img[630:810, 260:440]
                name = fname_start + "_" + categories[i]
                name = name + "_" + str(a) + ".png"
                # cv2.imshow('frame', img)
                # cv2.waitKey(1)
                print("Saving name...")
                cv2.imwrite(name, img)
                print("Saved.")
                # input("next?")

        print("Done saving images.")