def BoxDraw(box): import matplotlib from matplotlib.patches import Polygon ax = matplotlib.pyplot.subplot(111, aspect='equal') rect = Polygon([[box[0], box[2]], [box[1], box[2]], [box[1], box[3]], [box[0], box[3]]]) rect.set_fill(False) rect.set_linewidth(2) ax.add_patch(rect)
def visualize_inference_sample(sample_id: str, class_names: list, image_dir: str, json_dir: str, render_dir: str) -> None: """Load inference result json file and render overlay on raw image.""" image_path = get_image_local_path(sample_id, image_dir) json_path = get_inference_result_path(sample_id, json_dir) render_path = os.path.join(render_dir, sample_id + '.jpg') image = cv2.imread(image_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) with open(json_path, 'r') as f: data = json.load(f) assert image.shape[0] == data["image"]["height"] assert image.shape[1] == data["image"]["width"] fig, ax = plt.subplots(1, figsize=(20,20)) colors = random_colors(len(data["annotations"])) height, width = image.shape[:2] ax.set_ylim(height + 10, -10) ax.set_xlim(-10, width + 10) ax.axis('off') ax.set_title(f"Sample_id: {sample_id}, shape {image.shape}") masked_image = image.astype(np.uint32).copy() for i, instance in enumerate(data["annotations"]): x, y, w, h = instance["bbox"] p = patches.Rectangle((x, y), w, h, linewidth=2, alpha=0.7, linestyle="dashed", edgecolor=colors[i], facecolor='none') ax.add_patch(p) polygon = instance["segmentation"][0] polygon = np.array(polygon).reshape(-1, 2) p = Polygon(polygon, facecolor="none", edgecolor=colors[i], linewidth=None, fill=True) p.set_fill(True) ax.add_patch(p) # Label ax.text(x, y + 8, class_names[instance["category_id"]], color='w', size=11, backgroundcolor="none") ax.imshow(masked_image.astype(np.uint8)) plt.savefig(render_path) plt.close(fig) return
def drawPolygon(self): """ Draws the polygon """ plt.ion() # plt.figure(self.n) plt.axes() if self.prevPolygon: self.prevPolygon.remove() plt.clf() polygon = Polygon(xy=self.points, closed='True') self.prevPolygon = polygon polygon.set_fill(False) plt.gca().add_patch(polygon) plt.axis('scaled') # plt.show(block = False) self.n += 1
def visualize_sample(sample_id: str, directory_path: str) -> None: """Render the image and annotations for a given sample id. """ image = cv2.imread(get_image_local_path(sample_id, directory_path)) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) with open(get_json_local_path(sample_id, directory_path)) as f: data = json.load(f) _, ax = plt.subplots(1, figsize=(20, 20)) colors = random_colors(len(data["objects"])) height, width = image.shape[:2] ax.set_ylim(height + 10, -10) ax.set_xlim(-10, width + 10) ax.axis('off') ax.set_title(f"Sample_id: {sample_id}, shape {image.shape}") masked_image = image.astype(np.uint32).copy() for i, instance in enumerate(data["objects"]): if instance["geometryType"] == "rectangle": y1, x1, y2, x2 = instance["points"]["exterior"][0][1], \ instance["points"]["exterior"][0][0], \ instance["points"]["exterior"][1][1], \ instance["points"]["exterior"][1][0] p = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2, alpha=0.7, linestyle="dashed", edgecolor=colors[i], facecolor='none') ax.add_patch(p) if instance["geometryType"] == "polygon": y1, x1 = instance["points"]["exterior"][0][1], \ instance["points"]["exterior"][0][0] p = Polygon(instance["points"]["exterior"], facecolor="none", edgecolor=colors[i], linewidth=None, fill=True) p.set_fill(True) ax.add_patch(p) # Label ax.text(x1, y1 + 8, instance["classTitle"], color='w', size=11, backgroundcolor="none") ax.imshow(masked_image.astype(np.uint8)) plt.show() return
def visualize_sample_analytic(sample_id: str, directory_path: str) -> None: """Render the image and annotations for a given sample id in analytic set.""" image = cv2.imread(get_image_local_path(sample_id, directory_path)) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) with open(get_people_local_path(sample_id, directory_path)) as f: annotation_people = json.load(f) with open(get_poly_local_path(sample_id, directory_path)) as f: annotation_poly = json.load(f) _, ax = plt.subplots(1, figsize=(20, 20)) print(f"{len(annotation_people['objects'])} peoples + {len(annotation_poly['objects'])} polygons") colors = random_colors(len(annotation_people["objects"]) + len(annotation_poly["objects"])) height, width = image.shape[:2] ax.set_ylim(height + 10, -10) ax.set_xlim(-10, width + 10) ax.axis('off') ax.set_title(f"Sample_id: {sample_id}, shape {image.shape}") masked_image = image.astype(np.uint32).copy() for i, instance in enumerate(annotation_people["objects"]): if instance["geometryType"] == "rectangle": y1, x1, y2, x2 = instance["points"]["exterior"][0][1], \ instance["points"]["exterior"][0][0], \ instance["points"]["exterior"][1][1], \ instance["points"]["exterior"][1][0] p = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2, alpha=0.7, linestyle="dashed", edgecolor=colors[i], facecolor='none') ax.add_patch(p) # Label ax.text(x1, y1 + 8, instance["classTitle"], color='w', size=11, backgroundcolor="none") for j, instance in enumerate(annotation_poly["objects"]): if instance["geometryType"] == "bitmap": mask = base64_2_mask(instance["bitmap"]["data"]) x1, y1 = instance["bitmap"]["origin"][0], \ instance["bitmap"]["origin"][1] contours, hierarchy = cv2.findContours(mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnt = np.squeeze(contours[0], axis=1) # Depends on whether the mini-mask was adpoted # cnt += [x1, y1] p = Polygon(cnt, facecolor="none", edgecolor=colors[len(annotation_people["objects"])+j], linewidth=None, fill=True) p.set_fill(True) ax.add_patch(p) # Label ax.text(x1, y1 + 8, instance["classTitle"], color='w', size=11, backgroundcolor="none") ax.imshow(masked_image.astype(np.uint8)) plt.show() return