Пример #1
0
    def __init__(self):

        # Instantiate objects
        self.detector = MotionDetector()
        self.music_controls = Player()

        # Start video capture and initialize frames and variables
        self.capture = cv2.VideoCapture(0)

        self.capture.set(3, 640)
        self.capture.set(4, 480)

        self.ret1, self.frame1 = self.capture.read()
        self.ret2, self.frame2 = self.capture.read()

        self.thresh = self.frame1

        self.static_frame_counter = 0
        self.music_controls.start_music()

        self.room_status = 'Occupied'
        self.music_status = 'Playing'

        self.frame_count = 0
        self.start_time = time.time()

        # Set up thread for polling
        self.thread = Thread(target=self.poll, args=())
        self.thread.daemon = True
        self.thread.start()
Пример #2
0
 def __init__(self, screen, book):
     self.screen = screen
     self.clock = pygame.time.Clock()
     # init interface
     self.interface = Interface()
     # init books
     self.book = book
     # init the player
     self.music = Player(self.book)
Пример #3
0
def main(inmods, debug):
    inputs = []
    for mod in inmods:
        modname = 'input.l2m_' + mod
        inputs.append(getattr(__import__(modname, globals(), locals(),
                                         [mod.capitalize()], -1),
                              mod.capitalize())(debug=debug))

    state = LifeState(inputs, debug=debug)
    player = Player(state, debug=debug)
    player.play_song()
Пример #4
0
 def __init__(self, screen, book):
   self.screen = screen
   self.clock = pygame.time.Clock()
   # init interface 
   self.interface = Interface()
   # init books
   self.book = book
   # init the player 
   self.music = Player(self.book)
Пример #5
0
print "Build staffer: "
cur_time = time.time()
staffer = Staffer(image, tempo=TEMPO)
print "Completed in " + str(time.time() - cur_time) + " seconds."
print ""

print "Export wav."
cur_time = time.time()
staffer.export_wav("final.wav")
print "Completed in " + str(time.time() - cur_time) + " seconds."
print ""

print "Build player."
cur_time = time.time()
player = Player()
print "Completed in " + str(time.time() - cur_time) + " seconds."
print ""

player.play_file("final.wav")
player.terminate()

##def motion_detected(frameGray, previousFrame):
##
##	frameDelta = cv2.absdiff(previousFrame, gray)
##	thresh = cv2.threshold(frameDelta, THRESHOLD, 255, cv2.THRESH_BINARY)[1]
##	thresh = cv2.dilate(thresh, None, iterations=2)
##	(_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
##	cv2.CHAIN_APPROX_SIMPLE)
##
##	for c in cnts:
def main():

    # Instantiate objects
    detector = MotionDetector()
    music_controls = Player()

    # Start video capture and initialize frames and variables
    capture = cv2.VideoCapture(0)

    capture.set(3, 640)
    capture.set(4, 480)

    ret, frame1 = capture.read()
    ret, frame2 = capture.read()

    static_frame_counter = 0
    music_controls.start_music()

    room_status = 'Occupied'
    music_status = 'Playing'

    start = time.time()
    frames = 0

    # Loops until user hits ESC to exit the program
    while True:

        # Detect motion and output frame with text and motion tracking rectangle
        frame1, thresh = detector.detect_motion(frame1, frame2, room_status,
                                                music_status)
        cv2.imshow('Motion Detection', frame1)
        cv2.imshow('Differences', thresh)
        frame1 = frame2
        ret, frame2 = capture.read()

        # If the user is in the room, then start playing music if music was stopped
        if detector.user_in_room:
            static_frame_counter = 0
            if not music_controls.music_playing:
                music_controls.start_music()

            room_status = 'Occupied'
            music_status = 'Playing'

        # Music keeps playing if there is no more motion detected for up to ~2 seconds
        # In case user quickly walks off or stands/sits very still
        elif not detector.user_in_room and static_frame_counter < 30:
            static_frame_counter += 1
            room_status = 'Empty'
            music_status = 'Playing'

        # If no movement detected for >2 seconds, then pause music
        else:
            if music_controls.music_playing:
                music_controls.pause_music()

            room_status = 'Empty'
            music_status = 'Paused'

        frames += 1

        if cv2.waitKey(40) == 27:
            break

    cv2.destroyAllWindows()
    capture.release()
    music_controls.pause_music()
    print('FPS: ', frames / (time.time() - start))
Пример #7
0
class PlayerInterface:

    def __init__(self, screen, book):
      self.screen = screen
      self.clock = pygame.time.Clock()
      # init interface 
      self.interface = Interface()
      # init books
      self.book = book
      # init the player 
      self.music = Player(self.book)

    def on_click(self):
      ''' recognize touchscreen and mouse selections to 
      run functionalities of buttons '''
      click_pos = pressed()
      # stop music and return to previous screen
      if 10 <= click_pos[0] <= 55 and 190 <= click_pos[1] <= 235:
        self.music.stop()
        return False
      # play previous chapter
      if 65 <= click_pos[0] <= 110 and 190 <= click_pos[1] <= 235:
        self.music.previous_chapter()
      # play next chapter
      if 115 <= click_pos[0] <= 160 and 190 <= click_pos[1] <= 235:
        self.music.next_chapter()
      # pause/unpause the music
      if 165 <= click_pos[0] <= 210 and 190 <= click_pos[1] <= 235:
        self.music.pause()
      # stop the music the music        
      if 215 <= click_pos[0] <= 260 and 190 <= click_pos[1] <= 235:
        self.music.stop()
      # play the music
      if 265 <= click_pos[0] <= 310 and 190 <= click_pos[1] <= 235:
        self.music.play()
      # skip to selected position on the progress bar 
      if 10 <= click_pos[0] <= 310 and 160 <= click_pos[1] <= 185:
        self.music.set_pos(click_pos[0])
      return True

    def run(self):
      '''run method for drawing the screen to dispay'''
      mainloop = True
      while mainloop:
        # Limit frame speed to 30 FPS
        self.clock.tick(30)
        # draw interface to screen
        self.interface.player_interface(self.screen, self.book.get_title(),
          self.book.get_artist(), self.music.get_chapter() + 1, self.book.get_num_chapter(),
          self.book.get_chapter_playtime()[self.music.get_chapter()], 
          self.music.get_pos(), self.book.get_cover())
        for event in pygame.event.get():
          if event.type == pygame.USEREVENT:
            # start playing next chapter when a song ends
            self.music.next_chapter()
          # wait for touchscreen pressed
          if event.type == pygame.MOUSEBUTTONDOWN:
            # draw touchscreen feedback to screen
            pygame.draw.circle(self.screen, YELLOW, pressed(), 10, 0)
            # run functionalities
            mainloop = self.on_click()
        # update display
        pygame.display.flip()
Пример #8
0
class PlayerInterface:
    def __init__(self, screen, book):
        self.screen = screen
        self.clock = pygame.time.Clock()
        # init interface
        self.interface = Interface()
        # init books
        self.book = book
        # init the player
        self.music = Player(self.book)

    def on_click(self):
        ''' recognize touchscreen and mouse selections to 
      run functionalities of buttons '''
        click_pos = pressed()
        # stop music and return to previous screen
        if 10 <= click_pos[0] <= 55 and 190 <= click_pos[1] <= 235:
            self.music.stop()
            return False
        # play previous chapter
        if 65 <= click_pos[0] <= 110 and 190 <= click_pos[1] <= 235:
            self.music.previous_chapter()
        # play next chapter
        if 115 <= click_pos[0] <= 160 and 190 <= click_pos[1] <= 235:
            self.music.next_chapter()
        # pause/unpause the music
        if 165 <= click_pos[0] <= 210 and 190 <= click_pos[1] <= 235:
            self.music.pause()
        # stop the music the music
        if 215 <= click_pos[0] <= 260 and 190 <= click_pos[1] <= 235:
            self.music.stop()
        # play the music
        if 265 <= click_pos[0] <= 310 and 190 <= click_pos[1] <= 235:
            self.music.play()
        # skip to selected position on the progress bar
        if 10 <= click_pos[0] <= 310 and 160 <= click_pos[1] <= 185:
            self.music.set_pos(click_pos[0])
        return True

    def run(self):
        '''run method for drawing the screen to dispay'''
        mainloop = True
        while mainloop:
            # Limit frame speed to 30 FPS
            self.clock.tick(30)
            # draw interface to screen
            self.interface.player_interface(
                self.screen, self.book.get_title(), self.book.get_artist(),
                self.music.get_chapter() + 1, self.book.get_num_chapter(),
                self.book.get_chapter_playtime()[self.music.get_chapter()],
                self.music.get_pos(), self.book.get_cover())
            for event in pygame.event.get():
                if event.type == pygame.USEREVENT:
                    # start playing next chapter when a song ends
                    self.music.next_chapter()
                # wait for touchscreen pressed
                if event.type == pygame.MOUSEBUTTONDOWN:
                    # draw touchscreen feedback to screen
                    pygame.draw.circle(self.screen, YELLOW, pressed(), 10, 0)
                    # run functionalities
                    mainloop = self.on_click()
            # update display
            pygame.display.flip()
Пример #9
0
class SmartPlay(object):
    def __init__(self):

        # Instantiate objects
        self.detector = MotionDetector()
        self.music_controls = Player()

        # Start video capture and initialize frames and variables
        self.capture = cv2.VideoCapture(0)

        self.capture.set(3, 640)
        self.capture.set(4, 480)

        self.ret1, self.frame1 = self.capture.read()
        self.ret2, self.frame2 = self.capture.read()

        self.thresh = self.frame1

        self.static_frame_counter = 0
        self.music_controls.start_music()

        self.room_status = 'Occupied'
        self.music_status = 'Playing'

        self.frame_count = 0
        self.start_time = time.time()

        # Set up thread for polling
        self.thread = Thread(target=self.poll, args=())
        self.thread.daemon = True
        self.thread.start()

    def poll(self):
        '''
        Polls new frames from the webcam in a separate thread. Updates instance variables with newly polled frame for use in analysis and output.
        '''

        while True:
            self.ret2, self.frame2 = self.capture.read()

    def analyze(self):
        '''
        Analyze frames for motion, and adjust Spotify stream accordingly. Runs on main thread.
        '''
        self.frame1, self.thresh = self.detector.detect_motion(
            self.frame1, self.frame2, self.room_status, self.music_status)
        self.frame_show = self.frame1
        self.frame1 = self.frame2
        self.frame_count += 1

        # If the user is in the room, then start playing music if music was stopped
        if self.detector.user_in_room:
            self.static_frame_counter = 0
            if not self.music_controls.music_playing:
                self.music_controls.start_music()

            self.room_status = 'Occupied'
            self.music_status = 'Playing'

        # Music keeps playing if there is no more motion detected for up to ~2 seconds
        # In case user quickly walks off or stands/sits very still
        elif not self.detector.user_in_room and self.static_frame_counter < 30:
            self.static_frame_counter += 1
            self.room_status = 'Empty'
            self.music_status = 'Playing'

        # If no movement detected for >2 seconds, then pause music
        else:
            if self.music_controls.music_playing:
                self.music_controls.pause_music()

            self.room_status = 'Empty'
            self.music_status = 'Paused'

    def show_frame(self):
        '''
        Outputs windows for motion detection and diffs. Runs on main thread.
        '''

        cv2.imshow('Motion Detection', self.frame_show)
        cv2.imshow('Differences', self.thresh)

        # Escapes with ESC key
        if cv2.waitKey(40) == 27:
            self.stop()

    def stop(self):
        '''
        Close windows, stop video capture, and pause music
        '''
        self.capture.release()
        cv2.destroyAllWindows()
        self.music_controls.pause_music()
        print('FPS: ', self.frame_count / (time.time() - self.start_time))
        exit(1)