def generate_transformations(): trs = [] for p1 in GridTransformation.config.rotation_range[ 12::18]: # 6 [-30, 30] for p2 in GridTransformation.config.translate_range[ 1::2]: # 1 [-3, 3] for p2_v in GridTransformation.config.translate_range[ 1::2]: # 1 [-3, 3] for p3 in GridTransformation.config.shear_range[ 8::12]: # 4 [-20, 20] if GridTransformation.config.enable_filters: for p4 in GridTransformation.config.zoom_range[ 4::6]: # 2 [-10, 10] for p5 in GridTransformation.config.blur_range[:: 1]: # for p6 in GridTransformation.config.brightness_range[ 4::12]: # 4 [-16, 16] for p7 in GridTransformation.config.contrast_range[ 2::6]: # 2 [-8, 8] tr = Transformation( p1, p2, p2_v, p3, p4, p5, p6, p7) trs.append(tr) else: tr = Transformation(p1, p2, p2_v, p3) trs.append(tr) return trs
def _process_trace(self, t, reverse=False): l1 = len(t.trace) l2 = len(t.trace[0]) swap_groups = [] cols_used = set() for i in range(l1 - 1): prev_row = -1 for j in range(l2 - 1): if t.trace[i][j]: if prev_row != i: if j not in cols_used: if reverse: grp = Transformation([t.s2[i]], [t.s1[j]]) cols_used.add(j) else: grp = Transformation([t.s1[j]], [t.s2[i]]) cols_used.add(j) swap_groups.append(grp) prev_row = i else: if reverse: grp = Transformation([t.s1[j]], []) else: grp = Transformation([], [t.s2[i]]) swap_groups.append(grp) prev_row = i else: if reverse: swap_groups[len(swap_groups) - 1].right.append( t.s1[j]) else: swap_groups[len(swap_groups) - 1].left.append( t.s1[j]) return swap_groups
def parse(src, p, color): m = Matrix() with open(src, "r") as raw: commands = raw.readlines() cmdbuf = '' t = Transformation() for cmd in commands: if cmd == 'line\n': cmdbuf = 'line' elif cmd == 'ident\n': t = Transformation() cmdbuf = '' elif cmd == 'scale\n': cmdbuf = 'scale' elif cmd == 'move\n': cmdbuf = 'move' elif cmd == 'rotate\n': cmdbuf = 'rotate' elif cmd == 'apply\n': t.apply(m) t = Transformation() cmdbuf = '' elif cmd == 'display\n': p.clear() l = Line(p, color) l.draw(m) p.display() cmdbuf = '' elif cmd == 'save\n': cmdbuf = 'save' elif cmd == 'quit\n': break else: args = cmd.split() if cmdbuf == 'line': m.addEdge((float(args[0]), float(args[1]), float(args[2])), (float(args[3]), float(args[4]), float(args[5]))) elif cmdbuf == 'scale': t.scale(float(args[0]), float(args[1]), float(args[2])) elif cmdbuf == 'move': t.move(float(args[0]), float(args[1]), float(args[2])) elif cmdbuf == 'rotate': t.rotate(args[0], float(args[1])) elif cmdbuf == 'save': p.clear() l = Line(p, color) l.draw(m) if args[0][-4:] == '.ppm': p.fname = args[0] p.commit() else: p.fname = args[0][:-4] + '.ppm' p.commit() subprocess.run(['convert', args[0][:-4] + '.ppm', args[0]]) subprocess.run(['rm', args[0][:-4] + '.ppm']) print(args[0]) cmdbuf = ''
def __init__(self, x_train=None, y_train=None, original_target=None): config = ExperimentalConfig.gen_config() self.config = config self.queue_set = [] # num_test * num_mutate self.queue_len = config.queue_len self.pt = Perturbator() self.original_target = original_target self.x_train = x_train self.y_train = y_train # using grid strategy to init population if not self.config.random_init: self.gt = GridTransformation(original_target.num_classes) # generate first population temp_x_original_train = copy.deepcopy(x_train) for i in range(len(temp_x_original_train)): label = np.argmax(y_train[i]) q = list() # add original data to initial population q.append(Item(Transformation(), 1)) # initialize loss as 1 for j in range(9): if self.config.random_init: # random init population angle = random.choice(config.rotation_range) translation = random.choice(config.translate_range) shear = random.choice(config.shear_range) # transformation based on filter if config.enable_filters: zoom = random.choice(config.zoom_range) blur = random.choice(config.blur_range) brightness = random.choice(config.brightness_range) contrast = random.choice(config.contrast_range) tr = Transformation(angle, translation, shear, zoom, blur, brightness, contrast) else: tr = Transformation(angle, translation, shear) q.append(Item(tr, 1)) else: tr = self.gt.get_next_transformation(label) q.append(Item(tr, 1)) self.queue_set.append(q)
def main(): if len(sys.argv) != 3: print('Error: Execution -> python3 main.py <url> <name_database>') exit(1) url = sys.argv[1] name_db = sys.argv[2] transformation = Transformation(url=url, output_path='databases/', name_db=name_db) transformation.transformation() load = Load(transformation.new_engine) load.load(output_path='excel/')
def __init__(self): self.vicon_lists = [] self.vicon_ts = [] self.vicon_x = [] self.vicon_y = [] self.vicon_z = [] self.vicon_qw = [] self.vicon_qx = [] self.vicon_qy = [] self.vicon_qz = [] self.opti_lists = [] self.opti_ts = [] self.opti_x = [] self.opti_y = [] self.opti_z = [] self.opti_qw = [] self.opti_qx = [] self.opti_qy = [] self.opti_qz = [] self.opti_orig_lists = [ ] # Original (i.e., opti to wand) before transformation self.opti_orig_ts = [] self.opti_orig_x = [] self.opti_orig_y = [] self.opti_orig_z = [] self.opti_orig_qw = [] self.opti_orig_qx = [] self.opti_orig_qy = [] self.opti_orig_qz = [] self.T_ts = [] self.T_x = [] self.T_y = [] self.T_z = [] self.T_qw = [] self.T_qx = [] self.T_qy = [] self.T_qz = [] self.T_vicon_to_opti_positions = [] self.T_vicon_to_opti_quats = [] self.T_obj = Transformation()
def animate_cfg(cfg: ConfigFile, identity=False): corr_markers = cfg.markers # List of vertex-tuples (source, target) if identity: corr_markers = np.ascontiguousarray( np.array((corr_markers[:, 0], corr_markers[:, 0]), dtype=np.int).T) original_source = meshlib.Mesh.load(cfg.source.reference) original_target = meshlib.Mesh.load(cfg.target.reference) if identity: original_target = meshlib.Mesh.load(cfg.source.reference) mapping = get_correspondence(original_source, original_target, corr_markers) transf = Transformation(original_source, original_target, mapping, smoothness=1) animate(transf, list(cfg.source.load_poses()))
data_dict = CreateDictionary(data_table, data_type).initialize_dic() runtime = time.time() # Pop the csv object to reduce memory usage del data_table print("Runtime : %.4f" % (runtime - startTime)) # Create Column Combination column_combination = ColumnCombination(data_dict).create_combination() print("Column combination Created.") runtime2 = time.time() print("Runtime : %.4f" % (runtime2 - runtime)) # Create Scenario Dictionary - Transformation + Guessing Scenario value scenario_dict = Transformation(data_dict, column_combination).transformation() print("Scenario dictionary created") runtime3 = time.time() print("Runtime : %.4f" % (runtime3 - runtime2)) # Calculate Scenario score and Rank. Top 20 will be printed picked_scenario = Rank(scenario_dict).rank() # Final Time Check endTime = time.time() - startTime json = JsonGenerator(picked_scenario).generate_json() print("Program Runtime : %.4f" % endTime) print(json)
def generateActions(self, world, market): finish = False actions = [] untradeable_resources = [ 'R1', 'R4', 'R7', 'R19', 'R21', 'R22', "R1'", "R5'", "R6'", "R18'", "R19'", "R20'", "R21'", "R22'", "cash" ] while not finish: print("Awaiting command: ") string = str(input()) tokens = string.split() command = tokens[0].lower() if command == "buy": if len(tokens) < 3: print("Missing parameters") else: ticker = tokens[1].upper() if ticker in untradeable_resources: print("Cannot buy " + ticker) else: orders = [] for i in range(2, len(tokens)): order = tokens[i].split(",") content = {} strike = float(order[0]) content["strike"] = strike quantity = int(order[1]) content["quantity"] = quantity if len(order) >= 3: expiration = int(order[2]) content["expiration"] = expiration orders.append(content) transaction = Transaction(self.name, "buy", {ticker: orders}, market) actions.append(transaction) print("Submitted:") print(transaction.toString()) elif command == "sell": if len(tokens) < 3: print("Missing parameters") else: ticker = tokens[1].upper() if ticker in untradeable_resources: print("Cannot sell " + ticker) else: orders = [] for i in range(2, len(tokens)): order = tokens[i].split(",") content = {} strike = float(order[0]) content["strike"] = strike quantity = int(order[1]) content["quantity"] = quantity if len(order) >= 3: expiration = int(order[2]) content["expiration"] = expiration orders.append(content) transaction = Transaction(self.name, "sell", {ticker: orders}, market) actions.append(transaction) print("Submitted:") print(transaction.toString()) elif command == "show": if len(tokens) > 1: flag = tokens[1].lower() if flag == "-w": print("World State:", world) elif flag == "-m": market.printOrderBook() elif flag == "-t": if len(tokens) < 3: print("Missing parameters") else: tickers = tokens[2].split(",") for i in range(0, len(tickers)): ticker = tickers[i].upper() print(ticker, market.quotePrice(ticker)) elif flag == "-tm": print("housing:", housing) print("alloys:", alloys) print("electronics:", electronics) print("farms:", farms) print("factories:", factories) print("metallic_elements:", metallic_elements) print("timber:", timber) print("plant:", plant) else: print("Illgeal flag") else: print("My State:", world[self.name]) elif command == "mine": if len(tokens) < 3: print("missing parameters") else: resource = tokens[1].upper() difficulty = int(tokens[2]) mine = Mine(self, resource, difficulty) print("Submitted: ", mine.toString()) actions.append(mine) elif command == "transform": if len(tokens) < 3: print("missing parameters") else: t = tokens[1].lower() multiplier = int(tokens[2]) template = "" if t == "housing": template = housing if t == "alloys": template = alloys if t == "electronics": template = electronics if t == "farms": template = farms if t == "factories": template = factories if t == "metallic_elements": template = metallic_elements if t == "timber": template = timber if t == "plant": template = plant if template: transformation = Transformation( self.name, template, multiplier) print("Submitted: ", transformation.toString()) actions.append(transformation) else: print("Illegal template") # Congratulations! You have found the easter egg! elif command == "greedisgood": money = 5000 if len(tokens) >= 2: money = float(tokens[1]) world[self.name]["cash"] = world[self.name]["cash"] + money print("You gave yourself " + str(money) + " cash. Go crazy!") elif command == "end": finish = True elif command == "help": print("Here's a list of the commands: ") print("1. show [-w] [-m] [-t (t1,t2,t3...)] [-tm]") print( "2. buy (ticker) (strike1,quantity1,[expiration1]) (strike2,quantity2,[expiration2]) ..." ) print( "3. sell (ticker) (strike1,quantity1,[expiration1]) (strike2,quantity2,[expiration2]) ..." ) print("4. mine (resource) (difficulty)") print("5. transform (template) (multiplier)") print("6. end") else: print("Illegal command") print() return actions
# Load meshes original_source = meshlib.Mesh.load(cfg.source.reference) original_pose = meshlib.Mesh.load(cfg.source.poses[0]) original_target = meshlib.Mesh.load(cfg.target.reference) if identity: original_target = meshlib.Mesh.load(cfg.source.reference) ######################################################### # Load correspondence from cache if possible mapping = get_correspondence(original_source, original_target, corr_markers, plot=False) transf = Transformation(original_source, original_target, mapping) result = transf(original_pose) path = f"result/{name}" os.makedirs(path, exist_ok=True) plt.MeshPlots.plot_correspondence(original_source, original_target, mapping).finalize().write_html( os.path.join(path, "correspondence.html"), include_plotlyjs="cdn") plt_res.plot(original_source, original_target).write_html(os.path.join( path, "reference.html"), include_plotlyjs="cdn") plt_res.plot(original_pose, result).write_html(os.path.join(path, "result.html"),
return args if __name__ == '__main__': # v_args = GetArgs() # v_input_file_type = v_args.input_file_type v_input_file_type = "csv" # Import Data : class get_data obj_get_data = getdata() source_df = obj_get_data.ReadCSVData() # Transformation : class transformation obj_transformation = Transformation(source_df) obj_transformation.set_time_readable() obj_transformation.set_medium_source_path() top_source_medium_df = obj_transformation.final_dataframe() #v_name = top_source_medium_df. print(top_source_medium_df) distinctusers_batch = obj_transformation.execute_metrics(10000) print(distinctusers_batch) distinctusers_day = obj_transformation.calc_distinctusers_perday() print(distinctusers_day) # Save Output based on required format obj_SaveOutput = SaveOutput(top_source_medium_df, 'top_source_medium') obj_SaveOutput.save_data()
def __init__(self, dataSource, dataSet): trans_obj = Transformation(dataSource, dataSet)
def run(self): self.tracker1.getVideo(self.Video1) self.tracker2.getVideo(self.Video2) self.db.connect() self._id = 0 self.idChecker = CamId() while True: if self.stopC == 1: break center1 = self.tracker1.trackFrame() center2 = self.tracker2.trackFrame() if center1 == -1 or center2 == -1: break if center1 == None: scaledCenter = Transformation( center2, self.trapezium.getTrapeziumBaseLarge(), self.trapezium.getTrapeziumBaseSmall(), self.trapezium.getTrapeziumHeight(), self.trapezium.getZeroPointPosition()) self.center = Rotate(scaledCenter) delta = None self.camInd = "Вторая" elif center2 == None: scaledCenter = Transformation( center1, self.trapezium.getTrapeziumBaseLarge(), self.trapezium.getTrapeziumBaseSmall(), self.trapezium.getTrapeziumHeight(), self.trapezium.getZeroPointPosition()) self.center = scaledCenter delta = None self.camInd = "Первая" else: scaledCenter1 = Transformation( center1, self.trapezium.getTrapeziumBaseLarge(), self.trapezium.getTrapeziumBaseSmall(), self.trapezium.getTrapeziumHeight(), self.trapezium.getZeroPointPosition()) scaledCenter2 = Transformation( center2, self.trapezium.getTrapeziumBaseLarge(), self.trapezium.getTrapeziumBaseSmall(), self.trapezium.getTrapeziumHeight(), self.trapezium.getZeroPointPosition()) delta = Compare(scaledCenter1, scaledCenter2) self.center = ([ int((scaledCenter1[0] + scaledCenter2[0]) / 2), int((scaledCenter1[1] + scaledCenter2[1]) / 2) ]) delta = [int(delta[0]), int(delta[1])] self.camInd = "Обе" self.center = [int(self.center[0]), int(self.center[1])] if not self.idChecker.isCurrent(self.center): self._id = self._id + 1 self.GetNewCoordinatesInt.emit(self.center[0], self.center[1]) self.GetNewCoordinatesStr.emit("Позиция = " + str(self.center) + " Камера: " + self.camInd + " Объект: " + str(self._id)) self.db.vrite(self.center, delta, self.camInd, self._id) if self.runIndicator != 1: self.runIndicator = 1 self.db.commit() self.db.disconnect() self.tracker1.stop() self.tracker2.stop() if self.stopC != 0: self.stopC = 0 if self.runIndicator != 0: self.runIndicator = 0
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) if __name__ == '__main__': strings_list = sys.argv if len(strings_list) < 2: print("Please pass in at least one argument") exit() action = f"{strings_list[1]}" output_format = "" if len(strings_list) >= 3: output_format = f"{strings_list[2]}" transformation = Transformation(output_format) if action.lower().strip() == "device_tahmo_mapping": transformation.map_devices_to_tahmo_station() elif action.lower().strip() == "site_tahmo_mapping": transformation.map_sites_to_tahmo_station() elif action.lower().strip() == "sites_without_a_primary_device": transformation.get_sites_without_a_primary_device() elif action.lower().strip() == "missing_devices_on_bigquery": transformation.get_devices_not_up_to_date_on_big_query() elif action.lower().strip() == "update_primary_devices": transformation.update_primary_devices()
def main(args: list) -> int: parser = argparse.ArgumentParser() parser.add_argument('-f', '--file', default=0) parser.add_argument('-e', '--extent', default=-1, type=int) parser.add_argument('-s', '--shape', choices={ 'linear', 'square', 'circular', 'cubic', 'spherical', 'cylindrical' }, default='square') parser.add_argument('-b', '--filetype', choices={'string', 'binary'}, default='binary') parser.add_argument('-t', '--datatype', choices={'int', 'float'}, default='float') parser.add_argument('-l', '--slide', type=int, default=1) parser.add_argument('-g', '--gap', type=int, default=1) parsedargs = parser.parse_args(args[1:]) filename = parsedargs.file extent = parsedargs.extent shape = parsedargs.shape filetype = parsedargs.filetype slide = parsedargs.slide gap = parsedargs.gap WINDOW_WIDTH: int = 800 WINDOW_HEIGHT: int = 600 pygame.init() pygame.display.set_caption(f"View {filename}") pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) surface = pygame.display.get_surface() zoom = 1.0 distance = 1000 xshift = WINDOW_WIDTH / 2 yshift = WINDOW_HEIGHT / 2 extent_s = '' changed = True # TODO: read in as strings if filetype == 'binary': with open(filename, 'rb') as bf: bdata: bytes = bf.read() darray = array.array('B') darray.frombytes(bdata) data = darray.tolist() elif filetype == 'string': with open(filename, 'r') as f: fdata: str = f.read() data = list(map(int, fdata.split())) def zeros(): while True: yield 0 # map data if shape == 'linear': points = np.array(tuple(zip(data[:], zeros(), zeros())), dtype='float64') if shape == 'square': points = np.array(tuple(zip(data[:], data[1:], zeros())), dtype='float64') if shape == 'circular': points = np.array(tuple(zip(data[:], data[1:], zeros())), dtype='float64') if shape == 'cubic': points = np.array(tuple(zip(data[:], data[1:], data[2:])), dtype='float64') if shape == 'spherical': points = np.array(tuple(zip(data[:], data[1:], data[2:])), dtype='float64') if shape == 'cylindrical': points = np.array(tuple(zip(data[:], data[1:], data[2:])), dtype='float64') transformation = Transformation() running = True while running: for event in pygame.event.get(): changed = True #print(event) if event.type == pygame.QUIT: running = False break if event.type == pygame.KEYDOWN: # fov if event.key == pygame.K_EQUALS: distance += 500 if event.key == pygame.K_MINUS: if distance > 500: distance -= 500 # rotations if event.key == pygame.K_w: transformation *= rotate_x(-15) if event.key == pygame.K_s: transformation *= rotate_x(15) if event.key == pygame.K_a: transformation *= rotate_y(-15) if event.key == pygame.K_d: transformation *= rotate_y(15) if event.key == pygame.K_q: transformation *= rotate_z(15) if event.key == pygame.K_e: transformation *= rotate_z(-15) # translations if event.key == pygame.K_UP: ... if event.key == pygame.K_DOWN: ... if event.key == pygame.K_LEFT: ... if event.key == pygame.K_RIGHT: ... screen_translation = (xshift, yshift, distance) if changed: changed = False render_matrix = make_render_matrix(shape, points, transformation) renderables = make_renderables(render_matrix, screen_translation) render(renderables, surface) pygame.display.flip() surface.fill((0, 0, 0)) ''' if event.key == pygame.K_EQUALS: if zoom >= 1: zoom += 1 else: zoom += 0.1 elif event.key == pygame.K_MINUS: if zoom > 1: zoom -= 1 else: zoom -= 0.1 elif event.key == pygame.K_UP: yshift += abs(8 * zoom) elif event.key == pygame.K_DOWN: yshift -= abs(8 * zoom) elif event.key == pygame.K_LEFT: xshift += abs(8 * zoom) elif event.key == pygame.K_RIGHT: xshift -= abs(8 * zoom) elif event.key == pygame.K_w: x = rotation[0] x += 0.1 # x %= 2 rotation[0] = x elif event.key == pygame.K_s: x = rotation[0] x -= 0.1 # x %= 2 rotation[0] = x elif event.key == pygame.K_a: y = rotation[1] y -= 0.1 # y %= 2 rotation[1] = y elif event.key == pygame.K_d: y = rotation[1] y += 0.1 # y %= 2 rotation[1] = y elif event.key == pygame.K_LEFTBRACKET and pygame.key.get_mods() & pygame.KMOD_SHIFT: if gap > 1: gap -= 1 elif event.key == pygame.K_RIGHTBRACKET and pygame.key.get_mods() & pygame.KMOD_SHIFT: gap += 1 elif event.key == pygame.K_LEFTBRACKET: if slide > 1: slide -= 1 elif event.key == pygame.K_RIGHTBRACKET: slide += 1 elif pygame.key.get_mods() & pygame.KMOD_SHIFT and event.key == pygame.K_1: shape = 'linear-extent' extent_s = '' elif pygame.key.get_mods() & pygame.KMOD_SHIFT and event.key == pygame.K_2: shape = 'square' elif pygame.key.get_mods() & pygame.KMOD_SHIFT and event.key == pygame.K_3: shape = 'circular' elif pygame.key.get_mods() & pygame.KMOD_SHIFT and event.key == pygame.K_4: shape = 'cubic' elif pygame.key.get_mods() & pygame.KMOD_SHIFT and event.key == pygame.K_5: shape = 'spherical' elif pygame.key.get_mods() & pygame.KMOD_SHIFT and event.key == pygame.K_6: shape = 'cylindrical' # elif event.key == pygame.K_q: # running = False if shape == 'linear-extent': if event.key == pygame.K_0: extent_s += '0' elif event.key == pygame.K_1: extent_s += '1' elif event.key == pygame.K_2: extent_s += '2' elif event.key == pygame.K_3: extent_s += '3' elif event.key == pygame.K_4: extent_s += '4' elif event.key == pygame.K_5: extent_s += '5' elif event.key == pygame.K_6: extent_s += '6' elif event.key == pygame.K_7: extent_s += '7' elif event.key == pygame.K_8: extent_s += '8' elif event.key == pygame.K_9: extent_s += '9' elif event.key == pygame.K_RETURN: extent = int(extent_s) extent_s = '' ''' ''' if changed: changed = False transform:list = [] if shape == 'linear-extent' and extent > 0: transform = transform_data_extent(data, extent) elif shape == 'square': transform = transform_data_square(data, slide, gap) elif shape == 'circular': transform = transform_data_circular(data, slide, gap) elif shape == 'cubic': transform = transform_data_cubic(data, slide, gap, rotation, zoom) draw_guide_cube(surface, xshift, yshift, rotation, zoom) elif shape == 'spherical': transform = transform_data_spherical(data, slide, gap, rotation, zoom) elif shape == 'cylindrical': transform = transform_data_cylindrical(data, slide, gap, rotation, zoom) # apply zoom and shift transform = general_transform(transform, zoom, xshift, yshift) # trim data which does not fit transform = trim(transform, WINDOW_WIDTH, WINDOW_HEIGHT) # DRAW DATA draw_points(surface, transform) pygame.display.flip() surface.fill((0,0,0)) ''' return 0
def runStream(videoUrl, model, sample_rate=0.1): """ Processes the prerecorded video, or webcam, and displays the analyzed video. The program processes only a few selected videoframes for performance reasons. Sample rate specifies what portion of the video is processed. A video frame if processed if video_frame_index mod 1/sample_rate is 0. At the end of the processing the program displays the density of the violations on a heatmap. Parameters ---------- videoUrl Path of the prerecorded video file, if '0' the program processes the webcam stream model YOLO type, must be yolov3, yolov3-tiny or yolov2-tiny sample_rate : float, optional Sample rate of processing, by default 0.1 """ vc = cv2.VideoCapture(videoUrl) frameWidth = 1920 // 2 frameHeight = 1080 // 2 with open('camera_conf.json') as f: data = json.load(f) cameraCallibrationArray = np.array(data['cameraCallibrationArray'], dtype = "float32") firstSectionToMeter = data['factorToMeter'] transformation = Transformation(cameraCallibrationArray, firstSectionToMeter, frameWidth, frameHeight) analyzer=Analyzer(transformation, model = model) print(videoUrl) last_time=time.perf_counter() counter=0 s=int(1/sample_rate) heatMap = np.zeros((frameWidth, frameHeight)) while True: ret, frame = vc.read() # img, width, height = peopleDetector.load_image_pixels(frame, frame.shape) if counter%s!=0: # process every s th frame counter+=1 continue if ret==False: break print(frame.shape) frame = cv2.resize(frame, (frameWidth, frameHeight)) analyzer.add_video_frame(frame) #(x,y,width,height) tuple #boxes, labels, scores = Detect(frame) for person in analyzer.activePeople: bbox=person.bounding_boxes[-1] if not(bbox is None): x,y,w,h = bbox.left, bbox.top, bbox.width, bbox.height frame=cv2.rectangle(frame, (x,y), (x+w, y+h), (0,255,0), 1) frame=cv2.putText(frame, f"Person {person.id}", (x+w+10, y+h), 0, 0.3, (0,255,0)) # add bbox center to heatmap for i in range(-5, 5): for j in range(-5, 5): if x + i > 0 and y + j > 0: heatMap[x + i][y + j] += 1 for violation in analyzer.violations: color = (255, 0, 0) if violation.accepted: color = (255, 255, 0) #print(violation.p1.getCoordinate().x) #print(violation.p1.getCoordinate().y) if(violation.p1.getCenter() == None or violation.p2.getCenter() == None): continue from_p = (int(violation.p1.getCenter()[0]), int(violation.p1.getCenter()[1])) to_p = (int(violation.p2.getCenter()[0]), int(violation.p2.getCenter()[1])) cv2.line(frame, from_p, to_p, color, 2) #Map view #cv2.rectangle(frame, (0,0), (frameWidth/5,frameHeight/5), (0,0,0), -1) #from_p = (int(violation.p1.getCoordinate().x/10), int(violation.p1.getCoordinate().y/10)) #to_p = (int(violation.p2.getCoordinate().x/10), int(violation.p2.getCoordinate().y/10)) #cv2.line(frame, from_p, to_p, color, 2) transformed = transformation.transformationMatrix width = transformation.transformedFrameWidth height = transformation.transformedFrameHeight warped = cv2.warpPerspective(frame, transformed, (int(width), int(height))) warped = cv2.resize(warped, (int(width/2), int(height/2))) cv2.imshow('Human detection example', frame) current_time=time.perf_counter() #print(f"{(s)/(current_time-last_time):0.4f} fps") last_time=current_time counter+=1 if cv2.waitKey(1) & 0xFF == ord('q'): break #Display created heatmap plt.imshow(heatMap, cmap='hot', interpolation='nearest') plt.show() #Release resources vc.release() cv2.destroyAllWindows()
def generateActions(self, world, market): # a list of the possible random actions actions = [] # Generate possible transforms transform_templates = [ housing, alloys, electronics, farms, factories, metallic_elements, timber, plant ] myResources = world[self.name] for template in transform_templates: max_multiplier = utils.calculate_transform_max_multiplier( myResources, template) if max_multiplier: transform = Transformation(self.name, template, max_multiplier) actions.append(transform) # Generate possible buys from the market untradeable_resources = [ 'R1', 'R4', 'R7', 'R19', 'R21', 'R22', "R1'", "R5'", "R6'", "R18'", "R19'", "R20'", "R21'", "R22'", "cash" ] resources = myResources.keys() for resource in resources: if resource not in untradeable_resources: sell_orders = market.getSellOrders(resource) for order in sell_orders: quantity = order["quantity"] price = order["strike"] seller = order["name"] quantity_to_buy = world[self.name]["cash"] // price # create a buy order of a random quantity of the resource up to the limit quantity_to_buy = random.randint(1, quantity_to_buy) if seller != self.name: transaction = Transaction( self.name, "buy", { resource: [{ "quantity": quantity_to_buy, "strike": price, "expiration": 3 }] }, market) actions.append(transaction) # Generate possible sells for the market for resource in resources: if resource not in untradeable_resources: quantity = myResources[resource] if quantity > 0: price = market.quotePrice(resource)["sellingPrice"] if price == float("-inf"): price = 30 # Create a sell order for a random amount of the resource that the country has at the market's price for it quantity = random.randint(1, quantity) transaction = Transaction( self.name, "sell", { resource: [{ "quantity": quantity, "strike": price, "expiration": 3 }] }, market) actions.append(transaction) # Generate possible random mine actions mineable_resources = ["R2", "R3", "R6"] for resource in mineable_resources: for difficulty in [1, 2, 3]: mine = Mine(self, resource, difficulty) actions.append(mine) # Return 3-5 random actions of all the possible actions random_actions = [] num_actions = random.randint(3, 5) has_mine = False for i in range(num_actions): while True: action = random.choice(actions) # If action was selected already if action in random_actions: continue # If a Mine action was already chosen elif isinstance(action, Mine) and has_mine: continue else: if isinstance(action, Mine): has_mine = True break random_actions.append(action) #print(self.name + ": " + action[0].toString()) return random_actions
The transformation phase includes the creation of a dictionary of words and preparing the data for the topic model software or package. The dictionary of words (also referred to as the vocabulary) is typically a list of unique words represented as integers. For example, ‘fish’ is 1, ‘population’ is 2 and so on. The length of the dictionary is thus the number of unique words within the corpus; normalization reduces the length of the dictionary, and speeds up the inference time. The next step is to represent the documents as bag-of-words features. Doing this for all the documents creates a matrix (i.e., table) with the rows being the individual documents, the columns being the words within the dictionary, and the cells being the frequency of that word within the document. This is one representation of bag-of-words features and other less sparse representations exist as well. If not performed during the pre-processing phase, words that occur only once, and words that occur in roughly 90% of the documents can be eliminated as they serve no discriminative topical significance. Especially omitting high frequently occurring words prevents such words from dominating all topics. Removing high and low-frequency words (i.e., pruning) within a matrix representation is generally much faster to perform. Several LDA tools are available and each of them requires a slightly different transformation step to make the data suitable for topic analysis. However, in essence, they require a conversion from words to bag-of-words representation, to some matrix representation of the full corpus. Several LDA packages exist that might be worth exploring: Gensim, Mallet, Stanford Topic Modeling Toolbox, Yahoo! LDA, and Mr. LDA. """ # instantiate Transformation class transformation = Transformation() # transform data to make it suitable for LDA analysis transformation.transform_for_lda() if DATAMINING: """ The data mining phase involves fitting or training the LDA model. It also involves a careful analysis of the hyper-parameters and the creation of different LDA models. Similarly to the transformation phase, the use of a certain LDA module or software tool determines what parameters and hyper-parameters can be adjusted. Since calculating a closed form solution of the LDA model is intractable, approximate posterior inference is used to create the distributions of words in topics, and topics in documents. To avoid local minima, both in the case of variational and sampling-based inference techniques, the initialization of the model is an important consideration in the data mining phase. Thus, regardless the initialization, and regardless the inference method used, to guard for problems of local minima, multiple starting points should be used to improve the stability of the inferred latent variables. Running the inference is the most important step in the data mining phase. It results in the discovery of the latent variables (words in topics, and topics in documents).
def DFA_module(input_data): """ Mock-up Ver. Dataset 1: TmaxDay_data Dataset 2: tmax_raw_data (from ERP) Dataset 3: Carcrash data """ # read CSV file format (may change) # f = open("./TmaxDay_data.csv", 'r', encoding='utf-8') # f = open("./tmax_raw_data.csv", 'r', encoding='utf-8') # f = open("./carcrash.csv", 'r', encoding='utf-8') # rdr = csv.reader(f) # data_table = [] # for line in rdr: # data_table.append(line) # # f.close() # get data_type (from meta in future) # data_type = ["tem", "cat", "cat", "num", "cat", "num", "num", "num", "num"] #tmaxday # csv_contents_type = ["cat", "cat", "cat", "cat", "cat", "cat", "cat", "cat", "cat", "cat", "cat", "tem", "tem", "num", "cat", "num", "num", "num", "cat"] #tmax_raw_data_set # csv_contents_type = ["cat", "cat", "cat", "cat", "num", "num", "num", "num", "num", "num"] #carcrash data_table = [] data_type = [] data_name = [] for i in range(len(input_data['meta'])): data_name.append(input_data['meta'][i]['name']) data_type.append(input_data['meta'][i]['type']) data_table.append(data_name) for i in range(len(input_data['data'])): data_table.append(input_data['data'][i]) # Time check startTime = time.time() # Create Column Data Dictionary data_dict = CreateDictionary(data_table, data_type).initialize_dic() runtime = time.time() # Pop the csv object to reduce memory usage del data_table print("Runtime : %.4f" % (runtime - startTime)) # Create Column Combination column_combination = ColumnCombination(data_dict).create_combination() print("Column combination Created.") runtime2 = time.time() print("Runtime : %.4f" % (runtime2 - runtime)) # Create Scenario Dictionary - Transformation + Guessing Scenario value scenario_dict = Transformation(data_dict, column_combination).transformation() print("Scenario dictionary created") runtime3 = time.time() print("Runtime : %.4f" % (runtime3 - runtime2)) # Calculate Scenario score and Rank. Top 20 will be printed picked_scenario = Rank(scenario_dict).rank() # Final Time Check endTime = time.time() - startTime json = JsonGenerator(picked_scenario).generate_json() print("Program Runtime : %.4f" % endTime) return json
# cv2.imwrite("plate.jpg", img_data.crop_gray) # cv2.imwrite("thresh.jpg", img_data.thresholds[0]) img_data = characteran.characteranalysis(img_data) if img_data.disqualified == True: print "low score" continue edgeFinder = EdgeFinder(img_data) img_data.plate_corners = edgeFinder.findEdgeCorners() img_data = edgeFinder.img_data img_data.plate_corners = img_data.plate_corners[0] expandRegion = img_data.regionOfInterest imgTransform = Transformation(img_data.grayImg, img_data.crop_gray, expandRegion.x, expandRegion.y, expandRegion.width, expandRegion.height) orcImageWidthPx = round(120.0 * 1.3333333) orcImageHeightPx = round(60 * 1.3333333) cropSize = imgTransform.getCropSize(img_data.plate_corners, (orcImageWidthPx, orcImageHeightPx)) transmtx = imgTransform.getTransformationMatrix1(img_data.plate_corners, cropSize[0], cropSize[1]) projectPoints = copy.deepcopy(img_data.plate_corners) projectPoints = np.array(projectPoints, np.float32) img_data.color_deskewed = np.zeros((cropSize[0], cropSize[1]), dtype=img_data.colorImg.dtype) cols1, rows1 = img_data.color_deskewed.shape deskewed_points = []
def _transform_grouped_sentence(self, t, s): flat_s = [ item for sublist in s for item in (sublist if type(sublist) is list else [sublist]) ] idx = 0 sl, sh = 0, 0 transformed = [] lefts = [] try: while sh <= len(flat_s): while not t[idx].matches(flat_s[sl:sh]): sh += 1 tr = t[idx].transform(flat_s[sl:sh]) transformed.append(tr) lefts.append(tr[0]) sl = sh idx += 1 except IndexError: pass s1 = [] while len(lefts) > 0: if lefts[0] == s[0]: s1.append(lefts[0]) lefts = lefts[1:] s = s[1:] elif len(lefts[0]) > len(s[0]): s1.append(lefts[0]) exp_len = len(lefts[0]) lefts = lefts[1:] while exp_len > 0: if len(s[0]) < exp_len: exp_len -= len(s[0]) s = s[1:] else: s = s[exp_len:] exp_len = 0 else: s1.append(s[0]) exp_len = len(s[0]) s = s[1:] while exp_len > 0: if len(lefts[0]) < exp_len: exp_len -= len(lefts[0]) lefts = lefts[1:] else: lefts = lefts[exp_len:] exp_len = 0 s2 = [] tid = 0 prods = [] for g in s1: l, h = 0, 0 x = [] while h <= len(g): try: trans = t[tid] while len(trans.left) == 0: prods.append(trans) tid += 1 trans = t[tid] except IndexError: break while not trans.matches(g[l:h]) and h <= len(g): h += 1 try: r = trans.transform(g[l:h]) prods.append(Transformation([r[1][0]], [r[1][0]])) x.append(r[1][0]) tid += 1 l = h except Exception: pass s2.append(x) return s2, s1, prods
def rotate_z(d) -> np.array: t = radians(d) return Transformation(((cos(t), -sin(t), 0, 0), (sin(t), cos(t), 0, 0), (0, 0, 1, 0), (0, 0, 0, 1)))
T_vicon_to_wand = np.dot(T_vicon_to_opti, T_opti_to_wand) opti_position, opti_quat = \ T_obj.convert_T_matrix_to_position_and_quat(T_vicon_to_wand) opti_list = [ ts, opti_position[0], opti_position[1], opti_position[2], opti_quat[0], opti_quat[1], opti_quat[2], opti_quat[3] ] data_obj.add_opti_data(opti_list) if __name__ == "__main__": # Class initialization data_obj = Data() T_obj = Transformation() draw_obj = Draw() T_vicon_to_opti = T_obj.return_T_vicon_to_opti() # Read data and store bag = rosbag.Bag( "../../data/data_trajectory/optitrack/2018-02-19-18-02-18.bag") # bag = rosbag.Bag("../../data/data_trajectory/optitrack/2018-02-19-18-10-05.bag") vicon_topic = "/dongki/vicon" opti_topic = "/Robot_2/pose" for topic, msg, t in bag.read_messages(topics=[opti_topic, vicon_topic]): if topic == "/dongki/vicon": vicon_cb(msg, data_obj) elif topic == "/Robot_2/pose": opti_cb(msg, T_vicon_to_opti, data_obj)
def parse(src, p, color): l = Line(p, color) m = Matrix() t = Transformation() param = Parametric(m, .0001) f = Polygon(m) fxns = { 'line': lambda args: m.addEdge((args[0], args[1], args[2]), (args[3], args[4], args[5])), 'scale': lambda args: t.scale(args[0], args[1], args[2]), 'move': lambda args: t.move(args[0], args[1], args[2]), 'rotate': lambda args: t.rotate(args[0], args[1]), 'save': lambda args: save(l, m, args), 'circle': lambda args: param.arc((args[0], args[1], args[2]), args[3]), 'hermite': lambda args: param.hermite((args[0], args[1]), (args[2], args[3]), (args[4], args[5]), (args[6], args[7])), 'bezier': lambda args: param.bezier((args[0], args[1]), (args[2], args[3]), (args[4], args[5]), (args[6], args[7])), 'box': lambda args: f.box(args[:3], args[3:6]), 'sphere': lambda args: f.sphere(args[:3], args[3]), 'torus': lambda args: f.torus(args[:3], args[3], args[4]), } with open(src, "r") as raw: commands = raw.readlines() cmdbuf = '' for cmd in commands: if cmd == 'line\n': cmdbuf = 'line' elif cmd == 'ident\n': t = Transformation() cmdbuf = '' elif cmd == 'scale\n': cmdbuf = 'scale' elif cmd == 'move\n': cmdbuf = 'move' elif cmd == 'rotate\n': cmdbuf = 'rotate' elif cmd == 'apply\n': print('apply') t.apply(m) t = Transformation() cmdbuf = '' elif cmd == 'display\n': print('display') p.clear() l.draw(m) p.display() cmdbuf = '' elif cmd == 'save\n': cmdbuf = 'save' elif cmd == 'quit\n': break elif cmd == 'circle\n': cmdbuf = 'circle' elif cmd == 'hermite\n': cmdbuf = 'hermite' elif cmd == 'bezier\n': cmdbuf = 'bezier' elif cmd == 'clear\n': print('clear') m = Matrix() f.matrix = m cmdbuf = '' elif cmd == 'box\n': cmdbuf = 'box' elif cmd == 'sphere\n': cmdbuf = 'sphere' elif cmd == 'torus\n': cmdbuf = 'torus' elif cmd[0] == '#': pass else: args = cmd.split() fargs = list() for i in range(len(args)): try: fargs.append(float(args[i])) except ValueError: fargs.append(args[i]) print(cmdbuf + ': ' + ','.join(args)) fxns[cmdbuf](fargs) cmdbuf = ''
def generateActions(self, world, market): resources_filename = "resources.xlsx" depth_bound = 5 frontier_max_size = 5 multiplier = 0.3 transform_templates = [ housing, alloys, electronics, farms, factories, metallic_elements, timber, plant ] worldWrapper = WorldWrapper(world, self.name, transform_templates, resources_filename) scheduler = Scheduler(worldWrapper) res = scheduler.search(depth_bound, frontier_max_size, multiplier) bestSchedule = heapq.heappop(res).getSchedule() # below is the wrapper table = str.maketrans(dict.fromkeys("()")) bestAction = bestSchedule[0][0].translate(table) tokens = bestAction.split() actions = [] if tokens[0] == "TRANSFER": fromName = tokens[2] ticker = tokens[5] quantity = int(tokens[6]) # convert a transfer that brings resource out to a market sell order if fromName == self.name: price = market.quotePrice(ticker)["sellingPrice"] # arbitrarily set the price so that we gain 25% of what we have in cash right now if price == float("-inf"): price = world[self.name]["cash"] / 4 / quantity transaction = Transaction( self.name, "sell", { ticker: [{ "quantity": quantity, "strike": price, "expiration": 2 }] }, market) actions.append(transaction) # convert a transfer that brings resource in to a market buy order else: price = market.quotePrice(ticker)["buyingPrice"] # arbitrarily use 25% of the money to bid for it if price == float("inf"): price = world[self.name]["cash"] / 4 / quantity # buy as much as possible else: quantity = int(world[self.name]["cash"] / price) if quantity != 0: transaction = Transaction( self.name, "buy", { ticker: [{ "quantity": quantity, "strike": price, "expiration": 2 }] }, market) actions.append(transaction) # if the country cannot afford to buy it, convert it to a mine action else: mine = Mine(self, ticker, 2) actions.append(mine) # convert a transform to a transformation if tokens[0] == "TRANSFORM": tokens = bestAction.split("||") inputs = tokens[1].split() outputs = tokens[2].split() multiplier = int(tokens[3].split()) template = {} template["in"] = {} template["out"] = {} for i in range(0, len(inputs) / 2): template["in"][inputs[i]] = int(inputs[i + 1]) for i in range(0, len(outputs) / 2): template["out"][outputs[i]] = int(outputs[i + 1]) transformation = Transformation(self.name, template, multiplier) actions.append(transformation) return actions