def test_split_rectangles(): for tc in test_cases: result = split_rectangles(tc) info = f'{tc} {result}' assert not utils.intersect(result), info assert utils.total_area(tc) == sum(r.area for r in result), info assert utils.bounding_box(tc) == utils.bounding_box(result), info
def run(): seed(time.time()) g = lambda: randint(1, 100) while True: n = randint(2, 100) print(f'n = {n}') recs = [Rect(g(), g(), g(), g()) for _ in range(n)] result = split_rectangles(recs) assert not utils.intersect(result) assert utils.bounding_box(recs) == utils.bounding_box(result) assert utils.total_area(recs) == sum(r.area for r in result)
def test_subtraction(): for a, b, _ in intersection_pairs: result = a - b result.extend(b - a) assert utils.bounding_box( [a, b]) == utils.bounding_box(result), f'{a} {b} {result}' assert sum( r.area for r in result) + a.collision_box(b).area == utils.total_area( [a, b]), f'{a} {b} {result}' a = Rect(0, 0, 2, 2) assert len(a - a) == 0
def get_viewbox(location, distance): boundary = utils.bounding_box(location["lat"], location["lon"], distance / 1000.0) return "{},{},{},{}".format(boundary['southwest']['lng'], boundary['southwest']['lat'], boundary['northeast']['lng'], boundary['northeast']['lat'])
def updateData(i): color = 'r' co = coords[i] for j, ax in enumerate(axs.flat): for p in ax.patches: p.remove() c = co[j] rect = bounding_box(c[0], c[1], patch_size, color) ax.add_patch(rect)
def main(plot_dir, epoch): # read in pickle files glimpses = pickle.load( open(plot_dir + "g_{}.p".format(epoch), "rb") ) locations = pickle.load( open(plot_dir + "l_{}.p".format(epoch), "rb") ) probas = pickle.load( open(plot_dir + "lg_{}.p".format(epoch), "rb") ) glimpses = np.concatenate(glimpses) # grab useful params size = 8 num_cols = glimpses.shape[0] img_shape = glimpses.shape[1] # denormalize coordinates coords = [denormalize(img_shape, l) for l in locations] fig, axs = plt.subplots(nrows=num_cols // 3, ncols=num_cols // 3) # fig.set_dpi(100) # plot base image for j, ax in enumerate(axs.flat): ax.imshow(glimpses[j], cmap="Greys_r") ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) names = [] for i in range(len(coords)): fig.suptitle('{} epoch, {} time'.format(epoch, i + 1)) color = 'r' co = coords[i] pr = probas[i].max(axis=1) for j, ax in enumerate(axs.flat): for p in ax.patches: p.remove() c = co[j] rect = bounding_box( c[0], c[1], size, color ) ax.set_title('Proba: {:.2f}%'.format(pr[j] * 100.),fontsize=10) ax.add_patch(rect) name = os.path.join(plot_dir, '{}_epoch{}.png'.format(i + 1, epoch)) plt.savefig(name) names.append(name) frames = [] for name in names: frames.append(imageio.imread(name)) os.remove(name) imageio.mimsave(os.path.join(plot_dir + 'epoch{}.gif'.format(epoch)), frames, 'GIF', duration=0.5)
def __getitem__(self, index: int): image_id = self.image_ids[index] image = cv2.imread(f'{self.image_dir}/{image_id}.jpg', cv2.IMREAD_COLOR) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32) image /= 255.0 if self.train: records = utils.annotation_filereader(f'{self.annotation_dir}/{image_id}.txt') target = utils.bounding_box(records).bbox_dict # boxes = records[['x', 'y', 'w', 'h']].values # boxes[:, 2] = boxes[:, 0] + boxes[:, 2] # boxes[:, 3] = boxes[:, 1] + boxes[:, 3] # # area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0]) # area = torch.as_tensor(area, dtype=torch.float32) # # # there is only one class # labels = torch.ones((records.shape[0],), dtype=torch.int64) # suppose all instances are not crowd iscrowd = torch.zeros((records.shape[0],), dtype=torch.int64) # target = {} # target['boxes'] = boxes # target['labels'] = labels # target['masks'] = None target['image_id'] = torch.tensor([index]) # target['area'] = area target['iscrowd'] = iscrowd if self.transforms: if not self.train: sample = { 'image': image, } else: sample = { 'image': image, 'bboxes': target['boxes'], 'labels': target['labels'] } # print(sample['image'].shape) # print(sample['image']) image1 = self.transforms(sample['image']) # image = sample['image'] # target['boxes'] = torch.stack(tuple(map(torch.tensor, zip(*sample['bboxes'])))).permute(1, 0) if not self.train: return image1, image_id else: return image1, target, image_id
def updateData(i): #print("i",i) color = 'r' co = coords[i] #print(size, co) for j, ax in enumerate(axs.flat): for p in ax.patches: p.remove() print(j) c = co[j] rect = bounding_box(c[0], c[1], size, color) ax.add_patch(rect)
def recognition_from_dataset(image_path, detection_method='hog', threshold=0.6): ''' Apply face recognition of the given face :param image_path: image path - SHOULD BE CROP FACE OF ONE PERSON :param tolerance: threshold value (default 0.6) :param embedding_data: npy file that hold the embeddings vector :param embedding_data_type: npy file that hold the embeddings vector (hog or cnn) :return: the name of the person in the image and distance, if not recognition will return "Unknown" and 1000 as distance ''' # Loads an image file (.jpg, .png, etc) into a numpy array in RGB format image = fc.load_image_file(image_path) # find all faces - face_locations is a list of bounding boxes for each face location in a the image [tuple(top, right, bottom, left)..] face_locations = fc.face_locations(image, model=detection_method) # embedding_list is a list of 128-dimensional face encodings. # NOTE: Each embedding in the list is: <class 'numpy.ndarray'> with shape of (128,) That store <class 'numpy.float64'> embedding_list = fc.face_encodings(image, face_locations) # run over all the faces that detected and compare each face to ALL stored face in the dataset for locations_index, unknown_embedding in enumerate(embedding_list): # initialize default min. distance and default name closet_name, min_distance = 'Unknown', 1000 # run over the embedding dataset for i in range(len(ds.names(print_flag=False))): name, known_embedding = ds.get_row_data(index=i) # Compare the unknow_embedding to every embedding vector in the dataset by euclidean distance. # For each comparison the distance tells you how similar the faces are. # NOTE - The parameters for the function are: # face_encodings – List of face encodings to compare, so we need to use [] (list of) # face_to_compare – A face encoding to compare against # distance is store in numpy ndarray with the distance for each face in the same order as the ‘faces’ array, but we compare 1:1 so we intrested in the first index only. distance = fc.face_distance(face_encodings=[known_embedding], face_to_compare=unknown_embedding)[0] # The aim of the code block is to search the min. distance and store the vector and his corresponding name. # First distance should be < tolerance if distance <= threshold: # Check if the distance is the min. distance until this point. if distance <= min_distance: min_distance = distance closet_name = name # draw on the given image (BGR format) image = utils.bounding_box(image, face_locations[locations_index], min_distance, closet_name) # convert to RGB back return cv.cvtColor(image, cv.COLOR_RGB2BGR)
def updateData(i): color = ['r', 'b', 'g', 'c', 'm', 'y'] co = coords[i] for j, ax in enumerate(axs.flat): # print(len(list(ax.patches))) # for k, p in enumerate(ax.patches): # # print(k) # p.remove() # print(len(list(ax.patches))) c = co[j] # print(j, c) for l in range(num_patches): rect = bounding_box( c[0], c[1], patch_size*(glimpse_scale**l), color[i] ) ax.add_patch(rect)
def plot_image(self,index: int,bbox=True): image_id = self.image_ids[index] image = cv2.imread(f'{self.image_dir}/{image_id}.jpg', cv2.IMREAD_COLOR) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32) if bbox: records = utils.annotation_filereader(f'{self.annotation_dir}/{image_id}.txt') target = utils.bounding_box(records).bbox_dict for i in range(len(target['boxes'],)): cv2.rectangle(image, target['boxes'][i][0], target['boxes'][i][1], color=(0, 255, 0), thickness=1) # Draw Rectangle with the coordinates # cv2.putText(img,pred_cls[i], boxes[i][0], cv2.FONT_HERSHEY_SIMPLEX, text_size, (0,255,0),thickness=text_th) # Write the prediction class plt.figure(figsize=(20, 30)) # display the output image plt.imshow(image) plt.xticks([]) plt.yticks([]) plt.show()
def bounding_rect(self): self.bounding_rect_vertices, self.bounding_rect_indices = bounding_box(self.vertices.reshape(-1, 3) ,self.element_dict["triangles"])
""" PyCharm Editor Author cyRi-Le """ import cv2 import matplotlib.pyplot as plt import numpy as np import consts from detection.segmentation import process_threshold, find_contours from utils import keep_contour_with_min_max_area, select_k, keep_and_order_by_criterion, criterion_card_like, \ bounding_box, compute_area path = f"data/train_games/game5/8.jpg" src = cv2.imread(path, cv2.IMREAD_UNCHANGED) bw = process_threshold(src, True, min_val=consts.MIN_THRESHOLD_VAL) contours = find_contours(bw) contours = keep_and_order_by_criterion(contours, criterion_card_like) for cnt in contours: print(cv2.contourArea(cnt)) if 50000 < cv2.contourArea(cnt) < 1e5 else None contours = [ cnt for cnt in contours if consts.MIN_CARD_AREA < compute_area(cnt) < consts.MAX_CARD_AREA ] #keep_contour_with_min_max_area(contours, consts.MIN_CARD_AREA, consts.MAX_CARD_AREA) #contours, success = select_k(contours, consts.CARD_MIN_DISTANCE) cv2.drawContours(src, [bounding_box(cnt) for cnt in contours], -1, consts.BOUNDING_BOX_COLOR, 60) plt.imshow(src) plt.show()
def process(self, path=False, vertices=False, indices=False, info= False): if path: vertices, element_dict, info = read_model(path) indices = element_dict["triangles"] print(f"Reading {path}") else: assert (type(vertices) ==np.ndarray and type(indices) ==np.ndarray), "Define path or both vertices and indices" pre_box = indices.size bounding_rect_vertices, bounding_rect_indices = bounding_box(vertices,indices) vertices = np.append(vertices,bounding_rect_vertices) indices = np.append(indices,bounding_rect_indices) vertex_normals = pyrr.vector3.generate_vertex_normals(vertices.reshape((-1,3)), indices.reshape((-1,3)), normalize_result=True).flatten() vertices_final = np.append(vertices,vertex_normals) # initializing glfw library if not glfw.init(): raise Exception("glfw can not be initialized!") # creating the window window = glfw.create_window(1280, 720, "My OpenGL window", None, None) input_handler = InputHandler(window) # check if window was created if not window: glfw.terminate() raise Exception("glfw window can not be created!") # set window's position glfw.set_window_pos(window, 400, 200) # set the callback function for window resize glfw.set_window_size_callback(window, self.window_resize) # make the context current glfw.make_context_current(window) as_points = vertices.reshape(-1, 3) barycenter = as_points.mean(axis=0) max_x, max_y, max_z = as_points.max(axis=0) min_x, min_y, min_z = as_points.min(axis=0) middle_point = np.array( [min_x + (max_x-min_x)/2, min_y + (max_y-min_y)/2, min_z + (max_z-min_z)/2]) shader = shader_loader.compile_shader("src/shaders/vert.vs", "src/shaders/frag.fs") # Vertex Buffer Object VBO = glGenBuffers(1) VA0 = glGenVertexArrays(1) glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, vertices_final.nbytes, vertices_final, GL_STATIC_DRAW) #positions glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*4, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) #normals glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3*4, ctypes.c_void_p(4*len(vertices))) glEnableVertexAttribArray(1) # Element Buffer Object EBO = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW) glUseProgram(shader) glClearColor(0, 0.1, 0.1, 1) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glEnable(GL_DEPTH_TEST) ## Shader matrices model_loc = glGetUniformLocation(shader, "model") self.proj_loc = glGetUniformLocation(shader, "projection") view_loc = glGetUniformLocation(shader, "view") color_loc = glGetUniformLocation(shader,"color") transform_loc = glGetUniformLocation(shader, "transform") light_loc = glGetUniformLocation(shader, "light") window_height = glfw.get_window_size(window)[1] window_width = glfw.get_window_size(window)[0] projection = pyrr.matrix44.create_perspective_projection_matrix( fovy=45, aspect=window_width/window_height, near=0.1, far=100) #projection = pyrr.matrix44.create_orthogonal_projection_matrix(0,1280,0,720,-1000,1000) scale = pyrr.matrix44.create_from_scale(pyrr.Vector3([1]*3)) # eye pos , target, up view = pyrr.matrix44.create_look_at(pyrr.Vector3( [0, 0, 3]), pyrr.Vector3([0, 0, 0]), pyrr.Vector3([0, 1, 0])) proj_matrix = glGetUniformLocation(shader, "projection") initial_offset = middle_point translation = pyrr.matrix44.create_from_translation( pyrr.Vector3(-initial_offset)) ## Input rotation = pyrr.matrix44.create_from_axis_rotation(np.array([0, 1, 0]), 0) glfw.set_key_callback(window, input_handler.keyboard_handler) glfw.set_scroll_callback(window, input_handler.scroll_handler) glfw.set_mouse_button_callback(window, input_handler.mouse_handler) previous_displacement = np.zeros(2) rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.get_time() ) glUniformMatrix4fv(transform_loc, 1, GL_FALSE, rot_y) glEnable(GL_LIGHTING) glEnable(GL_COLOR_MATERIAL) while not glfw.window_should_close(window): glfw.poll_events() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) if input_handler.right_key_pressed: current_cursor_position = glfw.get_cursor_pos(window) cursor_displacement = np.array(current_cursor_position) - np.array( input_handler.start_cursor_position) - previous_displacement input_handler.rotation_list[0] += input_handler.rotations_per_screen_hor * \ cursor_displacement[0]/window_width input_handler.rotation_list[1] += input_handler.rotations_per_screen_vert * \ cursor_displacement[1]/window_height previous_displacement = cursor_displacement rot_x = pyrr.Matrix44.from_x_rotation(input_handler.rotation_list[1]) rot_y = pyrr.Matrix44.from_y_rotation(input_handler.rotation_list[0]) rotation = pyrr.matrix44.multiply(rot_x, rot_y) view = pyrr.matrix44.create_look_at(pyrr.Vector3(input_handler.eye), pyrr.Vector3( input_handler.target), pyrr.Vector3(input_handler.up)) light = pyrr.matrix44.create_identity() glUniformMatrix4fv(light_loc, 1, GL_FALSE, light) model = pyrr.matrix44.multiply(scale, translation) model = pyrr.matrix44.multiply(model, rotation) glUniformMatrix4fv(model_loc, 1, GL_FALSE, model) glUniformMatrix4fv(view_loc, 1, GL_FALSE, view) glUniformMatrix4fv(proj_matrix, 1, GL_FALSE, projection) default_RGB = np.zeros(shape=(3,),dtype=np.float32) +1 color = pyrr.Vector3(default_RGB) glUniform3fv(color_loc,1,color) if input_handler.mode == "default": glDrawElements(GL_TRIANGLES, pre_box, GL_UNSIGNED_INT, None) elif input_handler.mode == "point_cloud": glDrawElements(GL_POINTS, pre_box, GL_UNSIGNED_INT, None) elif input_handler.mode=="wireframe": glEnable(GL_POLYGON_OFFSET_FILL) glPolygonOffset(1.0, 2) glDrawElements(GL_TRIANGLES, pre_box, GL_UNSIGNED_INT, None) RGB = np.zeros(shape=(3,),dtype=np.float32) color = pyrr.Vector3(RGB) glUniform3fv(color_loc,1,RGB) glDrawElements(GL_LINES, pre_box, GL_UNSIGNED_INT, None) elif input_handler.mode == "bounding_box": glDrawElements(GL_LINES, len(indices), GL_UNSIGNED_INT, None) else: raise Exception("Invalid Mode!") glfw.swap_buffers(window) # terminate glfw, free up allocated resources glfw.terminate()
def main(): conf = get_config()[0] prepare_dirs(conf) conf.is_train = False kwargs = {} if conf.use_gpu: kwargs = {'num_workers': 1, 'pin_memory': True} batch_size = 1 dataloader = get_test_loader(conf.data_dir, batch_size, **kwargs) trainer = Trainer(conf, dataloader) setattr(trainer, 'batch_size', batch_size) trainer.load_checkpoint(True) viz_label = [] for i, (x, y) in enumerate(dataloader): y = y.numpy()[0] if y in viz_label: continue viz_label.append(y) loc = get_locations_from_model(trainer, x) imgs = [x] for j in range(1, trainer.num_stacks): imgs.append(F.avg_pool2d(Variable(x), 2**j).data) if x.size(-1) != trainer.num_channels: for j in range(0, trainer.num_stacks): imgs[j] = imgs[j].transpose(1, 3) imgs[j] = denormalize_image(imgs[j].numpy()) num_glimpses = len(loc) n_row = 2**trainer.num_stacks - 1 n_col = 2**(trainer.num_stacks - 1) * num_glimpses fig = plt.figure(1, (n_col, n_row)) gridspec.GridSpec(n_row, n_col) row_start_idx = [0] for j in range(1, trainer.num_stacks): idx = 2**(trainer.num_stacks - j) + row_start_idx[j - 1] row_start_idx.append(idx) for j in range(num_glimpses): for k in range(trainer.num_stacks): r = row_start_idx[k] c = 2**(trainer.num_stacks - 1) * j sz = 2**(trainer.num_stacks - k - 1) ax = plt.subplot2grid((n_row, n_col), (r, c), colspan=sz, rowspan=sz) ax.imshow(imgs[k][0]) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) x_coord, y_coord = int(loc[j][k][0]), int(loc[j][k][1]) for s in range(trainer.num_patches): size = trainer.patch_size * 2**s rect = bounding_box(x_coord, y_coord, size, 'r') ax.add_patch(rect) fig.tight_layout() plt.savefig(os.path.join(conf.viz_dir, 'viz_%d.jpg' % len(viz_label)), bbox_inches='tight') if len(viz_label) == 3: break
def parse_ns(mesh, coords, conn, node_num_map): """Parse the Nodeset elements Notes ----- <Nodeset exo_id="int"> int int ... int ... </Nodeset> """ els = mesh.getElementsByTagName("Nodeset") if not els: return nsdict = {} for el in els: attributes = dict(el.attributes.items()) exo_id = attributes.get("exo_id") if exo_id is not None: exo_id = Int(exo_id) name = attributes.get("name") if all([x is None for x in (exo_id, name)]): raise AFEPYError("Nodeset requires at least one " "of name or exo_id attribute") if name is None: i = i while True: name = "Nodeset-{0}".format(i) if name not in nsdict: break if i > MAX_NUM_SETS: raise AFEPYError("maximum number of nodesets exceeded") if name in nsdict: raise AFEPYError("{0}: duplicate nodeset".format(exo_id)) if exo_id is None: exo_id = NSET_ID_START while True: if exo_id not in [v["exo_id"] for (k,v) in nsdict.items()]: break exo_id += 1 if exo_id > MAX_NUM_SETS+NSET_ID_START: raise AFEPYError("maximum number of nodesets exceeded") axis = find_region(el) if axis: box = bounding_box(coords) nodes, els = members_at_position(coords, conn, axis[0], box[axis]) else: # nodesets are 1 based in the input file, convert to zero based # through the node_num_map nodes = child_data_to_array(el, np.int, flatten=1) nodes = np.array(nodes) for n in nodes: if n not in node_num_map: raise AFEPYError("Nodeset '{0}' references " "nonexistent nodes".format(name)) nsdict[name] = {"name": name, "exo_id": exo_id, "nodes": nodes} return nsdict