Example #1
0
    def on_button(self, pressed):
        # print(f"button:{pressed}")
        self.pressed = pressed

        if pressed:
            self.spell = None
            self.time = time.time_ns()
            self.positions = []
        else:
            # If releasing the button, get the gesture
            norm = self.min_max_normalize(self.positions)
            # print(norm)
            ms = round((time.time_ns() - self.time) / 1000)
            print(ms)
            gesture = mg.getGesture(norm)
            closest = mg.findClosestMatchingGesture(gesture,
                                                    self.gestures,
                                                    maxDifference=1)

            if closest != None:
                # Just use the first gesture in the list using the gesture key
                self.spell = self.gestures[closest[0]]
                self.vibrate(PATTERN.SHORT)
            # Print out the gesture
            print(f"{gesture}: {self.spell}")
Example #2
0
def lookup (points):
  strokes = moosegesture.getGesture(points)
  if len(strokes) == 0:
    if identify_hold(points):
      if len(points) > 10:
        return "long_hold"
      else: 
        return "short_hold"
    return None
  possibles = gestureMap.keys()
  gestures = moosegesture.findClosestMatchingGesture(strokes, 
    gestureMap.keys(),
    tolerance=3)

  print strokes

  if gestures == None:
    return None

  gestures = [gestureMap[x] for x in gestures]

  for g in gestures:
    print "potential", g
  
  for shape in ["updown", "triangle", "square", "leftright", "upright", \
                "upleft", "downleft", "downright"]:
    if shape in gestures:
      return shape

  return gestures[0]
Example #3
0
 def test_upright(self):
     moosegesture._MIN_STROKE_LEN = 60
     pointSets = ([(139, 440), (149, 430), (166, 412), (201, 373), (237, 334), (291, 284), (342, 232), (373, 198), (391, 175)],
                  [(137, 444), (141, 439), (144, 435), (149, 427), (157, 416), (171, 399), (186, 379), (200, 361), (213, 346), (223, 332), (233, 319), (245, 301), (252, 291), (258, 281), (265, 273), (272, 263), (280, 253), (286, 243), (290, 237), (294, 233), (301, 222), (308, 211), (315, 203)],
                  [(163, 447), (189, 415), (251, 345), (379, 206), (433, 129)],
                  [(147, 439), (171, 422), (199, 404), (226, 389), (252, 373), (283, 350), (308, 335), (334, 320), (360, 304), (385, 286), (410, 267), (433, 252), (448, 242), (452, 238)],
                  [(173, 455), (187, 440), (203, 423), (218, 409), (232, 395), (249, 378), (264, 359), (282, 341), (296, 324), (320, 297), (341, 274), (361, 245), (383, 214), (405, 182), (420, 162)],
                  [(226, 451), (249, 434), (281, 411), (314, 383), (356, 347), (383, 323)],
                 )
     for i in range(len(pointSets)):
         self.assertEqual((i, moosegesture.getGesture(pointSets[i])), (i, [UPRIGHT]))
Example #4
0
 def test_upleft(self):
     moosegesture._MIN_STROKE_LEN = 60
     pointSets = ([(390, 411), (383, 398), (365, 371), (341, 344), (306, 313), (271, 281), (238, 251), (219, 227), (204, 207), (194, 194), (188, 184)],
                  [(384, 439), (375, 431), (361, 417), (343, 393), (321, 366), (297, 335), (268, 295), (240, 257), (215, 226), (196, 203)],
                  [(389, 389), (377, 367), (354, 332), (310, 271), (268, 213), (229, 167), (198, 133), (181, 117), (175, 113), (175, 112)],
                  [(323, 468), (301, 435), (275, 402), (246, 360), (216, 317), (187, 279), (162, 244), (140, 209), (124, 185), (116, 173)],
                  [(376, 426), (356, 412), (332, 397), (307, 377), (280, 354), (252, 322), (225, 293), (202, 266), (182, 243), (163, 219), (140, 193), (126, 177)],
                  [(357, 362), (335, 329), (294, 280), (236, 209), (203, 173), (179, 143), (159, 123)],
                  [(304, 404), (290, 377), (270, 340), (245, 299), (219, 256), (188, 209), (165, 174), (146, 144), (134, 125), (128, 113)],
                 )
     for i in range(len(pointSets)):
         self.assertEqual((i, moosegesture.getGesture(pointSets[i])), (i, [UPLEFT]))
Example #5
0
    def on_button(self, pressed):
        self.pressed = pressed

        if not pressed:
            # If releasing the button, print out the gestures and reset the positions
            gesture = moosegesture.getGesture(self.positions)
            self.positions = []

            print(gesture)
            # If it is a counterclockwise circle disconnect the wand
            if gesture == ['R', 'UR', 'U', 'UL', 'L', 'DL', 'D', 'DR', 'R']:
                self.disconnect()
Example #6
0
 def test_downright(self):
     moosegesture._MIN_STROKE_LEN = 60
     pointSets = ([(141, 136), (167, 163), (202, 200), (233, 237), (257, 270), (277, 299), (295, 327), (316, 350), (343, 369), (359, 375), (364, 377), (368, 383), (371, 386)],
                  [(195, 147), (214, 182), (256, 244), (286, 293), (310, 329), (325, 349), (333, 358), (336, 360)],
                  [(163, 117), (184, 146), (229, 214), (322, 366), (378, 449), (407, 482)],
                  [(143, 167), (170, 198), (176, 206), (200, 245), (217, 278), (237, 316), (258, 352), (281, 387), (300, 413)],
                  [(188, 140), (198, 150), (207, 162), (220, 179), (237, 202), (259, 232), (289, 276), (311, 308), (333, 341), (353, 369), (370, 392), (383, 408)],
                  [(168, 239), (183, 271), (203, 303), (226, 334), (248, 368), (277, 408), (296, 440), (313, 466), (330, 489)],
                  [(130, 198), (138, 208), (151, 228), (181, 268), (205, 299), (223, 323), (238, 344), (250, 365), (265, 390), (278, 411), (292, 433), (309, 453), (335, 474), (347, 485), (354, 492)],
                  [(169, 173), (174, 181), (185, 193), (207, 216), (233, 243), (260, 266), (280, 285), (297, 302), (312, 319), (325, 336), (341, 356), (354, 369), (365, 383), (375, 393), (385, 403), (397, 415), (411, 428)],
                 )
     for i in range(len(pointSets)):
         self.assertEqual((i, moosegesture.getGesture(pointSets[i])), (i, [DOWNRIGHT]))
Example #7
0
 def test_right(self):
     moosegesture._MIN_STROKE_LEN = 60
     pointSets = ([(85, 271), (141, 277), (230, 278), (284, 280), (340, 281), (383, 282), (412, 284), (428, 284), (433, 284), (435, 284)],
                  [(51, 364), (84, 368), (142, 373), (217, 374), (290, 374), (343, 376), (377, 377), (392, 377), (404, 377), (416, 377), (431, 377)],
                  [(88, 317), (103, 316), (139, 313), (191, 308), (244, 305), (285, 301), (316, 298), (348, 294), (372, 293), (394, 289), (412, 287), (422, 285), (428, 285), (432, 285), (436, 285), (442, 287), (451, 290), (465, 293)],
                  [(122, 361), (158, 361), (216, 361), (295, 361), (347, 358), (390, 356), (426, 355), (452, 353), (466, 350), (472, 349), (473, 348)],
                  [(138, 287), (177, 287), (224, 287), (270, 287), (314, 287), (362, 290), (420, 294), (435, 296)],
                  [(137, 426), (172, 429), (243, 434), (341, 434), (407, 434), (431, 434)],
                  [(97, 344), (145, 331), (216, 316), (274, 308), (322, 306), (356, 304), (369, 304)],
                  [(141, 390), (167, 388), (201, 388), (237, 388), (271, 387), (302, 387)],
                 )
     for i in range(len(pointSets)):
         self.assertEqual((i, moosegesture.getGesture(pointSets[i])), (i, [RIGHT]))
Example #8
0
 def test_left(self):
     moosegesture._MIN_STROKE_LEN = 60
     pointSets = ([(427, 316), (402, 315), (370, 315), (329, 318), (287, 326), (256, 329), (240, 329), (233, 329), (230, 329)],
                  [(488, 326), (458, 328), (415, 333), (365, 337), (314, 340), (265, 340), (202, 341), (176, 341), (169, 341), (166, 341)],
                  [(427, 276), (366, 270), (274, 268), (197, 268), (139, 268), (114, 268), (103, 268), (101, 268)],
                  [(471, 376), (447, 376), (418, 376), (385, 374), (353, 374), (317, 374), (257, 376), (221, 377), (187, 380), (159, 380), (138, 381), (127, 381), (119, 381)],
                  [(491, 299), (475, 291), (459, 284), (443, 282), (412, 278), (380, 276), (339, 273), (292, 270), (242, 267), (206, 266), (175, 265), (161, 265), (154, 265), (148, 265), (141, 265), (129, 265), (115, 265), (96, 265)],
                  [(483, 354), (448, 355), (425, 358), (402, 360), (381, 361), (358, 361), (333, 361), (307, 361), (279, 361), (245, 361), (226, 361), (210, 361), (194, 364), (179, 365), (163, 365), (147, 366), (134, 366), (123, 366), (112, 366), (105, 367)],
                  [(440, 314), (431, 314), (418, 314), (403, 313), (378, 310), (365, 310), (357, 309), (354, 309), (348, 309), (340, 309), (333, 308), (325, 308), (318, 308), (314, 308), (311, 307), (308, 306), (305, 306), (302, 306), (297, 306), (290, 307), (275, 312), (264, 316), (257, 319), (253, 321), (252, 323), (251, 323), (249, 323), (245, 323), (239, 323), (232, 324), (223, 324), (217, 324), (210, 324), (202, 323), (193, 323), (182, 321), (173, 319), (164, 318), (157, 316), (154, 315), (149, 314), (138, 313), (128, 312), (119, 311), (114, 311), (109, 311), (106, 310)],
                  [(429, 256), (316, 242), (209, 236), (145, 236), (106, 237), (77, 243)],
                 )
     for i in range(len(pointSets)):
         self.assertEqual((i, moosegesture.getGesture(pointSets[i])), (i, [LEFT]))
Example #9
0
 def test_downleft(self):
     moosegesture._MIN_STROKE_LEN = 60
     pointSets = ([(431, 159), (426, 169), (417, 181), (387, 214), (344, 249), (302, 294), (267, 336), (248, 365), (235, 380), (230, 387), (229, 390), (227, 392)],
                  [(363, 152), (343, 167), (304, 191), (277, 212), (247, 241), (216, 269), (197, 291), (182, 312), (168, 334), (159, 347), (153, 356)],
                  [(365, 249), (359, 258), (353, 269), (347, 278), (340, 288), (322, 315), (308, 335), (292, 357), (279, 377), (265, 398), (249, 423), (234, 446), (218, 471), (203, 494), (192, 512), (187, 522), (186, 523)],
                  [(374, 157), (367, 162), (357, 171), (340, 189), (311, 213), (278, 235), (248, 254), (221, 269), (208, 276), (197, 285), (182, 295)],
                  [(408, 246), (397, 260), (377, 278), (350, 300), (320, 328), (289, 362), (260, 394), (231, 424), (206, 451), (183, 482), (172, 496), (162, 509), (154, 522), (150, 530)],
                  [(405, 138), (378, 155), (344, 178), (298, 211), (256, 243), (219, 275), (187, 308), (158, 332), (141, 348), (137, 351)],
                  [(462, 251), (433, 284), (409, 305), (337, 376), (282, 430), (240, 473), (219, 494), (215, 499)],
                  [(249, 211), (236, 223), (209, 243), (179, 266), (143, 294), (118, 312), (89, 334), (61, 358)],
                  [(348, 250), (344, 255), (338, 262), (333, 269), (324, 278), (321, 282), (317, 286), (311, 293), (304, 301), (298, 310), (291, 319), (284, 328), (276, 337), (271, 342), (268, 347)],
                 )
     for i in range(len(pointSets)):
         self.assertEqual((i, moosegesture.getGesture(pointSets[i])), (i, [DOWNLEFT]))
Example #10
0
 def test_down(self):
     moosegesture._MIN_STROKE_LEN = 60
     pointSets = ([(264, 160), (264, 174), (264, 200), (264, 229), (264, 282), (264, 326), (264, 367), (264, 398), (265, 414), (266, 422), (267, 427)],
                  [(193, 127), (193, 136), (193, 164), (191, 193), (187, 230), (182, 273), (181, 314), (180, 348), (180, 364), (180, 370)],
                  [(254, 119), (254, 128), (254, 146), (255, 175), (257, 215), (258, 256), (259, 274), (260, 286), (260, 295), (260, 303), (260, 313), (260, 321)],
                  [(213, 128), (213, 130), (213, 135), (213, 142), (212, 150), (212, 159), (211, 167), (210, 174), (209, 182), (208, 194), (205, 210), (204, 219), (203, 229), (202, 237), (201, 241), (201, 244), (200, 249), (199, 258), (198, 271), (198, 286), (198, 307), (198, 330), (198, 354), (198, 380), (198, 404), (198, 424), (198, 435), (198, 445), (198, 449), (198, 451), (198, 452), (198, 453)],
                  [(260, 184), (264, 205), (267, 233), (273, 261), (278, 291), (286, 326), (292, 358), (299, 389), (301, 401), (303, 409), (304, 420)],
                  [(295, 206), (295, 216), (294, 228), (287, 250), (282, 278), (274, 322), (269, 344), (265, 374), (264, 420), (264, 455), (264, 477), (263, 493), (262, 508), (261, 517)],
                  [(263, 134), (263, 153), (262, 181), (262, 205), (262, 222), (263, 240), (264, 262), (266, 268), (269, 282), (271, 292), (274, 304), (277, 321), (278, 331), (278, 341), (278, 349), (278, 359), (278, 374), (278, 380), (278, 384), (278, 390), (278, 400), (277, 412), (277, 425), (278, 433), (280, 444), (282, 450), (282, 452), (283, 452), (283, 456), (283, 462)],
                  [(322, 170), (322, 186), (322, 205), (322, 243), (322, 279), (326, 323), (329, 363), (330, 392), (330, 414), (330, 429), (330, 442), (330, 461), (330, 473), (330, 481), (330, 487), (331, 493), (332, 497), (333, 498), (334, 500), (335, 503), (335, 505)],
                  [(264, 121), (261, 136), (258, 157), (255, 185), (254, 238), (254, 278), (254, 306), (254, 319), (254, 325), (254, 332), (254, 340), (254, 352)],
                  [(145, 64), (145, 91), (146, 132), (145, 248), (139, 386), (139, 501), (139, 566), (143, 589), (145, 595)],
                  )
     for i in range(len(pointSets)):
         self.assertEqual((i, moosegesture.getGesture(pointSets[i])), (i, [DOWN]))
Example #11
0
 def motion(self, _event):
     if self.inMotion:
         self.addPrevLoc(_event.pos)
         direction = moosegesture.getGesture(self.prevLocs)
         if len(direction) == 0:
             return
         if direction[-1].startswith("D"):
             self.vscroll = self.vscroll - 5
         elif direction[-1].startswith("U"):
             self.vscroll = self.vscroll + 5
         elif direction[-1] == "L" and self.scroll_horizontal:
             self.hscroll = self.hscroll + 5
         elif direction[-1] == "R" and self.scroll_horizontal:
             self.hscroll = self.hscroll - 5
         self.offset = (self.hscroll, self.vscroll)
         self.repaint()
Example #12
0
def gestureReco(buff1,buff2,gestures):
    pt1=np.array(buff1.allData())
    pt2=np.array(buff2.allData())
    x0=pt1[:,0]
    y0=pt1[:,1]
    x1=pt2[:,0]
    y1=pt2[:,1]
    x0=ptsPreprocess(x0)
    y0=ptsPreprocess(y0)
    x1=ptsPreprocess(x1)
    y1=ptsPreprocess(y1)
    dist=distanceVec(x0,y0,x1,y1)
    pts=dist2pts(dist)
    currgest=mg.getGesture(pts)
    currgest=[str(i) for i in currgest]
    result=mg.findClosestMatchingGesture(currgest,gestures)
    return str(result).strip('['),currgest
Example #13
0
    def on_button(self, pressed):
        self.pressed = pressed

        if pressed:
            self.spell = None
        else:
            # If releasing the button, get the gesture
            gesture = mg.getGesture(self.positions)
            self.positions = []
            closest = mg.findClosestMatchingGesture(gesture,
                                                    self.gestures,
                                                    maxDifference=1)

            if closest != None:
                # Just use the first gesture in the list using the gesture key
                self.spell = self.gestures[closest[0]]
                self.vibrate(PATTERN.SHORT)
            # Print out the gesture
            print("{}: {}".format(gesture, self.spell))
Example #14
0
    def eventFilter(self, source, event):
        if event.type() == QtCore.QEvent.MouseMove:
            self.frame.setCursor(QCursor(self.cursors[1], 35, 35))
            if event.buttons() == QtCore.Qt.LeftButton:

                if len(self.points) > 2:
                    startx, starty = self.points[0][0], self.points[0][1]
                    for i in range(len(self.points)):
                        self.points[i] = (self.points[i][0] - startx,
                                          self.points[i][1] - starty)
                self.points.append(
                    (event.localPos().x(), event.localPos().y()))
                # print(self.points)
        if event.type() == QtCore.QEvent.MouseButtonRelease and event.button(
        ) == QtCore.Qt.LeftButton:
            # print("Released!")
            self.mouseDown = False
            strokes = moosegesture.getGesture(self.points)
            if len(strokes) > 0:
                strokeText = str(strokes[-1])
                # print(strokeText)
                if strokeText == "R":
                    self.matrix, done = logic.right(self.matrix)
                    if done:
                        self.stateOfGame()
                elif strokeText == "L":
                    self.matrix, done = logic.left(self.matrix)
                    if done:
                        self.stateOfGame()
                elif strokeText == "U":
                    self.matrix, done = logic.up(self.matrix)
                    if done:
                        self.stateOfGame()
                elif strokeText == "D":
                    self.matrix, done = logic.down(self.matrix)
                    if done:
                        self.stateOfGame()
            strokes.clear()
        else:
            return True

        return self.frame.eventFilter(source, event)
Example #15
0
 def test_up(self):
     moosegesture._MIN_STROKE_LEN = 60
     pointSets = ([(264, 383), (264, 364), (263, 339), (260, 308), (255, 275), (252, 244), (252, 216), (252, 190), (252, 154), (252, 130), (252, 111), (252, 95), (252, 82), (252, 74), (252, 71)],
                  [(260, 458), (260, 431), (260, 400), (258, 364), (256, 325), (255, 270), (255, 236), (255, 203), (258, 169), (258, 148)],
                  [(331, 478), (331, 475), (331, 470), (331, 463), (331, 457), (331, 447), (331, 434), (331, 423), (331, 415), (331, 407), (331, 398), (331, 389), (331, 375), (331, 350), (331, 329), (331, 309), (330, 289), (329, 265), (329, 236), (326, 203), (325, 177), (325, 167), (325, 166)],
                  [(246, 479), (246, 457), (246, 410), (248, 342), (250, 260), (247, 150), (244, 93), (243, 68)],
                  [(332, 385), (332, 287), (332, 175), (330, 69), (324, 13), (322, 0)],
                  [(164, 523), (165, 490), (165, 441), (164, 332), (158, 225), (152, 132), (148, 81)],
                  [(313, 486), (313, 429), (323, 324), (344, 152), (349, 0), (351, 0), (349, 0)],
                  [(374, 458), (375, 400), (378, 315), (380, 196), (380, 88)],
                  [(224, 472), (222, 434), (222, 363), (213, 245), (192, 123), (176, 60)],
                  [(271, 466), (283, 411), (313, 258), (335, 127), (356, 43), (366, 26)],
                  [(327, 448), (320, 402), (312, 301), (301, 156), (289, 0)],
                  [(213, 518), (213, 490), (214, 434), (217, 378), (217, 323), (218, 279), (221, 251), (223, 233), (224, 222), (226, 215), (226, 210), (227, 205)],
                  [(334, 501), (336, 480), (336, 437), (335, 398), (331, 361), (329, 337), (329, 313), (328, 288), (328, 256), (328, 220), (325, 187), (324, 174), (322, 163), (322, 150), (320, 140), (319, 136)],
                  [(239, 506), (239, 487), (239, 465), (239, 430), (239, 388), (239, 340), (239, 296), (242, 253), (247, 228), (249, 205), (253, 185), (253, 166), (253, 144), (253, 126), (255, 116), (256, 106), (256, 102)],
                  [(340, 406), (345, 384), (352, 357), (357, 339), (362, 317), (364, 290), (365, 279), (367, 268), (369, 260), (369, 255), (369, 251), (369, 250)],
                  [(309, 512), (307, 484), (304, 442), (302, 380), (302, 300), (300, 231), (295, 191), (295, 175), (294, 168)],
                  )
     for i in range(len(pointSets)):
         self.assertEqual((i, moosegesture.getGesture(pointSets[i])), (i, [UP]))
Example #16
0
def sanitize_and_notify(coord_set):
    timestamp_vals = {}
    # filter all X axis events and set their timestamps
    for _, x_event in coord_set:
        if x_event is not None:
            timestamp_vals[f'{x_event.timestamp()}'] = [x_event.value]

    # filter all Y axis events and set their timestamps
    for y_event, _ in coord_set:
        try:
            if y_event is not None:
                try:
                    timestamp_vals[f'{y_event.timestamp()}'][1] = y_event.value
                except IndexError:
                    timestamp_vals[f'{y_event.timestamp()}'].append(
                        y_event.value)
        except KeyError:
            pass

    sanitized_tuple_list = []
    for item in timestamp_vals.values():
        if len(item) == 2:
            sanitized_tuple_list.append(tuple(item))
    # ignore the first 10% events cause it's garbage sometimes
    trim_beginning_len = 0.1 * len(sanitized_tuple_list)

    detected_gesture = moosegesture.getGesture(
        sanitized_tuple_list[int(trim_beginning_len):])
    # print(f'Gesture is :- {detected_gesture}')
    closest_match = moosegesture.findClosestMatchingGesture(detected_gesture,
                                                            tuple_gesture_keys,
                                                            maxDifference=4)

    if closest_match is None:
        return None
    notify(f"Gesture Detected :- {gesture_map[str(closest_match[0])]}")
    return gesture_map[str(closest_match[0])]
Example #17
0
            sys.exit()

        if event.type == MOUSEBUTTONDOWN:
            # on mouse down, erase the previous line and start drawing a new one
            mouseDown = True
            if len(points) > 2:
                startx, starty = points[0][0], points[0][1]
                for i in range(len(points)):
                    points[i] = (points[i][0] - startx, points[i][1] - starty)
            points = []
            storkeText = ''

        if event.type == MOUSEBUTTONUP:
            # try to identify the gesture when the mouse dragging stops
            mouseDown = False
            strokes = moosegesture.getGesture(points)
            segments = moosegesture.getSegments(points)
            strokeText = ' '.join(strokes)
            textobj = font.render(strokeText, 1, (255,255,255))
            textrect = textobj.get_rect()
            textrect.topleft = (10, WINDOWHEIGHT - 30)

        if event.type == MOUSEMOTION and mouseDown:
            # draw the line if the mouse is dragging
            points.append( (event.pos[0], event.pos[1]) )


    # Draw the window.
    windowSurface.fill(BACKGROUNDCOLOR)

    if strokeText:
Example #18
0
	#grabbing a frame
	_,img=cap.read()

	#preprocessing
	gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
	ret,th1 =cv2.threshold(gray,thresh,255,cv2.THRESH_TOZERO)
	cv2.imshow('threshold',th1)
	
	# contour detection and getting the contours with minArea and maxArea
	_,contours,hierarchy = cv2.findContours(th1.copy(),cv2.RETR_EXTERNAL, 2)
	cnts=ut.getContourBiggerThan(contours,minArea=3000,maxArea=40000)

	#if no object in the frame check the trail list for any previous trail
	if len(cnts)==0:
		#get the gesture
		strokes = mges.getGesture(trail)
		trail=[]
		index=0
		maxLen=0
		for i in xrange(0,len(strokes)):
			if strokes[i]>maxLen:
				maxLen=strokes[i]
				index=i

		#find the gesture with the maximum consistent stroke length
		if maxLen>0:
			strokeText = mges.getGestureStr(strokes)
			strokeText=strokeText.split(' ')[index]
			gesFound=10

Example #19
0
def main():
    global FPSCLOCK, WINDOWSURF, BASICFONT, BEEP1, BEEP2, BEEP3, BEEP4

    pygame.init()
    FPSCLOCK = pygame.time.Clock()
    WINDOWSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
    pygame.display.set_caption('Simon Gesture')

    BASICFONT = pygame.font.Font('freesansbold.ttf', 16)

    # set up surf and rect for instructional text
    infoSurf = BASICFONT.render('Drag the mouse to match the directional pattern. Press M to toggle music.', 1, LIGHTGRAY)
    infoRect = infoSurf.get_rect()
    infoRect.topleft = (10, WINDOWHEIGHT - 25)

    musicLoaded = True
    try:
        # This mp3 can be downloaded from http://www.freesound.org/people/ERH/sounds/30192/
        pygame.mixer.music.load(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'simonbackground.mp3'))
        pygame.mixer.music.play(-1, 0.0)
    except pygame.error:
        musicLoaded = False

    musicPlaying = True
    newGame = True

    while True: # main game loop
        if newGame:
            # Initialize some variables
            score = 0
            # when False, the pattern is playing. when True, waiting for the player to click a colored button:
            waitingForInput = False

            # sequence must be calculated in advance so we know the dimensions, and thus the start point
            seq = []
            for i in range(MAXGESTURES):
                seq.append(addToSequence(seq))
            mouseDown = False
            mousex, mousey = None, None
            playerMouseMovement = [] # a list of (x, y) tuples of mouse positions the player has moved
            mouseJustReleased = False
            newGame = False

        # basic drawing stuff
        WINDOWSURF.fill(BGCOLOR)

        scoreSurf = BASICFONT.render('Score: ' + str(score), 1, LIGHTGRAY)
        scoreRect = scoreSurf.get_rect()
        scoreRect.topleft = (WINDOWWIDTH - 100, 10)
        WINDOWSURF.blit(scoreSurf, scoreRect)

        WINDOWSURF.blit(infoSurf, infoRect)

        checkForQuit()
        mousex, mousey = None, None # stores where the last mouse motion event occurred.
        for event in pygame.event.get(): # event handling loop
            if event.type == MOUSEMOTION:
                mousex, mousey = event.pos
            elif event.type == MOUSEBUTTONUP:
                mouseDown = False
                mouseJustReleased = True
            elif event.type == MOUSEBUTTONDOWN:
                mouseDown = True
            elif event.type == KEYDOWN and event.key == K_m:
                 # toggle music
                musicPlaying = not musicPlaying
                if musicLoaded:
                    if musicPlaying:
                        pygame.mixer.music.play(-1, 0.0)
                    else:
                        pygame.mixer.music.stop()

        if mouseJustReleased:
            mouseJustReleased = False
            # see if the gesture matches
            gestures = moosegesture.getGesture(playerMouseMovement)
            if gestures != seq[:score+1]:
                # gesture didn't match
                drawGameOver()
                newGame = True

            waitingForInput = False
            score += 1
            continue

        if not waitingForInput:
            # show the animation sequence
            animateSequence(seq, score+1)
            waitingForInput = True
            playerMouseMovement = []
        else:
            # let the player enter their response
            if mousex != None and mousey != None and mouseDown:
                playerMouseMovement.append( (mousex, mousey) )

            if len(playerMouseMovement) > 1:
                pygame.draw.lines(WINDOWSURF, BLACK, False, playerMouseMovement)
                gestures = moosegesture.getGesture(playerMouseMovement)
                if len(gestures) > 0:
                    drawArrow(int(WINDOWWIDTH / 2) - HALFARROWSIZE, WINDOWHEIGHT - 80, gestures[-1])
                if gestures != seq[:len(gestures)] or len(gestures) > score + 1:
                    drawGameOver()
                    newGame = True
                    continue

        pygame.display.update()
        FPSCLOCK.tick(FPS)
Example #20
0
def main():
    global FPSCLOCK, WINDOWSURF, BASICFONT, BEEP1, BEEP2, BEEP3, BEEP4

    pygame.init()
    FPSCLOCK = pygame.time.Clock()
    WINDOWSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
    pygame.display.set_caption('Simon Gesture')

    BASICFONT = pygame.font.Font('freesansbold.ttf', 16)

    # set up surf and rect for instructional text
    infoSurf = BASICFONT.render(
        'Drag the mouse to match the directional pattern. Press M to toggle music.',
        1, LIGHTGRAY)
    infoRect = infoSurf.get_rect()
    infoRect.topleft = (10, WINDOWHEIGHT - 25)

    musicLoaded = True
    try:
        # This mp3 can be downloaded from http://www.freesound.org/people/ERH/sounds/30192/
        pygame.mixer.music.load(
            os.path.join(os.path.dirname(os.path.realpath(__file__)),
                         'simonbackground.mp3'))
        pygame.mixer.music.play(-1, 0.0)
    except pygame.error:
        musicLoaded = False

    musicPlaying = True
    newGame = True

    while True:  # main game loop
        if newGame:
            # Initialize some variables
            score = 0
            # when False, the pattern is playing. when True, waiting for the player to click a colored button:
            waitingForInput = False

            # sequence must be calculated in advance so we know the dimensions, and thus the start point
            seq = []
            for i in range(MAXGESTURES):
                seq.append(addToSequence(seq))
            mouseDown = False
            mousex, mousey = None, None
            playerMouseMovement = [
            ]  # a list of (x, y) tuples of mouse positions the player has moved
            mouseJustReleased = False
            newGame = False

        # basic drawing stuff
        WINDOWSURF.fill(BGCOLOR)

        scoreSurf = BASICFONT.render('Score: ' + str(score), 1, LIGHTGRAY)
        scoreRect = scoreSurf.get_rect()
        scoreRect.topleft = (WINDOWWIDTH - 100, 10)
        WINDOWSURF.blit(scoreSurf, scoreRect)

        WINDOWSURF.blit(infoSurf, infoRect)

        checkForQuit()
        mousex, mousey = None, None  # stores where the last mouse motion event occurred.
        for event in pygame.event.get():  # event handling loop
            if event.type == MOUSEMOTION:
                mousex, mousey = event.pos
            elif event.type == MOUSEBUTTONUP:
                mouseDown = False
                mouseJustReleased = True
            elif event.type == MOUSEBUTTONDOWN:
                mouseDown = True
            elif event.type == KEYDOWN and event.key == K_m:
                # toggle music
                musicPlaying = not musicPlaying
                if musicLoaded:
                    if musicPlaying:
                        pygame.mixer.music.play(-1, 0.0)
                    else:
                        pygame.mixer.music.stop()

        if mouseJustReleased:
            mouseJustReleased = False
            # see if the gesture matches
            gestures = moosegesture.getGesture(playerMouseMovement)
            if gestures != seq[:score + 1]:
                # gesture didn't match
                drawGameOver()
                newGame = True

            waitingForInput = False
            score += 1
            continue

        if not waitingForInput:
            # show the animation sequence
            animateSequence(seq, score + 1)
            waitingForInput = True
            playerMouseMovement = []
        else:
            # let the player enter their response
            if mousex != None and mousey != None and mouseDown:
                playerMouseMovement.append((mousex, mousey))

            if len(playerMouseMovement) > 1:
                pygame.draw.lines(WINDOWSURF, BLACK, False,
                                  playerMouseMovement)
                gestures = moosegesture.getGesture(playerMouseMovement)
                if len(gestures) > 0:
                    drawArrow(
                        int(WINDOWWIDTH / 2) - HALFARROWSIZE,
                        WINDOWHEIGHT - 80, gestures[-1])
                if gestures != seq[:len(gestures)] or len(
                        gestures) > score + 1:
                    drawGameOver()
                    newGame = True
                    continue

        pygame.display.update()
        FPSCLOCK.tick(FPS)
Example #21
0
    #grabbing a frame
    _, img = cap.read()

    #preprocessing
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret, th1 = cv2.threshold(gray, thresh, 255, cv2.THRESH_TOZERO)
    cv2.imshow('threshold', th1)

    # contour detection and getting the contours with minArea and maxArea
    _, contours, hierarchy = cv2.findContours(th1.copy(), cv2.RETR_EXTERNAL, 2)
    cnts = ut.getContourBiggerThan(contours, minArea=3000, maxArea=40000)

    #if no object in the frame check the trail list for any previous trail
    if len(cnts) == 0:
        #get the gesture
        strokes = mges.getGesture(trail)
        trail = []
        index = 0
        maxLen = 0
        for i in xrange(0, len(strokes)):
            if strokes[i] > maxLen:
                maxLen = strokes[i]
                index = i

        #find the gesture with the maximum consistent stroke length
        if maxLen > 0:
            strokeText = mges.getGestureStr(strokes)
            strokeText = strokeText.split(' ')[index]
            gesFound = 10

    #if gesture found show the gesture text for the next 10 frames