def vis_frame(frame, im_res, opt, format='coco'): ''' frame: frame image im_res: im_res of predictions format: coco or mpii return rendered image ''' kp_num = 26 if len(im_res['result']) > 0: kp_num = len(im_res['result'][0]['keypoints']) if kp_num == 17: if format == 'coco': l_pair = [ (0, 1), (0, 2), (1, 3), (2, 4), # Head (5, 6), (5, 7), (7, 9), (6, 8), (8, 10), (17, 11), (17, 12), # Body (11, 13), (12, 14), (13, 15), (14, 16) ] p_color = [ (0, 255, 255), (0, 191, 255), (0, 255, 102), (0, 77, 255), (0, 255, 0), # Nose, LEye, REye, LEar, REar (77, 255, 255), (77, 255, 204), (77, 204, 255), (191, 255, 77), (77, 191, 255), (191, 255, 77), # LShoulder, RShoulder, LElbow, RElbow, LWrist, RWrist (204, 77, 255), (77, 255, 204), (191, 77, 255), (77, 255, 191), (127, 77, 255), (77, 255, 127), (0, 255, 255) ] # LHip, RHip, LKnee, Rknee, LAnkle, RAnkle, Neck line_color = [(0, 215, 255), (0, 255, 204), (0, 134, 255), (0, 255, 50), (77, 255, 222), (77, 196, 255), (77, 135, 255), (191, 255, 77), (77, 255, 77), (77, 222, 255), (255, 156, 127), (0, 127, 255), (255, 127, 77), (0, 77, 255), (255, 77, 36)] elif format == 'mpii': l_pair = [(8, 9), (11, 12), (11, 10), (2, 1), (1, 0), (13, 14), (14, 15), (3, 4), (4, 5), (8, 7), (7, 6), (6, 2), (6, 3), (8, 12), (8, 13)] p_color = [ PURPLE, BLUE, BLUE, RED, RED, BLUE, BLUE, RED, RED, PURPLE, PURPLE, PURPLE, RED, RED, BLUE, BLUE ] line_color = [ PURPLE, BLUE, BLUE, RED, RED, BLUE, BLUE, RED, RED, PURPLE, PURPLE, RED, RED, BLUE, BLUE ] else: raise NotImplementedError elif kp_num == 136: l_pair = [ (0, 1), (0, 2), (1, 3), (2, 4), # Head (5, 18), (6, 18), (5, 7), (7, 9), (6, 8), (8, 10), # Body (17, 18), (18, 19), (19, 11), (19, 12), (11, 13), (12, 14), (13, 15), (14, 16), (20, 24), (21, 25), (23, 25), (22, 24), (15, 24), (16, 25), # Foot (26, 27), (27, 28), (28, 29), (29, 30), (30, 31), (31, 32), (32, 33), (33, 34), (34, 35), (35, 36), (36, 37), (37, 38), # Face (38, 39), (39, 40), (40, 41), (41, 42), (43, 44), (44, 45), (45, 46), (46, 47), (48, 49), (49, 50), (50, 51), (51, 52), # Face (53, 54), (54, 55), (55, 56), (57, 58), (58, 59), (59, 60), (60, 61), (62, 63), (63, 64), (64, 65), (65, 66), (66, 67), # Face (68, 69), (69, 70), (70, 71), (71, 72), (72, 73), (74, 75), (75, 76), (76, 77), (77, 78), (78, 79), (79, 80), (80, 81), # Face (81, 82), (82, 83), (83, 84), (84, 85), (85, 86), (86, 87), (87, 88), (88, 89), (89, 90), (90, 91), (91, 92), (92, 93), # Face (94, 95), (95, 96), (96, 97), (97, 98), (94, 99), (99, 100), (100, 101), (101, 102), (94, 103), (103, 104), (104, 105), # LeftHand (105, 106), (94, 107), (107, 108), (108, 109), (109, 110), (94, 111), (111, 112), (112, 113), (113, 114), # LeftHand (115, 116), (116, 117), (117, 118), (118, 119), (115, 120), (120, 121), (121, 122), (122, 123), (115, 124), (124, 125), # RightHand (125, 126), (126, 127), (115, 128), (128, 129), (129, 130), (130, 131), (115, 132), (132, 133), (133, 134), (134, 135) # RightHand ] p_color = [ (0, 255, 255), (0, 191, 255), (0, 255, 102), (0, 77, 255), (0, 255, 0), # Nose, LEye, REye, LEar, REar (77, 255, 255), (77, 255, 204), (77, 204, 255), (191, 255, 77), (77, 191, 255), (191, 255, 77), # LShoulder, RShoulder, LElbow, RElbow, LWrist, RWrist (204, 77, 255), (77, 255, 204), (191, 77, 255), (77, 255, 191), (127, 77, 255), (77, 255, 127), # LHip, RHip, LKnee, Rknee, LAnkle, RAnkle, Neck (77, 255, 255), (0, 255, 255), (77, 204, 255), # head, neck, shoulder (0, 255, 255), (0, 191, 255), (0, 255, 102), (0, 77, 255), (0, 255, 0), (77, 255, 255) ] # foot line_color = [(0, 215, 255), (0, 255, 204), (0, 134, 255), (0, 255, 50), (0, 255, 102), (77, 255, 222), (77, 196, 255), (77, 135, 255), (191, 255, 77), (77, 255, 77), (77, 191, 255), (204, 77, 255), (77, 222, 255), (255, 156, 127), (0, 127, 255), (255, 127, 77), (0, 77, 255), (255, 77, 36), (0, 77, 255), (0, 77, 255), (0, 77, 255), (0, 77, 255), (255, 156, 127), (255, 156, 127)] elif kp_num == 26: l_pair = [ (0, 1), (0, 2), (1, 3), (2, 4), # Head (5, 18), (6, 18), (5, 7), (7, 9), (6, 8), (8, 10), # Body (17, 18), (18, 19), (19, 11), (19, 12), (11, 13), (12, 14), (13, 15), (14, 16), (20, 24), (21, 25), (23, 25), (22, 24), (15, 24), (16, 25), # Foot ] p_color = [ (0, 255, 255), (0, 191, 255), (0, 255, 102), (0, 77, 255), (0, 255, 0), # Nose, LEye, REye, LEar, REar (77, 255, 255), (77, 255, 204), (77, 204, 255), (191, 255, 77), (77, 191, 255), (191, 255, 77), # LShoulder, RShoulder, LElbow, RElbow, LWrist, RWrist (204, 77, 255), (77, 255, 204), (191, 77, 255), (77, 255, 191), (127, 77, 255), (77, 255, 127), # LHip, RHip, LKnee, Rknee, LAnkle, RAnkle, Neck (77, 255, 255), (0, 255, 255), (77, 204, 255), # head, neck, shoulder (0, 255, 255), (0, 191, 255), (0, 255, 102), (0, 77, 255), (0, 255, 0), (77, 255, 255) ] # foot line_color = [(0, 215, 255), (0, 255, 204), (0, 134, 255), (0, 255, 50), (0, 255, 102), (77, 255, 222), (77, 196, 255), (77, 135, 255), (191, 255, 77), (77, 255, 77), (77, 191, 255), (204, 77, 255), (77, 222, 255), (255, 156, 127), (0, 127, 255), (255, 127, 77), (0, 77, 255), (255, 77, 36), (0, 77, 255), (0, 77, 255), (0, 77, 255), (0, 77, 255), (255, 156, 127), (255, 156, 127)] else: raise NotImplementedError # im_name = os.path.basename(im_res['imgname']) img = frame.copy() height, width = img.shape[:2] for human in im_res['result']: part_line = {} kp_preds = human['keypoints'] kp_scores = human['kp_score'] if kp_num == 17: kp_preds = torch.cat( (kp_preds, torch.unsqueeze((kp_preds[5, :] + kp_preds[6, :]) / 2, 0))) kp_scores = torch.cat( (kp_scores, torch.unsqueeze((kp_scores[5, :] + kp_scores[6, :]) / 2, 0))) if opt.tracking: color = get_color_fast(int(abs(human['idx']))) else: color = BLUE # Draw bboxes if opt.showbox: if 'box' in human.keys(): bbox = human['box'] bbox = [ bbox[0], bbox[0] + bbox[2], bbox[1], bbox[1] + bbox[3] ] # xmin,xmax,ymin,ymax else: from trackers.PoseFlow.poseflow_infer import get_box keypoints = [] for n in range(kp_scores.shape[0]): keypoints.append(float(kp_preds[n, 0])) keypoints.append(float(kp_preds[n, 1])) keypoints.append(float(kp_scores[n])) bbox = get_box(keypoints, height, width) # color = get_color_fast(int(abs(human['idx'][0][0]))) cv2.rectangle(img, (int(bbox[0]), int(bbox[2])), (int(bbox[1]), int(bbox[3])), color, 1) if opt.tracking: cv2.putText(img, str(human['idx']), (int(bbox[0]), int( (bbox[2] + 26))), DEFAULT_FONT, 1, BLACK, 2) # Draw keypoints for n in range(kp_scores.shape[0]): if kp_scores[n] <= 0.4: continue cor_x, cor_y = int(kp_preds[n, 0]), int(kp_preds[n, 1]) part_line[n] = (int(cor_x), int(cor_y)) bg = img.copy() if n < len(p_color): if opt.tracking: cv2.circle(bg, (int(cor_x), int(cor_y)), 2, color, -1) else: cv2.circle(bg, (int(cor_x), int(cor_y)), 2, p_color[n], -1) else: cv2.circle(bg, (int(cor_x), int(cor_y)), 1, (255, 255, 255), 2) # Now create a mask of logo and create its inverse mask also transparency = float(max(0, min(1, kp_scores[n]))) img = cv2.addWeighted(bg, transparency, img, 1 - transparency, 0) # Draw limbs for i, (start_p, end_p) in enumerate(l_pair): if start_p in part_line and end_p in part_line: start_xy = part_line[start_p] end_xy = part_line[end_p] bg = img.copy() X = (start_xy[0], end_xy[0]) Y = (start_xy[1], end_xy[1]) mX = np.mean(X) mY = np.mean(Y) length = ((Y[0] - Y[1])**2 + (X[0] - X[1])**2)**0.5 angle = math.degrees(math.atan2(Y[0] - Y[1], X[0] - X[1])) if (start_p, end_p) == (18, 19): print(Y[0], "--", X[0], "==", angle, " ") stickwidth = (kp_scores[start_p] + kp_scores[end_p]) + 1 polygon = cv2.ellipse2Poly((int(mX), int(mY)), (int(length / 2), int(stickwidth)), int(angle), 0, 360, 1) if i < len(line_color): if opt.tracking: cv2.fillConvexPoly(bg, polygon, color) else: cv2.fillConvexPoly(bg, polygon, line_color[i]) else: cv2.line(bg, start_xy, end_xy, (255, 255, 255), 1) fontpath = "/home/hyx/STHeiti Medium.ttc" font = ImageFont.truetype(fontpath, 32) img_pil = Image.fromarray(bg) draw = ImageDraw.Draw(img_pil) if start_p == 17: if abs(angle) <= 15.0 or abs(angle) >= 150.0: draw.text((X[0] - 30, Y[0] - 30), "倒地", font=font, fill=(0, 0, 255)) else: draw.text((X[0] - 30, Y[0] - 30), "正常", font=font, fill=(255, 0, 0)) bg = np.array(img_pil) transparency = float( max( 0, min( 1, 0.5 * (kp_scores[start_p] + kp_scores[end_p]) - 0.1))) img = cv2.addWeighted(bg, transparency, img, 1 - transparency, 0) return img
def vis_frame_fast(frame, im_res, opt, format='coco'): ''' frame: frame image im_res: im_res of predictions format: coco or mpii return rendered image ''' kp_num = 17 if len(im_res['result']) > 0: kp_num = len(im_res['result'][0]['keypoints']) if kp_num == 17: if format == 'coco': l_pair = [ (0, 1), (0, 2), (1, 3), (2, 4), # Head (5, 6), (5, 7), (7, 9), (6, 8), (8, 10), (17, 11), (17, 12), # Body (11, 13), (12, 14), (13, 15), (14, 16) ] p_color = [(0, 255, 255), (0, 191, 255), (0, 255, 102), (0, 77, 255), (0, 255, 0), # Nose, LEye, REye, LEar, REar (77, 255, 255), (77, 255, 204), (77, 204, 255), (191, 255, 77), (77, 191, 255), (191, 255, 77), # LShoulder, RShoulder, LElbow, RElbow, LWrist, RWrist (204, 77, 255), (77, 255, 204), (191, 77, 255), (77, 255, 191), (127, 77, 255), (77, 255, 127), (0, 255, 255)] # LHip, RHip, LKnee, Rknee, LAnkle, RAnkle, Neck line_color = [(0, 215, 255), (0, 255, 204), (0, 134, 255), (0, 255, 50), (77, 255, 222), (77, 196, 255), (77, 135, 255), (191, 255, 77), (77, 255, 77), (77, 222, 255), (255, 156, 127), (0, 127, 255), (255, 127, 77), (0, 77, 255), (255, 77, 36)] elif format == 'mpii': l_pair = [ (8, 9), (11, 12), (11, 10), (2, 1), (1, 0), (13, 14), (14, 15), (3, 4), (4, 5), (8, 7), (7, 6), (6, 2), (6, 3), (8, 12), (8, 13) ] p_color = [PURPLE, BLUE, BLUE, RED, RED, BLUE, BLUE, RED, RED, PURPLE, PURPLE, PURPLE, RED, RED, BLUE, BLUE] else: raise NotImplementedError elif kp_num == 136: l_pair = [ (0, 1), (0, 2), (1, 3), (2, 4), # Head (5, 18), (6, 18), (5, 7), (7, 9), (6, 8), (8, 10),# Body (17, 18), (18, 19), (19, 11), (19, 12), (11, 13), (12, 14), (13, 15), (14, 16), (20, 24), (21, 25), (23, 25), (22, 24), (15, 24), (16, 25),# Foot (26, 27),(27, 28),(28, 29),(29, 30),(30, 31),(31, 32),(32, 33),(33, 34),(34, 35),(35, 36),(36, 37),(37, 38),#Face (38, 39),(39, 40),(40, 41),(41, 42),(43, 44),(44, 45),(45, 46),(46, 47),(48, 49),(49, 50),(50, 51),(51, 52),#Face (53, 54),(54, 55),(55, 56),(57, 58),(58, 59),(59, 60),(60, 61),(62, 63),(63, 64),(64, 65),(65, 66),(66, 67),#Face (68, 69),(69, 70),(70, 71),(71, 72),(72, 73),(74, 75),(75, 76),(76, 77),(77, 78),(78, 79),(79, 80),(80, 81),#Face (81, 82),(82, 83),(83, 84),(84, 85),(85, 86),(86, 87),(87, 88),(88, 89),(89, 90),(90, 91),(91, 92),(92, 93),#Face (94,95),(95,96),(96,97),(97,98),(94,99),(99,100),(100,101),(101,102),(94,103),(103,104),(104,105),#LeftHand (105,106),(94,107),(107,108),(108,109),(109,110),(94,111),(111,112),(112,113),(113,114),#LeftHand (115,116),(116,117),(117,118),(118,119),(115,120),(120,121),(121,122),(122,123),(115,124),(124,125),#RightHand (125,126),(126,127),(115,128),(128,129),(129,130),(130,131),(115,132),(132,133),(133,134),(134,135)#RightHand ] p_color = [(0, 255, 255), (0, 191, 255), (0, 255, 102), (0, 77, 255), (0, 255, 0), # Nose, LEye, REye, LEar, REar (77, 255, 255), (77, 255, 204), (77, 204, 255), (191, 255, 77), (77, 191, 255), (191, 255, 77), # LShoulder, RShoulder, LElbow, RElbow, LWrist, RWrist (204, 77, 255), (77, 255, 204), (191, 77, 255), (77, 255, 191), (127, 77, 255), (77, 255, 127), # LHip, RHip, LKnee, Rknee, LAnkle, RAnkle, Neck (77, 255, 255), (0, 255, 255), (77, 204, 255), # head, neck, shoulder (0, 255, 255), (0, 191, 255), (0, 255, 102), (0, 77, 255), (0, 255, 0), (77, 255, 255)] # foot line_color = [(0, 215, 255), (0, 255, 204), (0, 134, 255), (0, 255, 50), (0, 255, 102), (77, 255, 222), (77, 196, 255), (77, 135, 255), (191, 255, 77), (77, 255, 77), (77, 191, 255), (204, 77, 255), (77, 222, 255), (255, 156, 127), (0, 127, 255), (255, 127, 77), (0, 77, 255), (255, 77, 36), (0, 77, 255), (0, 77, 255), (0, 77, 255), (0, 77, 255), (255, 156, 127), (255, 156, 127)] elif kp_num == 26: l_pair = [ (0, 1), (0, 2), (1, 3), (2, 4), # Head (5, 18), (6, 18), (5, 7), (7, 9), (6, 8), (8, 10),# Body (17, 18), (18, 19), (19, 11), (19, 12), (11, 13), (12, 14), (13, 15), (14, 16), (20, 24), (21, 25), (23, 25), (22, 24), (15, 24), (16, 25),# Foot ] p_color = [(0, 255, 255), (0, 191, 255), (0, 255, 102), (0, 77, 255), (0, 255, 0), # Nose, LEye, REye, LEar, REar (77, 255, 255), (77, 255, 204), (77, 204, 255), (191, 255, 77), (77, 191, 255), (191, 255, 77), # LShoulder, RShoulder, LElbow, RElbow, LWrist, RWrist (204, 77, 255), (77, 255, 204), (191, 77, 255), (77, 255, 191), (127, 77, 255), (77, 255, 127), # LHip, RHip, LKnee, Rknee, LAnkle, RAnkle, Neck (77, 255, 255), (0, 255, 255), (77, 204, 255), # head, neck, shoulder (0, 255, 255), (0, 191, 255), (0, 255, 102), (0, 77, 255), (0, 255, 0), (77, 255, 255)] # foot line_color = [(0, 215, 255), (0, 255, 204), (0, 134, 255), (0, 255, 50), (0, 255, 102), (77, 255, 222), (77, 196, 255), (77, 135, 255), (191, 255, 77), (77, 255, 77), (77, 191, 255), (204, 77, 255), (77, 222, 255), (255, 156, 127), (0, 127, 255), (255, 127, 77), (0, 77, 255), (255, 77, 36), (0, 77, 255), (0, 77, 255), (0, 77, 255), (0, 77, 255), (255, 156, 127), (255, 156, 127)] else: raise NotImplementedError # im_name = os.path.basename(im_res['imgname']) img = frame.copy() height, width = img.shape[:2] for human in im_res['result']: part_line = {} kp_preds = human['keypoints'] kp_scores = human['kp_score'] if kp_num == 17: kp_preds = torch.cat((kp_preds, torch.unsqueeze((kp_preds[5, :] + kp_preds[6, :]) / 2, 0))) kp_scores = torch.cat((kp_scores, torch.unsqueeze((kp_scores[5, :] + kp_scores[6, :]) / 2, 0))) if opt.pose_track or opt.tracking: color = get_color_fast(int(abs(human['idx']))) else: color = BLUE # Draw bboxes if opt.showbox: if 'box' in human.keys(): bbox = human['box'] bbox = [bbox[0], bbox[0]+bbox[2], bbox[1], bbox[1]+bbox[3]]#xmin,xmax,ymin,ymax else: from trackers.PoseFlow.poseflow_infer import get_box keypoints = [] for n in range(kp_scores.shape[0]): keypoints.append(float(kp_preds[n, 0])) keypoints.append(float(kp_preds[n, 1])) keypoints.append(float(kp_scores[n])) bbox = get_box(keypoints, height, width) cv2.rectangle(img, (int(bbox[0]), int(bbox[2])), (int(bbox[1]), int(bbox[3])), color, 2) if opt.tracking: cv2.putText(img, str(human['idx']), (int(bbox[0]), int((bbox[2] + 26))), DEFAULT_FONT, 1, BLACK, 2) # Draw keypoints for n in range(kp_scores.shape[0]): if kp_scores[n] <= 0.4: continue cor_x, cor_y = int(kp_preds[n, 0]), int(kp_preds[n, 1]) part_line[n] = (cor_x, cor_y) if n < len(p_color): if opt.tracking: cv2.circle(img, (cor_x, cor_y), 3, color, -1) else: cv2.circle(img, (cor_x, cor_y), 3, p_color[n], -1) else: cv2.circle(img, (cor_x, cor_y), 1, (255,255,255), 2) # Draw limbs for i, (start_p, end_p) in enumerate(l_pair): if start_p in part_line and end_p in part_line: start_xy = part_line[start_p] end_xy = part_line[end_p] if i < len(line_color): if opt.tracking: cv2.line(img, start_xy, end_xy, color, 2 * int(kp_scores[start_p] + kp_scores[end_p]) + 1) else: cv2.line(img, start_xy, end_xy, line_color[i], 2 * int(kp_scores[start_p] + kp_scores[end_p]) + 1) else: cv2.line(img, start_xy, end_xy, (255,255,255), 1) return img
def vis_frame_bbox(frame, im_res, opt, name_image, format='coco'): ''' frame: frame image im_res: im_res of predictions format: coco or mpii return rendered image ''' print(name_image) kp_num = 17 if len(im_res['result']) > 0: kp_num = len(im_res['result'][0]['keypoints']) img = frame.copy() height, width = img.shape[:2] incr = 0 for human in im_res[ 'result']: #Incrémentation sur le nombre de résultat donc le nombre de bbox part_line = {} kp_preds = human['keypoints'] kp_scores = human['kp_score'] if kp_num == 17: kp_preds = torch.cat( (kp_preds, torch.unsqueeze((kp_preds[5, :] + kp_preds[6, :]) / 2, 0))) kp_scores = torch.cat( (kp_scores, torch.unsqueeze((kp_scores[5, :] + kp_scores[6, :]) / 2, 0))) if opt.tracking: color = get_color_fast(int(abs(human['idx']))) else: color = BLUE treshold_value = opt.thr_bbox if (kp_scores[0] >= treshold_value and kp_scores[-1] >= treshold_value): # Draw bboxes if 'box' in human.keys(): bbox = human['box'] bbox = [ bbox[0], bbox[0] + bbox[2], bbox[1], bbox[1] + bbox[3] ] #xmin,xmax,ymin,ymax else: from trackers.PoseFlow.poseflow_infer import get_box keypoints = [] for n in range(kp_scores.shape[0]): keypoints.append(float(kp_preds[n, 0])) keypoints.append(float(kp_preds[n, 1])) keypoints.append(float(kp_scores[n])) bbox = get_box(keypoints, height, width) # color = get_color_fast(int(abs(human['idx'][0][0]))) position = [ int(bbox[0]), int(bbox[2]), int(bbox[1]), int(bbox[3]) ] #xmin,ymin,xmax,ymax for number in range( len(position)): # check if there is some negative value if position[number] < 0: position[number] = 0 len_y, len_x = position[3] - position[1], position[2] - position[0] # new_image = np.zeros((len_y, len_x, 3), dtype=np.uint8) # new_image[:, :, :] = img[ position[1]:position[3] , position[0]:position[2] , 0:3] new_image = [] new_image = img[position[1]:position[3], position[0]:position[2], 0:3] os.makedirs(opt.outputpath + 'crop/', exist_ok=True) new_name = name_image[:-4] + '_' + str(incr) + '.png' ## SANS CONDITION SUR LA TAILLE DE L'IMAGE # cv2.imwrite(opt.outputpath + 'crop/' + new_name,new_image) # incr+=1 ## CONDITION SUR LA TAILLE DE L'IMAGE # condition sur la hauteur ou la largeur : valeur 30% if int((new_image.shape[0] * 100) / img.shape[0]) > opt.size_bbox or int( (new_image.shape[1] * 100) / img.shape[1]) > opt.size_bbox: cv2.imwrite(opt.outputpath + 'crop/' + new_name, new_image) incr += 1