def render_as_image(self, width: int = None, section_index: int = 0): """ Returns an image. """ # Create all the elements then work out the height. title_height: int = 56 subtitle_height: int = 20 height = self.e_pad * 2 + title_height + len( self.subtitles) * subtitle_height canvas = np.zeros((height, width, 3), dtype=np.uint8) canvas[:, :] = (255, 255, 255) # Draw the title. t_region = Region(self.e_pad, width - self.e_pad, self.e_pad, self.e_pad + title_height) canvas = text.write_into_region(canvas, self.title, t_region, color=(0, 0, 0), font_size=42, h_align=text.ALIGN_LEFT) for i, subtitle in enumerate(self.subtitles): y = t_region.bottom + subtitle_height * i s_region = Region(t_region.left, t_region.right, y, y + subtitle_height) canvas = text.write_into_region(canvas, subtitle, s_region, color=(70, 70, 70), font_size=16, h_align=text.ALIGN_LEFT) return canvas
def render_as_image(self, width: int = None, section_index: int = 0): """ Returns an image. """ # Create all the elements then work out the height. title_height: int = 42 section_bg = (230, 230, 230) if section_index % 2 == 0 else (240, 240, 240) size = (width - ((self.n_columns - 1) * self.i_pad) - 2 * self.e_pad) // self.n_columns n_rows = math.ceil(len(self.images) / self.n_columns) height = self.e_pad * 2 + title_height + (n_rows * (size + self.i_pad)) print("Field Rows", n_rows) print("Field height", height) canvas = np.zeros((height, width, 3), dtype=np.uint8) canvas[:, :] = section_bg # Draw the title. t_region = Region(self.e_pad, width - self.e_pad, self.e_pad, self.e_pad + title_height) canvas = text.write_into_region(canvas, self.title, t_region, color=(0, 0, 0), bg_color=None, font_size=18, h_align=text.ALIGN_LEFT) # Draw the data-dict as keys. for i, image in enumerate(self.images): row = math.floor(i / self.n_columns) col = i % self.n_columns x = self.e_pad + col * (size + self.i_pad) y = t_region.bottom + self.i_pad + row * (size + self.i_pad) x_hat = x + size y_hat = y + size f_region = Region(x, x_hat, y, y_hat) crop_image = cv2.resize(image, (size, size)) canvas = visual.safe_implant_with_region(canvas, crop_image, f_region) return canvas
def render_as_image(self, width: int = None, section_index: int = 0): """ Returns an image. """ # Create all the elements then work out the height. title_height: int = 42 field_height: int = 120 label_height: int = 42 field_size: int = 52 label_size: int = 16 section_bg = (230, 230, 230) if section_index % 2 == 0 else (240, 240, 240) n_field_rows = math.ceil(len(self.data) / self.n_columns) height = self.e_pad * 2 + title_height + (n_field_rows * (field_height + self.i_pad)) print("Field Rows", n_field_rows) canvas = np.zeros((height, width, 3), dtype=np.uint8) canvas[:, :] = section_bg # Draw the title. t_region = Region(self.e_pad, width - self.e_pad, self.e_pad, self.e_pad + title_height) canvas = text.write_into_region(canvas, self.title, t_region, color=(0, 0, 0), bg_color=None, font_size=18, h_align=text.ALIGN_LEFT) # Draw the data-dict as keys. column_width = math.floor( (width - ((self.n_columns - 1) * self.i_pad) - 2 * self.e_pad) / self.n_columns) for i, (k, v) in enumerate(self.data.items()): row = math.floor(i / self.n_columns) col = i % self.n_columns x = self.e_pad + col * (column_width + self.i_pad) y = t_region.bottom + self.i_pad + row * (field_height + self.i_pad) x_hat = x + column_width y_hat = y + field_height t_region_1 = Region(x, x_hat, y, y_hat - label_height + self.i_pad) canvas = text.write_into_region(canvas, f"{v}", t_region_1, color=(0, 0, 0), bg_color=(255, 255, 255), font_size=field_size, h_align=text.ALIGN_CENTER) t_region_2 = Region(x, x_hat, y_hat - label_height, y_hat) canvas = text.write_into_region(canvas, f"{k}", t_region_2, color=(150, 150, 150), bg_color=(255, 255, 255), font_size=label_size, h_align=text.ALIGN_CENTER) return canvas
def render(self, tag="image"): """ Join the sections together and produce an output. """ section_width = self.fixed_width - self.page_padding * 2 section_height = self.fixed_height - self.page_padding * 2 - self.footer_height page_sections = [] section_list = [] for i, s in enumerate(self.sections): # If section is page-break, then ship-off previous section if it is populated else ignore. if s == self.PAGE_CAP_SECTION: if len(section_list) > 0: page_sections.append(section_list) section_list = [] continue section_image = s.render(tag, section_width, i) next_height = sum([image.shape[0] for image in section_list ]) + section_image.shape[0] if next_height > section_height: if len(section_list) == 0: page_sections.append([section_image]) else: page_sections.append(section_list) section_list = [section_image] else: section_list.append(section_image) # Add any dangling sections. if len(section_list) > 0: page_sections.append(section_list) image_list = [] for pi, sections in enumerate(page_sections): print(pi) page_num = pi + 1 canvas = np.zeros((self.fixed_height, self.fixed_width, 3), dtype=np.uint8) canvas[:, :] = (255, 255, 255) # Render a page. # Stitch the images together. y = self.page_padding for i, image in enumerate(sections): y_hat = y + image.shape[0] region = Region(self.page_padding, section_width + self.page_padding, y, y_hat) visual.safe_implant_with_region(canvas, image, region) y = y_hat # Create the footer for this page. y = self.fixed_height - self.page_padding - self.footer_height f_region = Region(self.page_padding, section_width + self.page_padding, y, y + self.footer_height) canvas = text.write_into_region(canvas, self.footer_text, f_region, color=(0, 0, 0), h_align=text.ALIGN_LEFT, font_size=14) canvas = text.write_into_region(canvas, f"Page {page_num}", f_region, color=(0, 0, 0), h_align=text.ALIGN_RIGHT, font_size=14) image_name = f"test_render_{pi}.png" image_list.append(image_name) cv2.imwrite(image_name, canvas) with open("output.pdf", "wb") as f: f.write(img2pdf.convert(image_list)) for image_path in image_list: os.remove(image_path)
def main(): # load config cfg.merge_from_file(args.config) cfg.CUDA = torch.cuda.is_available() device = torch.device('cuda' if cfg.CUDA else 'cpu') # create model model = ModelBuilder() # load model model.load_state_dict( torch.load(args.snapshot, map_location=lambda storage, loc: storage.cpu())) model.eval().to(device) cap = cv2.VideoCapture(args.video_name) body_detector = BodyDetector() body_detector.load_model(path_to_model=args.model_path) first_frame_with_detection = False updating_frame = False sot_trackers = {} counter = 0 while cap.isOpened(): ret, frame = cap.read() img_height, img_width, _ = np.shape(frame) regions, _, _ = body_detector.process(frame) if len(regions) > 0 and not first_frame_with_detection: first_frame_with_detection = True if first_frame_with_detection and not updating_frame: for r in regions: counter += 1 sot_trackers[counter] = MSTracker(model=model, tracker_id=counter) sot_trackers[counter].tracker_init(frame, r) updating_frame = True print(f"Init number of MSTracker: {counter}") continue if updating_frame: current_frame_sot_regions = [] for tracker_id in sot_trackers.keys(): sot_region, sot_score = sot_trackers[tracker_id].update(frame) if sot_score > 0.5: current_frame_sot_regions.append(sot_region) current_detected_regions = regions # compare SOT region and detected region to decide whether fire up a new MSTracker for d_region in current_detected_regions: new_tracker = True for sot_region in current_frame_sot_regions: distance = math.sqrt((d_region.x - sot_region.x)**2 + (d_region.y - sot_region.y)**2) if distance < 200: new_tracker = False break if new_tracker: counter += 1 sot_trackers[counter] = MSTracker(model=model, tracker_id=counter) sot_trackers[counter].tracker_init(frame, d_region) print(f"New Tracker: {counter}") # display # displayed = draw_regions(frame, current_frame_sot_regions, color=(0, 255, 0)) # displayed = draw_regions(displayed, current_detected_regions) for r in current_frame_sot_regions: t_id = r.data["sot_id"] frame = write_into_region(frame, str(t_id), r, show_region_outline=True) cv2.putText(frame, f"MSTracker: {len(sot_trackers.keys())}", (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0)) cv2.imshow("body", frame) cv2.waitKey(1)