def __init__(self): try: self.main_path = os.getcwd() print('main path: {}'.format(self.main_path)) settings = { "static_path": os.path.join(self.main_path, 'public'), "xsrf_cookies": True, } # initialize camera, gpio and so on self.camera = Camera() self.cGPIO = ControlGPIO() self.collage = Collage(1024, 720, 20) self.new_collage = Subject(None) self.action = Subject(None) self.running = threading.Event() loop1 = asyncio.new_event_loop() self.th_wait_and_capture = threading.Thread( target=self.wait_and_capture, args=[loop1, self.running]) self.th_wait_and_capture.start() loop2 = asyncio.new_event_loop() self.th_btns_input = threading.Thread( target=self.process_btn_action, args=[loop2, self.running]) self.th_btns_input.start() url_patterns.append((r"/data/(.*)/?", BaseStaticFileHandler, dict(path=settings['static_path']))) print('urls: ', url_patterns) tornado.web.Application.__init__(self, url_patterns, **settings) self.io_loop = tornado.ioloop.IOLoop.current() except Exception as e: print('exception in main, ', e)
def __init__(self, surface: pygame.Surface): self._screen = surface self._state = EditorState() self._widgets = self._build_widgets() self._uimanager = pygame_gui.UIManager(surface.get_size()) self._focus = Vec(surface.get_size()) // 2 self._camera = Camera(self._focus, surface.get_size()) self._camera.set_method(Follow(self._camera))
def main(): print("Running Task") camera = Camera() d = Detector() rs = d.detect_image(camera.capture()) print(rs) for obj, confidence in rs.items(): print('Object {} : Confidence {}'.format(obj, confidence))
def __init__(self, ID, debug, testing): # Device Setup self.camera = Camera() self.debug = debug self.testing = testing self.formatter = logging.Formatter( "\%(asctime)s \%(levelname)s \%(identifier)s \%(message)s\n") self.ID = ID # Arduino Debug if self.debug == 'n': self.arduino = Arduino() # Connect to Server self.init_conn()
def __init__(self, surface: pygame.Surface, infection_probability, proximity, recovery_probability, gravitation_tick, lockdown_strength): self._screen = surface self._widgets = self._build_widgets() self._uimanager = pygame_gui.UIManager(surface.get_size()) self._focus = Vec(surface.get_size()) // 2 self._camera = Camera(self._focus, surface.get_size()) self._camera.set_method(Follow(self._camera)) home_poi = POI("home", (0, 0, 255)) center_poi = POI("center", (255, 225, 255)) agent_poi = POI("agent", (0, 0, 255)) self._available_points = { home_poi.get_name(): home_poi, center_poi.get_name(): center_poi } self._simulation = Simulation('output/points.csv', infection_probability, proximity, recovery_probability, gravitation_tick, lockdown_strength)
def __init__(self, steering_model='', dataset_path=''): self.motors = Motors(*config['left_motor'].values(), *config['right_motor'].values()) self.camera = Camera() self.ultrasonic_sensor = UltrasonicSensor( *config['ultrasonic_sensor'].values()) self.joystick = None self.stop_sign_detector = SignDetector( r'training/stop sign detection/stopsign_classifier.xml') if not args.mode: self.joystick = Joystick() self.dataset_path = dataset_path if not dataset_path: self.dataset_path = os.path.join(os.getcwd(), 'dataset/') if steering_model: self.interpreter = tf.lite.Interpreter(model_path=steering_model) self.input_details = self.interpreter.get_input_details() self.output_details = self.interpreter.get_output_details() self.interpreter.allocate_tensors()
class DoorLock: def __init__(self, ID, debug, testing): # Device Setup self.camera = Camera() self.debug = debug self.testing = testing self.formatter = logging.Formatter( "\%(asctime)s \%(levelname)s \%(identifier)s \%(message)s\n") self.ID = ID # Arduino Debug if self.debug == 'n': self.arduino = Arduino() # Connect to Server self.init_conn() """ Init Server Connection """ def init_conn(self): SERVER_ADDRESS = '192.168.0.21' PORT = 8018 SERVER_PASSWORD = "******" connected = False if self.testing == 'n': while not connected: # TCP socket connection self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: self.socket.connect((SERVER_ADDRESS, PORT)) # Verifies server self.socket.sendall( self.make_packet("DATA", SERVER_PASSWORD)) response = self.socket.recv(4096) if response: response_hdr, response_msg, response_sdr = self.parse_packet( response) if response_hdr == "ERROR" and response_msg == "IDENTIFY FAILED": raise Exception("PASSWORD FAIL") elif response_hdr == "DATA" and response_msg == "CONNECTED": connected = True else: raise Exception("CONNECTION FAIL") except Exception as e: if e == "PASSWORD FAIL": print( "DEBUG: server connection failed (invalid credentials)" ) print("DEBUG: quitting") break else: print(e) print( "DEBUG: server connection failed (could not connect), trying again in 10s" ) time.sleep(10) else: print("DEBUG: socket setup skipped") """ Main Loop """ def run(self): while True: ##### Arduino ##### try: # Enter pin print("DEBUG: RUNNING") print("DEBUG: Please enter pin: ") # input to simulate Arduino if self.debug == 'n': inp = self.arduino.read() print(inp) print(len(inp[0])) if len(inp[0]): print("in") self.socket.setblocking(True) arduino_input_header, arduino_input = inp[0], inp[1] # the header of the input is DATA if arduino_input_header == "DATA": # if the header is DATA and MSG is BUZZ, go through buzz subroutin if arduino_input == "BUZZ": # $ buzz the owner self.buzzSubroutine() continue else: # four digit number is entered (passcode) then go through pin check subroutine self.pinCheckSubroutine(arduino_input) continue ##### Listen for command from server ##### # wait for 1 second to receive else: self.socket.settimeout(1) # check if command was received try: cmd = self.socket.recv(4096) print(cmd) except socket.timeout: continue else: cmd_hdr, cmd_msg, cmd_sdr = self.parse_packet(cmd) # if door is locked from the remote, lock door and print "Door Locked" if cmd_hdr == "CMD": if cmd_msg == "LOCK DOOR": if self.debug == 'n': self.arduino.write("Door Locked") # if door is unlocked from the remote, door is unlocked and "Door Unlocked" is printed elif cmd_msg == "UNLOCK DOOR": if self.debug == 'n': self.arduino.write("Door Unlocked") except (KeyboardInterrupt, SystemExit): self.socket.sendall(self.make_packet("CMD", "SHUTTING DOWN")) raise except Exception as e: print(e) break """ Buzzer Subroutine. """ def buzzSubroutine(self): # preloaded picture is sent to the server pic_num = self.camera.takePicture() print("PIC {}".format(pic_num)) self.socket.sendall(self.make_packet("CMD", "BUZZ")) response = self.socket.recv(4096) if response: response_hdr, response_msg, response_sdr = self.parse_packet( response) if response_hdr == "ACK" and response_msg == "BUZZ": pic_file = open("{}.png".format(pic_num), 'rb') pic_bytes = pic_file.read(1024) while pic_bytes: self.socket.send(pic_bytes) pic_bytes = pic_file.read(1024) pic_file.close() confirm = self.socket.recv(4096) # confirmation message when picture is received at the server end. if confirm: confirm_hdr, confirm_msg, confirm_sdr = self.parse_packet( confirm) if confirm_hdr == "DATA" and confirm_msg == "PICTURE RECEIVED": print("DEBUG: confirmed receive") """ Pin Check Subroutine to handle pin check. """ def pinCheckSubroutine(self, pin): self.socket.sendall(self.make_packet("CMD", "PIN CHECK")) response = self.socket.recv(4096) if response: response_hdr, response_msg, response_sdr = self.parse_packet( response) if response_hdr == "ACK" and response_msg == "PIN CHECK": # ready to send PIN to server self.socket.sendall(self.make_packet("DATA", pin)) pin_check = self.socket.recv(4096) if pin_check: pin_check_header, pin_check_str, pin_check_sender = self.parse_packet( pin_check) # Wrong pin was entered and access denied message is printed. if pin_check_header == "DATA": if pin_check_str == "PIN CHECK FAIL": print("DEBUG: wrong pin, go away.") # Take a picture and send it to server pic_num = self.camera.takePicture() else: # Correct pin was entered and access was granted. if self.debug == 'n': self.arduino.write("AG" + pin_check_str) ##### Utility Functions ##### """ Make packet. """ def make_packet(self, type, data): return ("{}\x00{}\x00{}".format(type, data, self.ID)).encode() """ Deconstruct packet. """ def parse_packet(self, data): return data.decode().split('\x00') """ Create log to send to server. """ def create_log(self, exc): return self.formatter.formatException(exc)
class SimulationPygame: ''' Simulation ''' def __init__(self, surface: pygame.Surface, infection_probability, proximity, recovery_probability, gravitation_tick, lockdown_strength): self._screen = surface self._widgets = self._build_widgets() self._uimanager = pygame_gui.UIManager(surface.get_size()) self._focus = Vec(surface.get_size()) // 2 self._camera = Camera(self._focus, surface.get_size()) self._camera.set_method(Follow(self._camera)) home_poi = POI("home", (0, 0, 255)) center_poi = POI("center", (255, 225, 255)) agent_poi = POI("agent", (0, 0, 255)) self._available_points = { home_poi.get_name(): home_poi, center_poi.get_name(): center_poi } self._simulation = Simulation('output/points.csv', infection_probability, proximity, recovery_probability, gravitation_tick, lockdown_strength) def _build_widgets(self): return [] def _process_keys(self): keys = pygame.key.get_pressed() offset = { pygame.K_w: (0, -1), pygame.K_s: (0, 1), pygame.K_a: (-1, 0), pygame.K_d: (1, 0) } speed_factor = (8 if keys[pygame.K_LCTRL] else 2 if keys[pygame.K_LSHIFT] else 4) for key in offset: if keys[key]: self._focus.update(self._focus + (Vec(offset[key]) * speed_factor)) def listen(self, events: List[pygame.event.Event], time_delta_): ''' Listen for events Args: events: queue of pygame events ''' mouse_pos = Vec(pygame.mouse.get_pos()) for widget in self._widgets: widget.listen(events, time_delta_) self._process_keys() def draw(self): ''' Draw components in the screen ''' self._camera.scroll() for center in self._simulation.get_centers(): screen.blit(self._available_points['center'].get_icon(), center + self._camera.offset) for home in self._simulation.get_homes(): screen.blit(self._available_points['home'].get_icon(), home + self._camera.offset) for agent in self._simulation.get_agents(): agent_surface = pygame.Surface((5, 5)) if agent.situation == "susceptible": agent_surface.fill((0, 255, 0)) else: agent_surface.fill((255, 0, 0)) screen.blit(agent_surface, agent.pos + self._camera.offset) for widget in self._widgets: widget.draw()
class Editor: ''' Simulation map editor ''' def __init__(self, surface: pygame.Surface): self._screen = surface self._state = EditorState() self._widgets = self._build_widgets() self._uimanager = pygame_gui.UIManager(surface.get_size()) self._focus = Vec(surface.get_size()) // 2 self._camera = Camera(self._focus, surface.get_size()) self._camera.set_method(Follow(self._camera)) def _build_widgets(self): names = self._state.get_poi_names() on_clicks = [partial( self._state.set_poi, name) for name in names + [None]] button_array = ButtonArray((100, 10), (200, 20), self._screen, (len(names)+1, 1), border=0, texts=(names+['nenhum']), onClicks=on_clicks) button_save = Button((310, 10), (60, 20), screen, text='salvar', onClick=self._state.save) button_load = Button((380, 10), (60, 20), screen, text='carregar', onClick=self._ask_for_file) return [button_array, button_save, button_load] def _ask_for_file(self): self._state.set_poi(None) file_explorer = FileExplorer((0,0), (300, 300), self._screen, {'select': self._load_file, 'cancel':self._cancel_load}) self._widgets.append(file_explorer) def _load_file(self, file_path): self._state.load(file_path) del self._widgets[-1] def _cancel_load(self): del self._widgets[-1] def _process_click(self, mouse_pos) -> bool: [_, _, right] = self._state.last_pressed_mouse points = self._state.get_points() poi = self._state.get_poi() if poi is None: if not right: return True for point in points: point_rect = pygame.Rect(point.pos, point.get_size()) if point_rect.collidepoint(mouse_pos): self._state.remove_point(point) else: poi = poi.copy() poi.pos -= self._camera.offset new_point_rect = pygame.Rect(poi.pos, poi.get_size()) for point in points: point_rect = pygame.Rect(point.pos, poi.get_size()) collided_point = point_rect.colliderect(new_point_rect) if collided_point: if right: self._state.remove_point(point) break else: if right: return True new_point_rect = pygame.Rect(poi.pos, poi.get_size()) widgets_rects = [ w.get_rect() for w in self._widgets] collided_widget = sum([ 1 for r in widgets_rects if r.colliderect(new_point_rect)]) > 0 if not collided_widget: self._state.add_point(poi) return True return False def _process_keys(self): keys = pygame.key.get_pressed() offset = { pygame.K_w: (0,1), pygame.K_s: (0,-1), pygame.K_a: (1,0), pygame.K_d: (-1,0) } speed_factor = ( 8 if keys[pygame.K_LCTRL] else 2 if keys[pygame.K_LSHIFT] else 4) for key in offset: if keys[key]: self._focus.update(self._focus+(Vec(offset[key])*speed_factor)) def listen(self, events: List[pygame.event.Event], time_delta_): ''' Listen for events Args: events: queue of pygame events ''' mouse_pos = Vec(pygame.mouse.get_pos()) self._state.update_poi_pos(mouse_pos) for widget in self._widgets: widget.listen(events, time_delta_) for event in events: if event.type == pygame.MOUSEBUTTONUP: if self._process_click(mouse_pos): break if event.type == pygame.MOUSEBUTTONDOWN: self._state.last_pressed_mouse = pygame.mouse.get_pressed() break self._process_keys() def draw(self): ''' Draw components in the screen ''' self._camera.scroll() poi = self._state.get_poi() for point in self._state.get_points(): screen.blit(point.get_icon(), point.pos + self._camera.offset) if poi is not None: screen.blit(poi.get_icon(), poi.pos) for widget in self._widgets: widget.draw()
class AutonomousCar: def __init__(self, steering_model='', dataset_path=''): self.motors = Motors(*config['left_motor'].values(), *config['right_motor'].values()) self.camera = Camera() self.ultrasonic_sensor = UltrasonicSensor( *config['ultrasonic_sensor'].values()) self.joystick = None self.stop_sign_detector = SignDetector( r'training/stop sign detection/stopsign_classifier.xml') if not args.mode: self.joystick = Joystick() self.dataset_path = dataset_path if not dataset_path: self.dataset_path = os.path.join(os.getcwd(), 'dataset/') if steering_model: self.interpreter = tf.lite.Interpreter(model_path=steering_model) self.input_details = self.interpreter.get_input_details() self.output_details = self.interpreter.get_output_details() self.interpreter.allocate_tensors() def drive(self): print('Driving motors...') self.motors.start() while True: steering = 0 throttle = 0 frame = self.camera.get_frame() if self.ultrasonic_sensor.measure_distance() <= 15: throttle = 0 else: frame, found_sign = self.stop_sign_detector.detect_signs(frame) if found_sign: print('Found stop sign, waiting for 3 seconds') self.motors.move(0, 0, 0.1) sleep(3) print('Resuming drive') else: steering = self.predict_steering(frame) throttle = IDLE_THROTTLE print(f'Throttle: {throttle}, Steering: {steering}') self.motors.move(throttle, (steering * TURN_SENSITIVITY), 0.1) self.camera.show_frame(frame, preview=args.preview) def collect_dataset(self): print('Collecting dataset...') data_collector = DataCollector(self.dataset_path) self.motors.start() collect_data = False collecting_data = False while True: joystick_values = self.joystick.get_buttons() throttle = joystick_values[config['drive_axis']] if joystick_values[PAUSE_DATASET_BTN]: collect_data = False if joystick_values[RESUME_DATASET_BTN]: collect_data = True if collect_data and not collecting_data: data_collector.start() collecting_data = True if not collect_data and collecting_data: data_collector.stop() collecting_data = False if collecting_data: frame = self.camera.get_frame(preview=args.preview) data_collector.save_frame(frame, joystick_values[config['turn_axis']]) throttle = IDLE_THROTTLE self.motors.move( throttle, (joystick_values[config['turn_axis']] * TURN_SENSITIVITY), 0.1) def predict_steering(self, frame): frame = frame.astype(np.float32) / 255.0 frame, _ = preprocess(frame) self.interpreter.set_tensor(self.input_details[0]['index'], [frame]) self.interpreter.invoke() output_data = self.interpreter.get_tensor( self.output_details[0]['index']) return float(output_data) def exit(self): self.motors.exit() self.ultrasonic_sensor.exit() self.camera.exit() gpio.cleanup()
class TornadoApplication(tornado.web.Application): """ Application definition """ def __init__(self): try: self.main_path = os.getcwd() print('main path: {}'.format(self.main_path)) settings = { "static_path": os.path.join(self.main_path, 'public'), "xsrf_cookies": True, } # initialize camera, gpio and so on self.camera = Camera() self.cGPIO = ControlGPIO() self.collage = Collage(1024, 720, 20) self.new_collage = Subject(None) self.action = Subject(None) self.running = threading.Event() loop1 = asyncio.new_event_loop() self.th_wait_and_capture = threading.Thread( target=self.wait_and_capture, args=[loop1, self.running]) self.th_wait_and_capture.start() loop2 = asyncio.new_event_loop() self.th_btns_input = threading.Thread( target=self.process_btn_action, args=[loop2, self.running]) self.th_btns_input.start() url_patterns.append((r"/data/(.*)/?", BaseStaticFileHandler, dict(path=settings['static_path']))) print('urls: ', url_patterns) tornado.web.Application.__init__(self, url_patterns, **settings) self.io_loop = tornado.ioloop.IOLoop.current() except Exception as e: print('exception in main, ', e) def stop(self): try: self.running.set() print('stopping camera') self.camera.stop() except Exception as e: print('Exception deleting camara components ', e) try: self.cGPIO.stop() except Exception as e: print('Exception deleting cgpio components ', e) self.th_wait_and_capture.join() def process_btn_action(self, loop, running_cond): asyncio.set_event_loop(loop) while not running_cond.is_set(): #btn_reset = self.cGPIO.btn_new_round() btn_left = False btn_right = False btn_left = self.cGPIO.btn_left() btn_right = self.cGPIO.btn_right() #if btn_reset: # print('sending action NEW') # self.new_collage.next(ActionsWebSocket.NEW) if not btn_left: print('sending action NEXT') self.action.next(ActionsWebSocket.NEXT) elif not btn_right: print('sending action BACK') self.action.next(ActionsWebSocket.BACK) time.sleep(2) def wait_and_capture(self, loop, running_cond): print('starting...') asyncio.set_event_loop(loop) self.capturing = False self.cGPIO.prepare() self.path = self.camera.prepare() self.cGPIO.set_ready_led(True) while not running_cond.is_set(): btn_state = self.cGPIO.btn_new_round() if not btn_state and not self.capturing: print('Starting new round!') self.cGPIO.set_leds_to(True) time.sleep(0.3) self.cGPIO.set_leds_to(False) time.sleep(0.3) self.cGPIO.set_leds_to(True) time.sleep(0.3) self.cGPIO.set_leds_to(False) self.capturing = True self.start_new_round() self.capturing = False self.cGPIO.set_ready_led(True) time.sleep(.1) print("finishing wait and capture thread") def start_new_round(self): a_images = [] i = 0 self.cGPIO.set_ready_led(True) print('Waiting for photobtn ') while i < 4: btn_state = self.cGPIO.btn_new_photo() if not btn_state: print('Starting new photo!') self.cGPIO.set_ready_led(False) self.cGPIO.turn_on_led(i, True) a_images.append(self.camera.capture(i)) time.sleep(0.5) i += 1 self.cGPIO.set_ready_led(True) time.sleep(.1) self.cGPIO.set_ready_led(False) print(a_images) imgname = self.collage.create(self.path, a_images) self.new_collage.next(imgname) time.sleep(.5) self.cGPIO.set_leds_to(False)
def play_game(): world = World() player = world.create_entity(Renderable(char='@', layer=Layers.ENTITIES), Position(x=10, y=10), Action(cost=1), Camera(width=20, height=20), Damager(attack_power=100), Physics(collidable=True), Inventory(26), Health(), Joystick(), Condition(), name='Player') world.create_entity(GameMap(player=player)) world.create_entity(Item(coast=5, weight=4), Position(x=1, y=10), Renderable(char='-', color='blue', layer=Layers.ITEMS), Physics(), name='Dagger') world.create_entity(Renderable(char='s', color='red', layer=Layers.ENTITIES), Position(x=19, y=10), Action(cost=3), Physics(collidable=True), Damager(), Health(), Condition(), Ai(NPC()), name='Worm') world.create_entity(Renderable(char='g', color='green', layer=Layers.ENTITIES), Position(x=2, y=10), Action(cost=2), Physics(collidable=True), Damager(), Health(), Condition(), Ai(NPC()), name='Goblin') processors = [ InputProcessor, MapProcessor, ActionProcessor, PhysicProcessor, ConditionProcessor, EventSystem, CameraProcessor, RenderProcessor ] for prior, processor in enumerate(processors): world.add_processor(processor(), priority=prior) world.get_processor(MapProcessor).get_current_map() world.get_processor(MapProcessor).process() world.get_processor(CameraProcessor).get_camera() world.get_processor(CameraProcessor).process() world.get_processor(EventSystem).process() world.get_processor(RenderProcessor).process() blt.refresh() while True: world.process()
""" Teste de camera """ # from typing import List import pygame from pygame.locals import QUIT from pygame.math import Vector2 as Vec from components.camera import Camera, Follow # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~-~-~-~-~-~-~-~- SETUP ~-~-~-~-~-~-~-~- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pygame.init() screen = pygame.display.set_mode((600, 600)) focus = Vec(400, 400) camera = Camera(focus, (1000, 1000)) follow = Follow(camera) camera.set_method(follow) pygame.display.set_caption('Teste') clock = pygame.time.Clock() focus.update(0, 0) point = pygame.Surface((10, 10)) point.fill((255, 0, 0)) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~-~-~-~-~-~-~-~- VARIABLES ~-~-~-~-~-~-~-~- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ while True: events = pygame.event.get() for event in events: if event.type == QUIT: pygame.quit() # <----- HOLD KEYS ------> keys = pygame.key.get_pressed()