def __init__( self, test_img=[], test_video=[], test_display=False, using_nt=False, ): # self.entries = entries # Memory Allocation # Numpy takes Rows then Cols as dimensions. Height x Width self.hsv = np.zeros(shape=(FRAME_HEIGHT, FRAME_WIDTH, 3), dtype=np.uint8) self.image = self.hsv.copy() self.display = self.hsv.copy() self.mask = np.zeros(shape=(FRAME_HEIGHT, FRAME_WIDTH), dtype=np.uint8) # Camera Configuration self.CameraManager = CameraManager(test_img=test_img, test_video=test_video, test_display=test_display) self.testing = len(test_video) or len(test_img) if not self.testing: self.CameraManager.setCameraProperty( 0, "white_balance_temperature_auto", 0) self.CameraManager.setCameraProperty(0, "exposure_auto", 1) self.CameraManager.setCameraProperty(0, "focus_auto", 0) self.CameraManager.setCameraProperty(0, "exposure_absolute", 1) self.Connection = Connection(using_nt=using_nt, test=self.testing) self.avg_horiz_angle = 0 self.avg_dist = 0 self.prev_dist = 0 self.prev_horiz_angle = 0 self.old_fps_time = 0
def __init__(self): self.id = 0 self.feed_amount = 0 self.event_time = '' self.has_run = 'false' self.images = [] self.servo_man = ServoManager() self.camera_man = CameraManager()
def restart(self): self.world.unload_map_layer(carla.MapLayer.All) # Get a random blueprint. blueprint = random.choice( self.world.get_blueprint_library().filter('vehicle.audi.a2')) blueprint.set_attribute('role_name', self.actor_role_name) if blueprint.has_attribute('color'): blueprint.set_attribute('color', '224,0,0') if blueprint.has_attribute('is_invincible'): blueprint.set_attribute('is_invincible', 'true') # Spawn the vehicle. while self.vehicle is None: #or self.player2 is None or self.player3 is None: if not self.map.get_spawn_points(): print('There are no spawn points available in your map/town.') print('Please add some Vehicle Spawn Point to your UE4 scene.') sys.exit(1) spawn_point = carla.Transform( carla.Location(x=initial_state["x"], y=initial_state["y"], z=initial_state["z"]), carla.Rotation(yaw=initial_state["yaw"]), ) self.vehicle = self.world.try_spawn_actor(blueprint, spawn_point) # spawing objects for player_id in range(1): while self.players[player_id] is None: spawn_point = carla.Transform( carla.Location( x=initial_state["x"] + self.players_location[player_id][0], y=initial_state["y"] + self.players_location[player_id][1], z=initial_state["z"], ), carla.Rotation(yaw=initial_state["yaw"] + self.players_location[player_id][2]), ) self.players[player_id] = self.world.try_spawn_actor( blueprint, spawn_point) # Set up the sensors. self.camera_manager = CameraManager(self.vehicle) # self.camera_manager.set_sensor(0, 0, notify=False) # main camera self.camera_manager.set_sensor(0, 1, notify=False) # front camera self.camera_manager.set_sensor(1, 1, notify=False) # depth camera self.camera_manager.set_sensor(2, 1, notify=False) # segmentation camera print("[+] World setup completed")
def __init__(self, baudrate, camera=CameraManager((512, 512)), timeout=1): self.baudrate = baudrate self.camera = camera self.timeout = timeout self.mapping = {0: 'r', 1: 'l', 2: 'b', 3: 'f', 4: 'None'} self.model = None self.input_details = None self.output_details = None self.port = None self.serial_connection = None self._start_port_conn() self._load_model()
def start_secure(): for i in range(retry_count): try: logger.info('retrying') time.sleep(5) new_socket = socket.socket() new_socket.connect((tilda_ip, tilda_port)) logger.info('connecting to {}:{}...'.format(tilda_ip, tilda_port)) new_connection = new_socket.makefile('wb') logger.info('connected to {}:{}'.format(tilda_ip, tilda_port)) new_streamer = ImageStreamer(new_connection) logger.info('Image streamer created ') camera_manager = CameraManager(new_streamer) logger.info('camera_manager created ') camera_manager.start_capturing() new_streamer.terminated = True new_streamer.join() logger.info('new_connection write last') new_connection.write(struct.pack('<L', 0)) logger.info('connection write ends') except Exception as e: t, value, t2 = sys.exc_info() logger.error('Exception when retrying streaming {} {}'.format( t, value), exc_info=True) traceback.print_tb(e.__traceback__) try: logger.info( f'Total images sent {camera_manager.get_total_images_count()} on fps {camera_manager.get_fps()}' ) except: pass finally: try: new_socket.close() new_connection.close() except Exception as e: t, value, t2 = sys.exc_info() logger.error( 'Exception when retrying to close connection and socket {} {}' .format(t, value), exc_info=True) traceback.print_tb(e.__traceback__)
def __init__( self, test_img=None, test_video=None, test_display=False, using_nt=False, ): # self.entries = entries # Memory Allocation self.hsv = np.zeros(shape=(FRAME_WIDTH, FRAME_HEIGHT, 3), dtype=np.uint8) self.image = self.hsv.copy() self.mask = np.zeros(shape=(FRAME_WIDTH, FRAME_HEIGHT), dtype=np.uint8) # Camera Configuration self.CameraManager = CameraManager(test_img=test_img, test_video=test_video, test_display=test_display) self.Connection = Connection(using_nt=using_nt, test=test_video or test_img) self.testing = not (type(test_img) == type(None) or type(test_video) == type(None))
def get_images(): cMgr = CameraManager() json_data = cMgr.getImages() return json.dumps(json_data)
if not ( abs(inner["area"] / cv2.contourArea(inner["rect"]) - 1) < RAW_RECT_AREA_ERROR ): return False, inner, outer if not ( abs(outer["area"] / cv2.contourArea(outer["rect"]) - 1) < RAW_RECT_AREA_ERROR ): return False, inner, outer return True, inner, outer def draw_contour_pair(self, inner: dict, outer: dict) -> None: """Draw the inner and outer contours on self.image""" self.image = cv2.drawContours( self.image, inner["rect"].reshape((1, 4, 2)), -1, (255, 0, 0), thickness=2 ) self.image = cv2.drawContours( self.image, outer["rect"].reshape((1, 4, 2)), -1, (0, 0, 255), thickness=2 ) if __name__ == "__main__": vision = Vision( CameraManager("Loading Bay Camera", "/dev/video0", 240, 320, 30, "kYUYV"), NTConnection(), ) while True: vision.run()
import asyncio # monkey patch for timer coroutines import nest_asyncio nest_asyncio.apply() from aiohttp import web from aiojobs.aiohttp import atomic import aiohttp_cors import socketio import json import time from camera_manager import CameraManager mgmt = CameraManager() sio = socketio.AsyncServer(async_mode='aiohttp', cors_allowed_origins="*") app = web.Application() cors = aiohttp_cors.setup(app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", ) }) sio.attach(app)
from camera_manager import CameraManager camera_man = CameraManager() camera_man.take_picture()
'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack', 'umbrella', 'N/A', 'N/A', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'N/A', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'N/A', 'dining table', 'N/A', 'N/A', 'toilet', 'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'N/A', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush' ] colorizer = rs.colorizer() cam_manager = CameraManager() model = jetson.inference.detectNet('ssd-mobilenet-v2', threshold=0.4) time.sleep(2) while True: start_time = time.time() rgb_frame, depth_frame = cam_manager.get_frames() detections = model.Detect(jetson.utils.cudaFromNumpy(rgb_frame)) for detection in detections: text = "{} {}%".format(COCO_INSTANCE_CATEGORY_NAMES[detection.ClassID], detection.Confidence) print(text) barcodes = pyzbar.decode(rgb_frame) # loop over the detected barcodes
outer["rect"] = get_corners_from_contour(outer["contour"]) if not len(outer["rect"]) == 4: return False, inner, outer if not (abs(inner["area"] / cv2.contourArea(inner["rect"]) - 1) < RAW_RECT_AREA_ERROR): return False, inner, outer if not (abs(outer["area"] / cv2.contourArea(outer["rect"]) - 1) < RAW_RECT_AREA_ERROR): return False, inner, outer return True, inner, outer def draw_contour_pair(self, inner: dict, outer: dict) -> None: """Draw the inner and outer contours on self.image""" self.image = cv2.drawContours(self.image, inner["rect"].reshape((1, 4, 2)), -1, (255, 0, 0), thickness=2) self.image = cv2.drawContours(self.image, outer["rect"].reshape((1, 4, 2)), -1, (0, 0, 255), thickness=2) if __name__ == "__main__": vision = Vision(CameraManager(), NTConnection()) while True: vision.run()
When ran, takes image, processes image, and sends results to RIO. """ self.connection.pong() frame_time, self.frame = self.camera_manager.get_frame() if frame_time == 0: self.camera_manager.notify_error(self.camera_manager.get_error()) return # Flip the image cause originally upside down. self.frame = cv2.rotate(self.frame, cv2.ROTATE_180) results = self.get_image_values(self.frame) self.connection.set_fps() if results is not None: distance, angle = results self.connection.send_results( (distance, angle, time.monotonic() )) # distance (meters), angle (radians), timestamp self.camera_manager.send_frame(self.display) if __name__ == "__main__": vision = Vision( CameraManager("Power Port Camera", "/dev/video0", 240, 320, 30, "kYUYV"), NTConnection(), ) while True: vision.run()