def main(filename, hrnet_m, hrnet_c, hrnet_j, hrnet_weights, image_resolution, single_person, max_batch_size, csv_output_filename, csv_delimiter, device): if device is not None: device = torch.device(device) else: if torch.cuda.is_available(): torch.backends.cudnn.deterministic = True device = torch.device('cuda') else: device = torch.device('cpu') # print(device) image_resolution = ast.literal_eval(image_resolution) rotation_code = check_video_rotation(filename) video = cv2.VideoCapture(filename) assert video.isOpened() nof_frames = video.get(cv2.CAP_PROP_FRAME_COUNT) assert csv_output_filename.endswith('.csv') with open(csv_output_filename, 'wt', newline='') as fd: csv_output = csv.writer(fd, delimiter=csv_delimiter) model = SimpleHRNet( hrnet_c, hrnet_j, hrnet_weights, model_name=hrnet_m, resolution=image_resolution, multiperson=not single_person, max_batch_size=max_batch_size, device=device ) index = 0 while True: t = time.time() ret, frame = video.read() if not ret: break if rotation_code is not None: frame = cv2.rotate(frame, rotation_code) pts = model.predict(frame) # csv format is: # frame_index,detection_index,<point 0>,<point 1>,...,<point hrnet_j> # where each <point N> corresponds to three elements: # x_coordinate,y_coordinate,confidence for j, pt in enumerate(pts): row = [index, j] + pt.flatten().tolist() csv_output.writerow(row) fps = 1. / (time.time() - t) print('\rframe: % 4d / %d - framerate: %f fps ' % (index, nof_frames - 1, fps), end='') index += 1
def main(filename, hrnet_m, hrnet_c, hrnet_j, hrnet_weights, image_resolution, max_batch_size, device): if device is not None: device = torch.device(device) else: if torch.cuda.is_available(): torch.backends.cudnn.deterministic = True device = torch.device('cuda') else: device = torch.device('cpu') # print(device) image_resolution = ast.literal_eval(image_resolution) rotation_code = check_video_rotation(filename) video = cv2.VideoCapture(filename) assert video.isOpened() nof_frames = video.get(cv2.CAP_PROP_FRAME_COUNT) model = SimpleHRNet(hrnet_c, hrnet_j, hrnet_weights, model_name=hrnet_m, resolution=image_resolution, max_batch_size=max_batch_size, device=device) fight_detector = FightDetectionModule() index = 0 pe_row = None od_row = None while True: t = time.time() ret, frame = video.read() if not ret: break if rotation_code is not None: frame = cv2.rotate(frame, rotation_code) pts, detections, intermediate_features = model.predict(frame) ######Module Calling Place ######### fight_result = fight_detector.analysis_from_json( pe_row, od_row, intermediate_features) ##################################### fps = 1. / (time.time() - t) print('\rframe: % 4d / %d - framerate: %f fps ' % (index, nof_frames - 1, fps), end='') index += 1
def crop(camera_id, filename, hrnet_m, hrnet_c, hrnet_j, hrnet_weights, hrnet_joints_set, image_resolution, single_person, use_tiny_yolo, disable_tracking, max_batch_size, disable_vidgear, save_video, video_format, video_framerate, device): video_writer = None if filename is not None: rotation_code = check_video_rotation(filename) video = cv2.VideoCapture(filename) assert video.isOpened() nof_frames = video.get(cv2.CAP_PROP_FRAME_COUNT) else: rotation_code = None if disable_vidgear: video = cv2.VideoCapture(camera_id) assert video.isOpened() else: video = CamGear(camera_id).start() while True: t = time.time() if filename is not None or disable_vidgear: ret, frame = video.read() if ret: #Code for bounding box and cropping of the video bbox, label, conf = cv.detect_common_objects(frame) frame_bounding = draw_bbox(frame, bbox, label, conf) #bb.add(image, left, top, right, bottom, label, color) if save_video: if video_writer is None: fourcc = cv2.VideoWriter_fourcc( *video_format) # video format video_writer = cv2.VideoWriter( 'output_bounding.avi', fourcc, video_framerate, (frame.shape[1], frame.shape[0])) video_writer.write(frame_bounding) if not ret: filename = 'output_bounding.avi' break if rotation_code is not None: frame = cv2.rotate(frame, rotation_code) else: frame = video.read() if frame is None: break
def main(camera_id, filename, hrnet_c, hrnet_j, hrnet_weights, hrnet_joints_set, image_resolution, disable_tracking, max_nof_people, max_batch_size, disable_vidgear, save_video, video_format, video_framerate, device): if device is not None: device = torch.device(device) else: if torch.cuda.is_available(): torch.backends.cudnn.deterministic = True device = torch.device('cuda') else: device = torch.device('cpu') # print(device) has_display = 'DISPLAY' in os.environ.keys() or sys.platform == 'win32' video_writer = None if filename is not None: rotation_code = check_video_rotation(filename) video = cv2.VideoCapture(filename) assert video.isOpened() else: rotation_code = None if disable_vidgear: video = cv2.VideoCapture(camera_id) assert video.isOpened() else: video = CamGear(camera_id).start() model = SimpleHigherHRNet(hrnet_c, hrnet_j, hrnet_weights, resolution=image_resolution, return_bounding_boxes=not disable_tracking, max_nof_people=max_nof_people, max_batch_size=max_batch_size, device=device) if not disable_tracking: prev_boxes = None prev_pts = None prev_person_ids = None next_person_id = 0 while True: t = time.time() if filename is not None or disable_vidgear: ret, frame = video.read() if not ret: break if rotation_code is not None: frame = cv2.rotate(frame, rotation_code) else: frame = video.read() if frame is None: break pts = model.predict(frame) if not disable_tracking: boxes, pts = pts if not disable_tracking: if len(pts) > 0: if prev_pts is None and prev_person_ids is None: person_ids = np.arange(next_person_id, len(pts) + next_person_id, dtype=np.int32) next_person_id = len(pts) + 1 else: boxes, pts, person_ids = find_person_id_associations( boxes=boxes, pts=pts, prev_boxes=prev_boxes, prev_pts=prev_pts, prev_person_ids=prev_person_ids, next_person_id=next_person_id, pose_alpha=0.2, similarity_threshold=0.4, smoothing_alpha=0.1, ) next_person_id = max(next_person_id, np.max(person_ids) + 1) else: person_ids = np.array((), dtype=np.int32) prev_boxes = boxes.copy() prev_pts = pts.copy() prev_person_ids = person_ids else: person_ids = np.arange(len(pts), dtype=np.int32) for i, (pt, pid) in enumerate(zip(pts, person_ids)): frame = draw_points_and_skeleton( frame, pt, joints_dict()[hrnet_joints_set]['skeleton'], person_index=pid, points_color_palette='gist_rainbow', skeleton_color_palette='jet', points_palette_samples=10) fps = 1. / (time.time() - t) print('\rframerate: %f fps / detected people: %d' % (fps, len(pts)), end='') if has_display: cv2.imshow('frame.png', frame) k = cv2.waitKey(1) if k == 27: # Esc button if disable_vidgear: video.release() else: video.stop() break else: cv2.imwrite('frame.png', frame) if save_video: if video_writer is None: fourcc = cv2.VideoWriter_fourcc(*video_format) # video format video_writer = cv2.VideoWriter( 'output.avi', fourcc, video_framerate, (frame.shape[1], frame.shape[0])) video_writer.write(frame) if save_video: video_writer.release()
def live(camera_id, filename, hrnet_m, hrnet_c, hrnet_j, hrnet_weights, hrnet_joints_set, image_resolution, disable_tracking, max_batch_size, disable_vidgear, save_heatmap_video, video_format, video_framerate, device): if device is not None: device = torch.device(device) else: if torch.cuda.is_available(): torch.backends.cudnn.deterministic = True device = torch.device('cuda') else: device = torch.device('cpu') # print(device) if save_heatmap_video : print('save video.') image_resolution = ast.literal_eval(image_resolution) has_display = 'DISPLAY' in os.environ.keys() or sys.platform == 'win32' video_writer = None if filename is not None: rotation_code = check_video_rotation(filename) video = cv2.VideoCapture(filename) assert video.isOpened() else: rotation_code = None if disable_vidgear: video = cv2.VideoCapture(camera_id) assert video.isOpened() else: video = CamGear(camera_id).start() model = OnlySimpleHRNet( hrnet_c, hrnet_j, hrnet_weights, model_name=hrnet_m, resolution=image_resolution, max_batch_size=max_batch_size, return_bounding_boxes=True, device=device ) nof_frames = video.get(cv2.CAP_PROP_FRAME_COUNT) if not disable_tracking: prev_boxes = None prev_pts = None prev_person_ids = None next_person_id = 0 ############################# MAKE JSON FORMAT ##################################### json_data = {} json_data['videos'] = [] json_data['annotations'] = [] json_data['categories'] = [] frame_idx = 0 while True: t = time.time() if filename is not None or disable_vidgear: ret, frame = video.read() if not ret: break if rotation_code is not None: frame = cv2.rotate(frame, rotation_code) else: frame = video.read() if frame is None: break _ = None if frame_idx==0: video_dict = {} video_dict['filename'] = str(filename) video_dict['height'] = frame.shape[0] video_dict['width'] = frame.shape[1] video_dict['date_captured'] = str(strftime("%Y-%m-%d %H:%M:%S", gmtime())) json_data['videos'].append(video_dict) pts = model.predict(frame) if not disable_tracking: boxes, pts = pts # import pdb;pdb.set_trace() object_idx = 0 if len(boxes)==0: annotation_dict = {} annotation_dict['filename'] = str(filename) annotation_dict['num_keypoints'] = 0 # number of keypoints annotation_dict['area'] = 0.0 # w*h area annotation_dict['iscrowd'] = 0 # 0 : one person , 1 : more than one person annotation_dict['keypoints'] = [0.0 for i in range(0,54)] # if 18 keypoints : number of points is 54 annotation_dict['frame_id'] = int(frame_idx) annotation_dict['bbox'] = [0.0, 0.0, 0.0, 0.0] annotation_dict['category_id'] = 1 annotation_dict['object_id'] = 0 json_data['annotations'].append(annotation_dict) else: for idx, (box,pt) in enumerate(zip(boxes,pts)): # frame = Image.open('/home/mmlab/CCTV_Server/000000469067.jpg') # draw = ImageDraw.Draw(frame) # draw.rectangle(((0,184.8),(557.32,288.86+184.8)), outline='red') # # draw.rectangle(((box[0], box[1]), (box[2], box[3])), outline='red') # frame.save(os.path.join(output_root,'test.jpg')) # import pdb;pdb.set_trace() bbox_x = round(float(box[0]),2) # visipedia annotation tool x1,y1,x2,y2 bbox format bbox_y = round(float(box[1]),2) bbox_w = round(float(box[2]-box[0]),2) bbox_h = round(float(box[3]-box[1]),2) keypoints_x = [x for y,x,conf in pt] keypoints_y = [y for y,x,conf in pt] confidences = [conf for y,x,conf in pt] keypoints = list() num_keypoints = 0 iscrowd = 0 if len(pts)>1: iscrowd =1 for pt_x,pt_y,confidence in zip(keypoints_x,keypoints_y,confidences): visibility = 0 if int(pt_x)==0 and int(pt_y)==0: pt_x = 0 pt_y = 0 confidence =0 else : pt_x = int(pt_x) pt_y = int(pt_y) num_keypoints+=1 confidence=float(confidence) keypoints.append(pt_x) keypoints.append(pt_y) keypoints.append(confidence) annotation_dict = {} annotation_dict['filename'] = str(filename) annotation_dict['num_keypoints'] = num_keypoints # number of keypoints annotation_dict['area'] = bbox_w*bbox_h # w*h area annotation_dict['iscrowd'] = iscrowd # 0 : one person , 1 : more than one person annotation_dict['keypoints'] = keypoints # if 18 keypoints : number of points is 54 annotation_dict['frame_id'] = int(frame_idx) annotation_dict['bbox'] = [bbox_x, bbox_y, bbox_w, bbox_h] annotation_dict['category_id'] = 1 annotation_dict['object_id'] = int(object_idx) object_idx+=1 json_data['annotations'].append(annotation_dict) # import pdb;pdb.set_trace() if save_heatmap_video: frame = cv2.imread('/home/mmlab/CCTV_Server/golf/heatmap_club_head/%05d.png'%frame_idx) frame_idx+=1 fps = 1. / (time.time() - t) print('\rframe: % 4d / %d - framerate: %f fps ' % (frame_idx, nof_frames - 1, fps), end='') video_full_name = filename.split('/')[-1] output_root = '/home/mmlab/CCTV_Server/golf/output_heatmap' if frame_idx==1: makedir(output_root) output_path = os.path.join(output_root,video_full_name) if save_heatmap_video: if video_writer is None: fourcc = cv2.VideoWriter_fourcc(*video_format) # video format video_writer = cv2.VideoWriter(output_path, fourcc, video_framerate, (frame.shape[1], frame.shape[0])) video_writer.write(frame) if save_heatmap_video: video_writer.release() output_root = '/home/mmlab/CCTV_Server/golf/output_json' output_path = os.path.join(output_root,video_full_name) json_data['categories'].append({'supercategory': 'person', 'id': '1', 'name': 'person', 'keypoints': ['nose', 'left_eye', 'right_eye', 'left_ear', 'right_ear', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist', 'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle', 'club_head'], 'skeleton': [[16, 14], [14, 12], [17, 15], [15, 13], [12, 13], [6, 12], [7, 13], [6, 7], [6, 8], [7, 9], [8, 10], [9, 11], [2, 3], [1, 2], [1, 3], [2, 4], [3, 5], [4, 6], [5, 7], [10, 18]]}) file_basename = video_basename(video_full_name) json_output_filename = file_basename+'.json' assert json_output_filename.endswith('.json') with open(os.path.join(output_root, json_output_filename), "w") as json_file: json.dump(json_data, json_file)
def main(format, filename, hrnet_m, hrnet_c, hrnet_j, hrnet_weights, image_resolution, single_person, use_tiny_yolo, max_batch_size, csv_output_filename, csv_delimiter, json_output_filename, device): if device is not None: device = torch.device(device) else: if torch.cuda.is_available(): torch.backends.cudnn.deterministic = True device = torch.device('cuda') else: device = torch.device('cpu') # print(device) image_resolution = ast.literal_eval(image_resolution) rotation_code = check_video_rotation(filename) video = cv2.VideoCapture(filename) assert video.isOpened() nof_frames = video.get(cv2.CAP_PROP_FRAME_COUNT) assert format in ('csv', 'json') if format == 'csv': assert csv_output_filename.endswith('.csv') fd = open(csv_output_filename, 'wt', newline='') csv_output = csv.writer(fd, delimiter=csv_delimiter) elif format == 'json': assert json_output_filename.endswith('.json') fd = open(json_output_filename, 'wt') json_data = {} if use_tiny_yolo: yolo_model_def = "./models/detectors/yolo/config/yolov3-tiny.cfg" yolo_class_path = "./models/detectors/yolo/data/coco.names" yolo_weights_path = "./models/detectors/yolo/weights/yolov3-tiny.weights" else: yolo_model_def = "./models/detectors/yolo/config/yolov3.cfg" yolo_class_path = "./models/detectors/yolo/data/coco.names" yolo_weights_path = "./models/detectors/yolo/weights/yolov3.weights" model = SimpleHRNet(hrnet_c, hrnet_j, hrnet_weights, model_name=hrnet_m, resolution=image_resolution, multiperson=not single_person, max_batch_size=max_batch_size, yolo_model_def=yolo_model_def, yolo_class_path=yolo_class_path, yolo_weights_path=yolo_weights_path, device=device) index = 0 while True: t = time.time() ret, frame = video.read() if not ret: break if rotation_code is not None: frame = cv2.rotate(frame, rotation_code) pts = model.predict(frame) # csv format is: # frame_index,detection_index,<point 0>,<point 1>,...,<point hrnet_j> # where each <point N> corresponds to three elements: # y_coordinate,x_coordinate,confidence # json format is: # {frame_index: [[<point 0>,<point 1>,...,<point hrnet_j>], ...], ...} # where each <point N> corresponds to three elements: # [y_coordinate,x_coordinate,confidence] if format == 'csv': for j, pt in enumerate(pts): row = [index, j] + pt.flatten().tolist() csv_output.writerow(row) elif format == 'json': json_data[index] = list() for j, pt in enumerate(pts): json_data[index].append(pt.tolist()) fps = 1. / (time.time() - t) print('\rframe: % 4d / %d - framerate: %f fps ' % (index, nof_frames - 1, fps), end='') index += 1 if format == 'json': json.dump(json_data, fd) fd.close()
def live(camera_id, filename, hrnet_m, hrnet_c, hrnet_j, hrnet_weights, hrnet_joints_set, image_resolution, disable_tracking, max_batch_size, disable_vidgear, save_video, video_format, video_framerate, device): if device is not None: device = torch.device(device) else: if torch.cuda.is_available(): torch.backends.cudnn.deterministic = True device = torch.device('cuda') else: device = torch.device('cpu') # print(device) if save_video: print('save video.') image_resolution = ast.literal_eval(image_resolution) has_display = 'DISPLAY' in os.environ.keys() or sys.platform == 'win32' video_writer = None if filename is not None: rotation_code = check_video_rotation(filename) video = cv2.VideoCapture(filename) assert video.isOpened() else: rotation_code = None if disable_vidgear: video = cv2.VideoCapture(camera_id) assert video.isOpened() else: video = CamGear(camera_id).start() model = OnlySimpleHRNet(hrnet_c, hrnet_j, hrnet_weights, model_name=hrnet_m, resolution=image_resolution, max_batch_size=max_batch_size, return_bounding_boxes=True, device=device) nof_frames = video.get(cv2.CAP_PROP_FRAME_COUNT) if not disable_tracking: prev_boxes = None prev_pts = None prev_person_ids = None next_person_id = 0 index = 0 while True: t = time.time() if filename is not None or disable_vidgear: ret, frame = video.read() if not ret: break if rotation_code is not None: frame = cv2.rotate(frame, rotation_code) else: frame = video.read() if frame is None: break _ = None pts = model.predict(frame) if not disable_tracking: boxes, pts = pts # import pdb;pdb.set_trace() if not disable_tracking: if len(pts) > 0: if prev_pts is None and prev_person_ids is None: person_ids = np.arange(next_person_id, len(pts) + next_person_id, dtype=np.int32) next_person_id = len(pts) + 1 else: boxes, pts, person_ids = find_person_id_associations( boxes=boxes, pts=pts, prev_boxes=prev_boxes, prev_pts=prev_pts, prev_person_ids=prev_person_ids, next_person_id=next_person_id, pose_alpha=0.2, similarity_threshold=0.4, smoothing_alpha=0.1, ) next_person_id = max(next_person_id, np.max(person_ids) + 1) else: person_ids = np.array((), dtype=np.int32) prev_boxes = boxes.copy() prev_pts = pts.copy() prev_person_ids = person_ids else: person_ids = np.arange(len(pts), dtype=np.int32) for i, (pt, pid) in enumerate(zip(pts, person_ids)): frame = draw_points_and_skeleton( frame, pt, joints_dict()[hrnet_joints_set]['skeleton'], person_index=pid, points_color_palette='gist_rainbow', skeleton_color_palette='jet', points_palette_samples=10) color_map = ['red', 'green', 'blue', 'yellow', 'purple', 'white'] fps = 1. / (time.time() - t) print('\rframe: % 4d / %d - framerate: %f fps ' % (index, nof_frames - 1, fps), end='') index += 1 # if has_display: # cv2.imshow('frame.png', frame) # k = cv2.waitKey(1) # if k == 27: # Esc button # if disable_vidgear: # video.release() # else: # video.stop() # break # else: # cv2.imwrite('frame.png', frame) video_full_name = filename.split('/')[-1] output_root = '/home/mmlab/CCTV_Server/golf/output' output_path = os.path.join(output_root, video_full_name) if save_video: if video_writer is None: fourcc = cv2.VideoWriter_fourcc(*video_format) # video format video_writer = cv2.VideoWriter( output_path, fourcc, video_framerate, (frame.shape[1], frame.shape[0])) video_writer.write(frame) if save_video: video_writer.release()
def draw_trajectory(filename, json_filename, video_format, video_framerate, save_frame, frame_dir): output_root = '/home/mmlab/CCTV_Server/golf/output_trajectory' makedir(output_root) with open(json_filename) as json_file: json_data = json.load(json_file) json_images = json_data['videos'] json_annotations = json_data['annotations'] # print('origin image len : ', len(json_images)) # print('origin annotations len : ', len(json_annotations)) sorted_annotations = sorted(json_annotations, key=sortFunction) # print(sorted_annotations) has_display = 'DISPLAY' in os.environ.keys() or sys.platform == 'win32' video_writer = None if filename is not None: rotation_code = check_video_rotation(filename) video = cv2.VideoCapture(filename) assert video.isOpened() nof_frames = video.get(cv2.CAP_PROP_FRAME_COUNT) prev_boxes = None prev_pts = None prev_person_ids = None next_person_id = 0 frame_idx = 0 points_list = [] makedir(frame_dir) while frame_idx < nof_frames - 1: t = time.time() annotation = sorted_annotations[frame_idx] # print(frame_idx, ' ' , annotation['frame_id']) # try: # assert frame_idx is int(annotation['frame_id']) # except: # print(frame_idx, ' ' ,int(annotation['frame_id'])) # import pdb;pdb.set_trace() if filename is not None: ret, frame = video.read() if not ret: break if rotation_code is not None: frame = cv2.rotate(frame, rotation_code) else: frame = video.read() if frame is None: break _ = None circle_size = max(1, min(frame.shape[:2]) // 160) pts = annotation['keypoints'] pt_y, pt_x, conf = pts[51:54] # import pdb;pdb.set_trace() if conf > 0.75: points_list.append([pt_x, pt_y]) prev_pt = None pprev_pt = None prev_i = None pprev_i = None save_p_i = 0 cos = 0 for p_i, pt in enumerate(points_list): # frame = cv2.circle(frame, (int(pt[1]), int(pt[0])), circle_size, (255,0,0), -1) save_p_i = p_i if p_i >= 2: # if euclidean_distance(prev_pt,pt)< int(frame.shape[0]//3): prev_mv = [p1 - p2 for p1, p2 in zip(prev_pt, pt)] pprev_mv = [p1 - p2 for p1, p2 in zip(pprev_pt, prev_pt)] if prev_mv[0] != 0 and prev_mv[1] != 0 and pprev_mv[ 0] != 0 and pprev_mv[1] != 0: cos = cos_sim(pprev_mv, prev_mv) if cos >= -0.5 and cos <= 1: frame = cv2.line(frame, (int(prev_pt[1]), int(prev_pt[0])), (int(pt[1]), int(pt[0])), (0, 0, 255), 3) # else: # frame = cv2.line(frame, (int(prev_pt[1]), int(prev_pt[0])), # (int(pt[1]), int(pt[0])), # (0, 0, 255), 3) else: pass if prev_i != p_i and prev_pt[0] != pt[0] and prev_pt[1] != pt[ 1]: if cos >= -0.5 and cos <= 1: pprev_pt = prev_pt prev_pt = pt pprev_i = prev_i prev_i = p_i elif p_i == 0: pprev_pt = pt pprev_i = p_i elif p_i == 1: prev_pt = pt prev_i = p_i if save_frame: if frame_idx > 1546 and frame_idx < 1600: frame_path = os.path.join(frame_dir, '%06d.jpg' % frame_idx) cv2.imwrite(frame_path, frame) fps = 1. / (time.time() - t) print('\rframe: % 4d / %d - framerate: %f fps ' % (frame_idx, nof_frames - 1, fps), end='') frame_idx += 1 video_full_name = filename.split('/')[-1] output_path = os.path.join(output_root, video_full_name) if video_writer is None: fourcc = cv2.VideoWriter_fourcc(*video_format) # video format video_writer = cv2.VideoWriter(output_path, fourcc, video_framerate, (frame.shape[1], frame.shape[0])) video_writer.write(frame) video_writer.release()
def main( camera_id=0, filename=None, hrnet_m='HRNet', hrnet_c=48, hrnet_j=17, hrnet_weights="/media/koshiba/Data/simple-HRNet/weights/pose_hrnet_w48_384x288.pth", hrnet_joints_set="coco", image_resolution='(384, 288)', single_person="store_true", use_tiny_yolo="store_true", disable_tracking="store_true", max_batch_size=16, disable_vidgear="store_true", save_video="store_true", video_format='MJPG', video_framerate=30, device=None): if device is not None: device = torch.device(device) else: if torch.cuda.is_available(): torch.backends.cudnn.deterministic = True device = torch.device('cuda') else: device = torch.device('cpu') inputPath = '/media/koshiba/Data/simple-HRNet/inputData' hrnetPath = '/media/koshiba/Data/simple-HRNet' # print(device) image_resolution = ast.literal_eval(image_resolution) has_display = 'DISPLAY' in os.environ.keys() or sys.platform == 'win32' video_writer = None filePath = glob.glob(inputPath + '/*') for filename in filePath: videoName = filename.split('/')[-1][:-4] print(videoName) rotation_code = check_video_rotation(filename) video = cv2.VideoCapture(filename) assert video.isOpened() width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH)) #幅 height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)) #高 frame_rate = int(video.get(cv2.CAP_PROP_FPS)) #FPS print(width, height, frame_rate) fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') #mp4出力 video_writer = cv2.VideoWriter( '/media/koshiba/Data/simple-HRNet/outputData/' + videoName + '_output.mp4', fourcc, frame_rate, (width, height)) if use_tiny_yolo: yolo_model_def = hrnetPath + "/models/detectors/yolo/config/yolov3-tiny.cfg" yolo_class_path = hrnetPath + "/models/detectors/yolo/data/coco.names" yolo_weights_path = hrnetPath + "/models/detectors/yolo/weights/yolov3-tiny.weights" else: yolo_model_def = hrnetPath + "/models/detectors/yolo/config/yolov3.cfg" yolo_class_path = hrnetPath + "/models/detectors/yolo/data/coco.names" yolo_weights_path = hrnetPath + "/models/detectors/yolo/weights/yolov3.weights" model = SimpleHRNet(hrnet_c, hrnet_j, hrnet_weights, model_name=hrnet_m, resolution=image_resolution, multiperson=not single_person, return_bounding_boxes=not disable_tracking, max_batch_size=max_batch_size, yolo_model_def=yolo_model_def, yolo_class_path=yolo_class_path, yolo_weights_path=yolo_weights_path, device=device) if not disable_tracking: prev_boxes = None prev_pts = None prev_person_ids = None next_person_id = 0 while True: t = time.time() if filename is not None or disable_vidgear: ret, frame = video.read() if not ret: break if rotation_code is not None: frame = cv2.rotate(frame, rotation_code) else: frame = video.read() if frame is None: break pts = model.predict(frame) if not disable_tracking: boxes, pts = pts if not disable_tracking: if len(pts) > 0: if prev_pts is None and prev_person_ids is None: person_ids = np.arange(next_person_id, len(pts) + next_person_id, dtype=np.int32) next_person_id = len(pts) + 1 else: boxes, pts, person_ids = find_person_id_associations( boxes=boxes, pts=pts, prev_boxes=prev_boxes, prev_pts=prev_pts, prev_person_ids=prev_person_ids, next_person_id=next_person_id, pose_alpha=0.2, similarity_threshold=0.4, smoothing_alpha=0.1, ) next_person_id = max(next_person_id, np.max(person_ids) + 1) else: person_ids = np.array((), dtype=np.int32) prev_boxes = boxes.copy() prev_pts = pts.copy() prev_person_ids = person_ids else: person_ids = np.arange(len(pts), dtype=np.int32) for i, (pt, pid) in enumerate(zip(pts, person_ids)): frame = draw_points_and_skeleton( frame, pt, joints_dict()[hrnet_joints_set]['skeleton'], person_index=pid, points_color_palette='gist_rainbow', skeleton_color_palette='jet', points_palette_samples=10) fps = 1. / (time.time() - t) print('\rframerate: %f fps' % fps, end='') if video_writer is None: fourcc = cv2.VideoWriter_fourcc(*video_format) # video format video_writer = cv2.VideoWriter( '/media/koshiba/Data/simple-HRNet/outputData/' + videoName + '_output.avi', fourcc, video_framerate, (frame.shape[1], frame.shape[0])) video_writer.write(frame) video_writer.release() print(person_ids)
def main(camera_id, filename, hrnet_m, hrnet_c, hrnet_j, hrnet_weights, hrnet_joints_set, image_resolution, single_person, use_tiny_yolo, disable_tracking, max_batch_size, disable_vidgear, save_video, video_format, video_framerate, device): if device is not None: device = torch.device(device) else: if torch.cuda.is_available(): torch.backends.cudnn.deterministic = True device = torch.device('cuda:0') else: device = torch.device('cpu') image_resolution = ast.literal_eval(image_resolution) has_display = 'DISPLAY' in os.environ.keys() or sys.platform == 'win32' video_writer = None falldown = FallDown() if foldername is not None: images_path = foldername images_name = os.listdir(images_path) images_path = [os.path.join(images_path, name) for name in images_name] images_path.sort() else: if filename is not None: rotation_code = check_video_rotation(filename) video = cv2.VideoCapture(filename) assert video.isOpened() else: rotation_code = None if disable_vidgear: video = cv2.VideoCapture(camera_id) assert video.isOpened() else: video = CamGear(camera_id).start() if use_tiny_yolo: yolo_model_def = "./models/detectors/yolo/config/yolov3-tiny.cfg" yolo_class_path = "./models/detectors/yolo/data/coco.names" yolo_weights_path = "./models/detectors/yolo/weights/yolov3-tiny.weights" else: yolo_model_def = "./models/detectors/yolo/config/yolov3.cfg" yolo_class_path = "./models/detectors/yolo/data/coco.names" yolo_weights_path = "./models/detectors/yolo/weights/yolov3.weights" model = SimpleHRNet(hrnet_c, hrnet_j, hrnet_weights, model_name=hrnet_m, resolution=image_resolution, multiperson=not single_person, return_bounding_boxes=not disable_tracking, max_batch_size=max_batch_size, yolo_model_def=yolo_model_def, yolo_class_path=yolo_class_path, yolo_weights_path=yolo_weights_path, device=device) if not disable_tracking: prev_boxes = None prev_pts = None prev_person_ids = None next_person_id = 0 step = 0 while True: t = time.time() if foldername is None: if filename is not None or disable_vidgear: ret, frame = video.read() if not ret: break if rotation_code is not None: frame = cv2.rotate(frame, rotation_code) else: frame = video.read() if frame is None: break else: if step >= len(images_path): break # Pre-process images = [] images_origin = [] path = images_path[step] frame = cv2.imread(path, cv2.IMREAD_COLOR) if frame is None: logging.error("read image error: {}. skip it.".format(path)) continue pts = model.predict(frame) if not disable_tracking: boxes, pts = pts if not disable_tracking: if len(pts) > 0: if prev_pts is None and prev_person_ids is None: person_ids = np.arange(next_person_id, len(pts) + next_person_id, dtype=np.int32) next_person_id = len(pts) + 1 else: boxes, pts, person_ids = find_person_id_associations( boxes=boxes, pts=pts, prev_boxes=prev_boxes, prev_pts=prev_pts, prev_person_ids=prev_person_ids, next_person_id=next_person_id, pose_alpha=0.2, similarity_threshold=0.4, smoothing_alpha=0.1, ) next_person_id = max(next_person_id, np.max(person_ids) + 1) else: person_ids = np.array((), dtype=np.int32) prev_boxes = boxes.copy() prev_pts = pts.copy() prev_person_ids = person_ids else: person_ids = np.arange(len(pts), dtype=np.int32) for i, (pt, pid) in enumerate(zip(pts, person_ids)): frame = draw_points_and_skeleton( frame, pt, joints_dict()[hrnet_joints_set]['skeleton'], person_index=pid, points_color_palette='gist_rainbow', skeleton_color_palette='jet', points_palette_samples=10) frame = falldown.check_fall_down( frame, pt, joints_dict()[hrnet_joints_set]['skeleton'], video_framerate) fps = 1. / (time.time() - t) print('\rframerate: %f fps' % fps, end='') if has_display: cv2.imshow('frame.png', frame) k = cv2.waitKey(1) if k == 27: # Esc button if disable_vidgear: video.release() else: video.stop() break else: cv2.imwrite('frame.png', frame) if save_video: if video_writer is None: fourcc = cv2.VideoWriter_fourcc(*video_format) # video format video_writer = cv2.VideoWriter( 'output.avi', fourcc, video_framerate, (frame.shape[1], frame.shape[0])) video_writer.write(frame) if save_video: video_writer.release()
def extract_poses(filename, single_person, Notuse_tiny_yolo): hrnet_m = 'HRNet' hrnet_c = 32 hrnet_j = 17 hrnet_weights = "./weights/pose_hrnet_w32_256x192.pth" image_resolution = '(384, 288)' max_batch_size = 16 # device = None # if device is not None: # device = torch.device(device) # else: if torch.cuda.is_available(): torch.backends.cudnn.deterministic = True device = torch.device('cuda') else: device = torch.device('cpu') # print(device) dict_frametoJson = {"frames": []} image_resolution = ast.literal_eval(image_resolution) rotation_code = check_video_rotation(filename) video = cv2.VideoCapture(filename) assert video.isOpened() # nof_frames = video.get(cv2.CAP_PROP_FRAME_COUNT) if Notuse_tiny_yolo: # default 는 tiny yolo를 사용 yolo_model_def = "./models/detectors/yolo/config/yolov3.cfg" yolo_class_path = "./models/detectors/yolo/data/coco.names" yolo_weights_path = "./models/detectors/yolo/weights/yolov3.weights" else: yolo_model_def = "./models/detectors/yolo/config/yolov3-tiny.cfg" yolo_class_path = "./models/detectors/yolo/data/coco.names" yolo_weights_path = "./models/detectors/yolo/weights/yolov3-tiny.weights" model = SimpleHRNet(hrnet_c, hrnet_j, hrnet_weights, model_name=hrnet_m, resolution=image_resolution, multiperson=not single_person, max_batch_size=max_batch_size, yolo_model_def=yolo_model_def, yolo_class_path=yolo_class_path, yolo_weights_path=yolo_weights_path, device=device) index = 0 while True: # t = time.time() ret, frame = video.read() if not ret: break if rotation_code is not None: frame = cv2.rotate(frame, rotation_code) pts = model.predict(frame) for j, pt in enumerate(pts): keypoint_byframe = {"frameNo": 0, "keyPoints": []} keyNum = 0 row = [index, j] + pt.flatten().tolist() if j == 0: # Person의 경우만 keypoint_byframe["frameNo"] = index for idx in range(2, len(row), 3): keypoint_byframe["keyPoints"].append( makePoint(row[idx], row[idx + 1], keyNum, row[idx + 2])) keyNum += 1 dict_frametoJson["frames"].append(keypoint_byframe) # fps = 1. / (time.time() - t) # print('\rframe: % 4d / %d - framerate: %f fps ' % (index, nof_frames - 1, fps), end='') index += 1 return json.dumps(dict_frametoJson)
def draw_trajectory(filename, json_filename, save_frame, frame_dir, output_root): makedir(output_root) pts = read_keypoints(json_filename) club_pts = pts[:, 17, :] # left_wrist_pts = pts[:, 9, :] # left_hip_pts = pts[:, 11, :] # right_hip_pts = pts[:, 12, :] if filename is not None: rotation_code = check_video_rotation(filename) video = cv2.VideoCapture(filename) assert video.isOpened() nof_frames = video.get(cv2.CAP_PROP_FRAME_COUNT) prev_boxes = None prev_pts = None prev_person_ids = None next_person_id = 0 dist = 0.0 stop = False frame_idx = 0 points_list = [] makedir(frame_dir) while frame_idx < nof_frames - 1: t = time.time() if filename is not None: ret, frame = video.read() if not ret: break if rotation_code is not None: frame = cv2.rotate(frame, rotation_code) else: frame = video.read() if frame is None: break conf = club_pts[frame_idx, 2] # import pdb;pdb.set_trace() if conf > 0.75: points_list.append(np.flip(club_pts[frame_idx, :2])) prev_pt = None pprev_pt = None prev_i = None pprev_i = None save_p_i = 0 cos = 0 for p_i, pt in enumerate(points_list): # frame = cv2.circle(frame, (int(pt[1]), int(pt[0])), circle_size, (255,0,0), -1) if p_i >= 2: # if euclidean_distance(prev_pt,pt)< int(frame.shape[0]//3): cur_mv = pt - prev_pt prev_mv = prev_pt - pprev_pt if cur_mv[0] != 0 and cur_mv[1] != 0 and prev_mv[ 0] != 0 and prev_mv[1] != 0: dist = euclidean_distance(prev_pt, pt) cos = cos_sim(prev_mv, cur_mv) if cos >= -0.5 and cos <= 1: frame = cv2.line(frame, (int(prev_pt[1]), int(prev_pt[0])), (int(pt[1]), int(pt[0])), (0, 0, 255), 3) save_p_i = p_i # else: # frame = cv2.line(frame, (int(prev_pt[1]), int(prev_pt[0])), (int(pt[1]), int(pt[0])), # (255, 0, 0), 3) else: pass if prev_i != p_i and prev_pt[0] != pt[0] and prev_pt[1] != pt[ 1]: if cos >= -0.5 and cos <= 1: pprev_pt = prev_pt prev_pt = pt pprev_i = prev_i prev_i = p_i # else: # pprev_pt = prev_pt # prev_pt = pt # pprev_i = prev_i # prev_i = p_i elif p_i == 0: pprev_pt = pt pprev_i = p_i elif p_i == 1: prev_pt = pt prev_i = p_i if save_frame: frame_path = os.path.join(frame_dir, '%06d.jpg' % save_p_i) cv2.imwrite(frame_path, frame) cv2.putText(frame, f'frame : {frame_idx}', (15, 25), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2) cv2.putText(frame, f'dist : {dist:.2f}', (250, 25), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2) cv2.imshow('frame', frame) if stop: import pdb pdb.set_trace() fps = 1. / (time.time() - t) print('\rframe: % 4d / %d - framerate: %f fps ' % (frame_idx, nof_frames - 1, fps), end='') frame_idx += 1 end_flag = False if cv2.waitKey(10) & 0xFF == 32: while (1): if cv2.waitKey(10) & 0xFF == 32: break if cv2.waitKey(10) & 0xFF == ord('q'): end_flag = True break if cv2.waitKey(10) & 0xFF == ord('q') or end_flag: break
def main(camera_id, filename, hrnet_m, hrnet_c, hrnet_j, hrnet_weights, hrnet_joints_set, image_resolution, single_person, use_tiny_yolo, disable_tracking, max_batch_size, disable_vidgear, save_video, video_format, video_framerate, device): if device is not None: device = torch.device(device) else: if torch.cuda.is_available(): torch.backends.cudnn.deterministic = True device = torch.device('cuda') else: device = torch.device('cpu') # print(device) image_resolution = ast.literal_eval(image_resolution) has_display = 'DISPLAY' in os.environ.keys() or sys.platform == 'win32' video_writer = None if filename is not None: rotation_code = check_video_rotation(filename) video = cv2.VideoCapture(filename) assert video.isOpened() #nof_frames = video.get(cv2.CAP_PROP_FRAME_COUNT) else: rotation_code = None if disable_vidgear: video = cv2.VideoCapture(camera_id) assert video.isOpened() else: video = CamGear(camera_id).start() if use_tiny_yolo: yolo_model_def = "./models/detectors/yolo/config/yolov3-tiny.cfg" yolo_class_path = "./models/detectors/yolo/data/coco.names" yolo_weights_path = "./models/detectors/yolo/weights/yolov3-tiny.weights" else: yolo_model_def = "./models/detectors/yolo/config/yolov3.cfg" yolo_class_path = "./models/detectors/yolo/data/coco.names" yolo_weights_path = "./models/detectors/yolo/weights/yolov3.weights" model = SimpleHRNet(hrnet_c, hrnet_j, hrnet_weights, model_name=hrnet_m, resolution=image_resolution, multiperson=not single_person, return_bounding_boxes=not disable_tracking, max_batch_size=max_batch_size, yolo_model_def=yolo_model_def, yolo_class_path=yolo_class_path, yolo_weights_path=yolo_weights_path, device=device) if not disable_tracking: prev_boxes = None prev_pts = None prev_person_ids = None next_person_id = 0 steps_cnt = 1 flag = 0 while True: t = time.time() if filename is not None or disable_vidgear: ret, frame = video.read() nof_frames = video.get(cv2.CAP_PROP_POS_FRAMES) print(nof_frames) # #Code for bounding box and cropping of the video # bbox, label, conf = cv.detect_common_objects(frame) # frame_bounding = draw_bbox(frame, bbox, label, conf) # #bb.add(image, left, top, right, bottom, label, color) # if save_video: # if video_writer is None: # fourcc = cv2.VideoWriter_fourcc(*video_format) # video format # video_writer = cv2.VideoWriter('output_bounding.avi', fourcc, video_framerate, (frame.shape[1], frame.shape[0])) # video_writer.write(frame_bounding) if not ret: print('\rstep_count: %d' % steps_cnt, end='\n') #print (steps_cnt) break if rotation_code is not None: frame = cv2.rotate(frame, rotation_code) else: frame = video.read() if frame is None: break pts = model.predict(frame) #print(pts[1][0][0][2]) left_hip = np.array(pts[1][0][11]) left_knee = np.array(pts[1][0][13]) left_ankle = np.array(pts[1][0][15]) right_hip = np.array(pts[1][0][12]) right_knee = np.array(pts[1][0][14]) right_ankle = np.array(pts[1][0][16]) ba = left_hip - left_knee bc = left_ankle - left_knee left_cosine_angle = np.dot( ba, bc) / (np.linalg.norm(ba) * np.linalg.norm(bc)) left_angle = np.arccos(left_cosine_angle) left_angle = np.degrees(left_angle) position1 = (10, 50) position1_1 = (200, 50) de = right_hip - right_knee df = right_ankle - right_knee right_cosine_angle = np.dot( de, df) / (np.linalg.norm(de) * np.linalg.norm(df)) right_angle = np.arccos(right_cosine_angle) right_angle = np.degrees(right_angle) position2 = (10, 100) position2_1 = (200, 100) if (left_angle >= 130 and right_angle < 110) or (right_angle >= 130 and left_angle < 110): gc = 'Yes' flag = 0 else: gc = 'No' if (flag == 0): steps_cnt = steps_cnt + 1 flag = 1 position3 = (10, 150) position3_1 = (300, 150) # fontScale fontScale = 1 # Blue color in BGR color = (0, 0, 255) # Line thickness of 2 px thickness = 2 #print (left_angle) #print (right_angle) if not disable_tracking: boxes, pts = pts if not disable_tracking: if len(pts) > 0: if prev_pts is None and prev_person_ids is None: person_ids = np.arange(next_person_id, len(pts) + next_person_id, dtype=np.int32) next_person_id = len(pts) + 1 else: boxes, pts, person_ids = find_person_id_associations( boxes=boxes, pts=pts, prev_boxes=prev_boxes, prev_pts=prev_pts, prev_person_ids=prev_person_ids, next_person_id=next_person_id, pose_alpha=0.2, similarity_threshold=0.4, smoothing_alpha=0.1, ) next_person_id = max(next_person_id, np.max(person_ids) + 1) else: person_ids = np.array((), dtype=np.int32) prev_boxes = boxes.copy() prev_pts = pts.copy() prev_person_ids = person_ids else: person_ids = np.arange(len(pts), dtype=np.int32) for i, (pt, pid) in enumerate(zip(pts, person_ids)): frame = draw_points_and_skeleton( frame, pt, joints_dict()[hrnet_joints_set]['skeleton'], person_index=pid, points_color_palette='gist_rainbow', skeleton_color_palette='jet', points_palette_samples=10) fps = 1. / (time.time() - t) print('\rframerate: %f fps' % fps, end='\n') #print(steps_cnt) if has_display: cv2.imshow('frame.png', frame) k = cv2.waitKey(1) if k == 27: # Esc button if disable_vidgear: video.release() else: video.stop() break else: frame = cv2.putText(frame, str('left_angle:'), position1, cv2.FONT_HERSHEY_SIMPLEX, fontScale, color, thickness, cv2.LINE_AA) frame = cv2.putText(frame, str(left_angle), position1_1, cv2.FONT_HERSHEY_SIMPLEX, fontScale, color, thickness, cv2.LINE_AA) frame = cv2.putText(frame, str('right_angle:'), position2, cv2.FONT_HERSHEY_SIMPLEX, fontScale, color, thickness, cv2.LINE_AA) frame = cv2.putText(frame, str(right_angle), position2_1, cv2.FONT_HERSHEY_SIMPLEX, fontScale, color, thickness, cv2.LINE_AA) frame = cv2.putText(frame, str('Ground_Contact:'), position3, cv2.FONT_HERSHEY_SIMPLEX, fontScale, color, thickness, cv2.LINE_AA) frame = cv2.putText(frame, str(gc), position3_1, cv2.FONT_HERSHEY_SIMPLEX, fontScale, color, thickness, cv2.LINE_AA) #bbox, label, conf = cv.detect_common_objects(frame) #frame = draw_bbox(frame, bbox, label, conf) #bb.add(image, left, top, right, bottom, label, color) cv2.imwrite('frame.png', frame) if save_video: if video_writer is None: fourcc = cv2.VideoWriter_fourcc(*video_format) # video format video_writer = cv2.VideoWriter( 'output.avi', fourcc, video_framerate, (frame.shape[1], frame.shape[0])) video_writer.write(frame) if save_video: video_writer.release()