def pose_detection(image): mode = '' input_source = image if not os.path.exists(args.outputpath): os.makedirs(args.outputpath) det_loader = DetectionLoader(input_source, get_detector(args), cfg, args, batchSize=args.detbatch, mode=mode, queueSize=args.qsize) det_worker = det_loader.start()
def run(self): if os.path.isfile(self.video): mode, input_source = 'video', self.video else: raise IOError( 'Error: --video must refer to a video file, not directory.') if not os.path.exists(self.outputpath): os.makedirs(self.outputpath) det_loader = DetectionLoader(input_source, get_detector(self), self.cfg, self, batchSize=self.detbatch, mode=mode, queueSize=self.qsize) det_worker = det_loader.start() # Load pose model pose_model = builder.build_sppe(self.cfg.MODEL, preset_cfg=self.cfg.DATA_PRESET) print(f'Loading pose model from {self.checkpoint}...') pose_model.load_state_dict( torch.load(self.checkpoint, map_location=self.device)) if self.pose_track: tracker = Tracker(tcfg, self) pose_model.to(self.device) pose_model.eval() if self.save_video: from alphapose.utils.writer import DEFAULT_VIDEO_SAVE_OPT as video_save_opt video_save_opt['savepath'] = self.outputpath + os.path.basename( self.video) video_save_opt.update(det_loader.videoinfo) writer = DataWriter(self.cfg, self, save_video=True, video_save_opt=video_save_opt, queueSize=self.qsize).start() else: writer = DataWriter(self.cfg, self, save_video=False, queueSize=self.qsize).start() data_len = det_loader.length im_names_desc = tqdm(range(data_len), dynamic_ncols=True) batchSize = self.posebatch try: for i in im_names_desc: start_time = getTime() with torch.no_grad(): (inps, orig_img, im_name, boxes, scores, ids, cropped_boxes) = det_loader.read() if orig_img is None: break if boxes is None or boxes.nelement() == 0: writer.save(None, None, None, None, None, orig_img, os.path.basename(im_name)) continue # Pose Estimation inps = inps.to(self.device) datalen = inps.size(0) leftover = 0 if (datalen) % batchSize: leftover = 1 num_batches = datalen // batchSize + leftover hm = [] for j in range(num_batches): inps_j = inps[j * batchSize:min((j + 1) * batchSize, datalen)] hm_j = pose_model(inps_j) hm.append(hm_j) hm = torch.cat(hm) #hm = hm.cpu() if self.pose_track: boxes, scores, ids, hm, cropped_boxes = track( tracker, self, orig_img, inps, boxes, hm, cropped_boxes, im_name, scores) writer.save(boxes, scores, ids, hm, cropped_boxes, orig_img, os.path.basename(im_name)) while (writer.running()): time.sleep(1) print('===========================> Rendering remaining ' + str(writer.count()) + ' images in the queue...') writer.stop() det_loader.stop() except KeyboardInterrupt: det_loader.terminate() while (writer.running()): time.sleep(1) print('===========================> Rendering remaining ' + str(writer.count()) + ' images in the queue...') writer.stop() self.all_results = writer.results() self._save()