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)
Ejemplo n.º 2
0
def demo(opt):
    os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpus_str
    opt.debug = max(opt.debug, 1)
    detector = Detector(opt)

    org = (50, 100)
    font = cv2.FONT_HERSHEY_SIMPLEX
    fontScale = 1
    color = (255, 0, 0)
    thickness = 2

    if opt.demo == 'webcam' or \
      opt.demo[opt.demo.rfind('.') + 1:].lower() in video_ext:
        is_video = True
        # demo on video stream
        cam = cv2.VideoCapture(0 if opt.demo == 'webcam' else opt.demo)
    elif 'http' in opt.demo:
        is_video = True
        cam = CamGear(source=opt.demo, stream_mode=True, logging=True).start()
    else:
        is_video = False
        # Demo on images sequences
        if os.path.isdir(opt.demo):
            image_names = []
            ls = os.listdir(opt.demo)
            for file_name in sorted(ls):
                ext = file_name[file_name.rfind('.') + 1:].lower()
                if ext in image_ext:
                    image_names.append(os.path.join(opt.demo, file_name))
        else:
            image_names = [opt.demo]

    # Initialize output video
    out = None
    out_name = opt.demo[opt.demo.rfind('/') + 1:]
    print('out_name', out_name)
    if not os.path.exists('../results'):
        os.mkdir('../results')
    if opt.save_video:
        fourcc = cv2.VideoWriter_fourcc(*'MPEG')
        out = cv2.VideoWriter(
            '../results/{}.avi'.format(opt.exp_id + '_' + out_name[:-4]),
            fourcc, opt.save_framerate, (opt.video_w, opt.video_h))

    if opt.debug < 5:
        detector.pause = False
    cnt = 0
    results = {}
    det_hist = {}

    ad = ActionDetection(opt)
    bowling_df = pd.DataFrame(columns=action_col_names)

    last_det_cnt = {}
    flag_dup_det = {}
    if opt.start_time and not opt.start_frame:
        opt.start_frame = timecode_to_frames(opt.start_time)
        opt.end_frame = timecode_to_frames(opt.end_time)

    cam.set(cv2.CAP_PROP_POS_FRAMES, opt.start_frame)
    cnt = opt.start_frame
    if opt.end_frame == -1:
        opt.end_frame = int(cam.get(cv2.CAP_PROP_FRAME_COUNT))
    print("Frame range", opt.start_frame, opt.end_frame)
    ex_start_time = time.time()
    while True:
        if is_video:
            if 'http' in opt.demo:
                img = cam.read()
            else:
                _, img = cam.read()
            if img is None or cnt > opt.end_frame:
                print("time taken to process ",
                      opt.end_frame - opt.start_frame, "frames",
                      time.time() - ex_start_time)
                bowling_df.to_pickle(
                    '/content/drive/MyDrive/cric_actions/results/results_df_' +
                    str(opt.start_frame) + '_' + str(opt.end_frame) + '.df')
                save_and_exit(opt, out, results, out_name)
        else:
            if cnt < len(image_names):
                img = cv2.imread(image_names[cnt])
            else:
                save_and_exit(opt, out, results, out_name)
        cnt += 1

        # resize the original video for saving video results
        if opt.resize_video:
            img = cv2.resize(img, (opt.video_w, opt.video_h))

        # skip the first X frames of the video
        if cnt < opt.skip_first:
            continue

        # cv2.imshow('input', img)

        # track or detect the image.
        ret = detector.run(img)

        # log run time
        time_str = 'frame {} |'.format(cnt)
        for stat in time_stats:
            time_str = time_str + '{} {:.3f}s |'.format(stat, ret[stat])
        # print(time_str)

        # results[cnt] is a list of dicts:
        #  [{'bbox': [x1, y1, x2, y2], 'tracking_id': id, 'category_id': c, ...}]
        results[cnt] = ret['results']

        for res in ret['results']:
            if res['score'] > opt.vis_thresh:
                if 'active' in res and res['active'] == 0:
                    continue
                if res['tracking_id'] in det_hist:
                    det_hist[res['tracking_id']] = np.append(
                        det_hist[res['tracking_id']],
                        res['ct'].reshape(1, 2),
                        axis=0)
                else:
                    det_hist[res['tracking_id']] = np.array(res['ct'].reshape(
                        1, 2))

        bowling_df_frame, results[cnt] = ad.detect_action(
            cnt, ret['results'], det_hist)
        print("[Frame : " + str(cnt) + "]")
        for ind, res in enumerate(results[cnt]):
            ret['generic'] = cv2.putText(
                ret['generic'],
                str(res['tracking_id']) + " : " + res['action'],
                (org[0], org[1] + (ind + 1) * 50), font, fontScale, color,
                thickness, cv2.LINE_AA)
        ret['generic'] = cv2.putText(ret['generic'], "Frame : " + str(cnt),
                                     (org[0], org[1]), font, fontScale, color,
                                     thickness, cv2.LINE_AA)

        if bowling_df_frame:
            tracking_id = bowling_df_frame[-2]
            if not tracking_id in last_det_cnt:
                last_det_cnt.update({tracking_id: cnt})
            if not tracking_id in flag_dup_det:
                flag_dup_det.update({tracking_id: False})

            if cnt > last_det_cnt[tracking_id] + 50:
                flag_dup_det[tracking_id] = False

            if not flag_dup_det[tracking_id]:
                print("bowling detected by ", tracking_id)
                cv2.imwrite(
                    '/content/drive/MyDrive/cric_actions/results/demo{}.jpg'.
                    format(cnt), ret['generic'])
                bowling_df.loc[len(bowling_df)] = bowling_df_frame
                last_det_cnt.update({tracking_id: cnt})
                flag_dup_det[tracking_id] = True
            else:
                [
                    results[cnt][ind].update({'action': 'idle'})
                    for ind, res in enumerate(results[cnt])
                    if res['tracking_id'] == tracking_id
                ]

        # save debug image to video
        if opt.save_video:
            out.write(ret['generic'])
            if not is_video:
                cv2.imwrite('../results/demo{}.jpg'.format(cnt),
                            ret['generic'])

        if cnt % 1000 == 0:
            save_and_exit(opt, out, results, "inter" + out_name, False)
            bowling_df.to_pickle(
                '/content/drive/MyDrive/cric_actions/results/results.df')

        # esc to quit and finish saving video
        if cv2.waitKey(1) == 27:
            save_and_exit(opt, out, results, out_name)
            return

    save_and_exit(opt, out, results)
Ejemplo n.º 3
0
def start_checking(start_time, video_path, minimum_distance, seconds, waits,
                   frame_width, audio_path, audio_length, camera_target,
                   model):
    # event = threading.Event()
    good_to_run = False
    good_to_write = True
    output_video_1 = None
    loop_count = 0
    # frame_count = 0
    ######################################################
    # 				START THE VIDEO STREAM               #
    ######################################################
    if video_path == 0:
        try:
            # event.set()
            vs = cv2.VideoCapture(0)
            frame_per_seconds = int(vs.get(cv2.CAP_PROP_FPS))
            good_to_run = True
            good_to_write = True
        except:
            # event.clear()
            good_to_run = False
            good_to_write = False
            end = time.time()
            time_elapsed = int(end - start_time)
            # Final("WebCam Failed", f"Time consumed: {time_elapsed} \n Webcam not connected." )

    elif video_path.startswith("http"):
        try:
            vs = CamGear(source=video_path,
                         y_tube=True,
                         time_delay=1,
                         logging=True).start()
            # event.set()
            frame_per_seconds = 30
            good_to_run = True
            good_to_write = True

        except:
            # event.clear()
            good_to_run = False
            good_to_write = False
            end = time.time()
            time_elapsed = int(end - start_time)
            print(f"Time consumed: {time_elapsed} seconds.")
            print(
                "Youtube Video Load Failed. Online video link stopped working."
            )
            # Final("Youtube Video Load Failed.",f"Time consumed: {time_elapsed} seconds. \n Online video link stopped working.")

    else:
        try:
            # event.set()
            vs = cv2.VideoCapture(video_path)
            frame_per_seconds = int(vs.get(cv2.CAP_PROP_FPS))
            good_to_run = True
            good_to_write = True
        except:
            # event.clear()
            good_to_run = False
            good_to_write = False
            end = time.time()
            time_elapsed = int(end - start_time)
            print(f"Time consumed: {time_elapsed} seconds.")
            print("Webcam not connected.")
            os._exit(0)
            # Final("Video Load Failed", f"Time consumed: {time_elapsed} seconds. \n Something wrong in videopath provided." )

    # Loop until the end of the video stream
    while True and good_to_run == True:
        if type(video_path) != int:
            if video_path.startswith("http"):
                try:
                    frame = vs.read()
                    # event.set()
                    good_to_run = True
                    good_to_write = True
                except:
                    # event.clear()
                    good_to_run = False
                    good_to_write = False
                    end = time.time()
                    time_elapsed = int(end - start_time)
                    print(f"Time consumed: {time_elapsed} seconds.")
                    print("Online video link stopped working.")
                    os._exit(0)
                    # Final("Youtube Video Read Failed",f"Time consumed: {time_elapsed} seconds. \n Online video link stopped working.")
            else:
                try:
                    (frame_exist, frame) = vs.read()
                    # event.set()
                    good_to_run = True
                    good_to_write = True
                except:
                    # event.clear()
                    good_to_run = False
                    good_to_write = False
                    end = time.time()
                    time_elapsed = int(end - start_time)
                    print(f"Time consumed: {time_elapsed} seconds.")
                    print("Could not get frames from video.")
                    os._exit(0)
                    # Final("Video load Failed.",f"Time consumed: {time_elapsed} seconds. \n Could not get frames from video.")
        else:
            try:
                (frame_exist, frame) = vs.read()
                # event.set()
                good_to_run = True
                good_to_write = True
            except:
                # event.clear()
                good_to_run = False
                good_to_write = False
                end = time.time()
                time_elapsed = int(end - start_time)
                print(f"Time consumed: {time_elapsed} seconds.")
                print("Could not get frames from video.")
                os._exit(0)
                # Final("Video load Failed.",f"Time consumed: {time_elapsed} seconds. \n Could not get frames from video.")

        if frame is None:
            break
        else:
            # event.set()
            good_to_run = True
            good_to_write = True
            # frame_count += 1
            # Resize the image to the correct size
            frame = image_resize(frame, width=frame_width)

            # Make the predictions for this frame
            (boxes, scores, classes) = model.predict(frame)
            # print(type(boxes))
            # print(type(scores))
            # print(type(classes))

            if len(boxes) > 0:

                # Get the human detected in the frame and return the 2 points to build the bounding box
                array_boxes_detected = get_human_box_detection(
                    boxes, scores[0].tolist(), classes[0].tolist(),
                    frame.shape[0], frame.shape[1])

                if len(array_boxes_detected) > 0:
                    # Both of our lists that will contain the centroïds coordonates and the ground points
                    array_centroids = get_centroids(array_boxes_detected)
                    box_and_centroid = list(
                        zip(array_centroids, array_boxes_detected))

                    # Check if 2 or more people have been detected (otherwise no need to detect)
                    if len(array_centroids) >= 2:
                        close_pairs = []
                        for i, pair in enumerate(
                                itertools.combinations(array_centroids, r=2)):
                            # for i,pair in enumerate(itertools.combinations(array_centroids, r=2)):
                            # Check if the distance between each combination of points is less than the minimum distance chosen
                            distance_between_pair = math.sqrt(
                                (pair[0][0] - pair[1][0])**2 +
                                (pair[0][1] - pair[1][1])**2)
                            # print(distance_between_pair)
                            #Pairs with probability that will not maintain social distancing.
                            if distance_between_pair <= int(
                                    minimum_distance) * 2:
                                #Creating new dictionary containing distances between pairs
                                distance_between_pairs[
                                    f"pairs{i}"] = distance_between_pair
                                #Checking and creating timer for pairs from distance_between_pairs
                                if f"pairs{i}" not in timer_for_each_pairs.keys(
                                ):
                                    timer_for_each_pairs[f"pairs{i}"] = 0

                            if distance_between_pair < int(minimum_distance):
                                close_pairs.append(pair)

                        flat_list = []
                        for sublist in close_pairs:
                            for item in sublist:
                                flat_list.append(item)
                        common_close_pairs = list(set(flat_list))
                        # print(common_close_pairs)
                        boxes_to_make_red = []
                        for ccp in common_close_pairs:
                            for b_and_c in box_and_centroid:
                                if ccp == b_and_c[0]:
                                    boxes_to_make_red.append(b_and_c[1])
                        # print(boxes_to_make_red)
                        for i, items in enumerate(boxes_to_make_red):
                            first_point = boxes_to_make_red[i][0]
                            second_point = boxes_to_make_red[i][1]
                            third_point = boxes_to_make_red[i][2]
                            fourth_point = boxes_to_make_red[i][3]
                            cv2.rectangle(frame, (second_point, first_point),
                                          (fourth_point, third_point),
                                          COLOR_RED, 2)

                        box_and_centroid.clear()
                        close_pairs.clear()
                        flat_list.clear()
                        common_close_pairs.clear()
                        boxes_to_make_red.clear()
            # else:
            # 	print(f"Something is wrong in frame {frame_count}.")

        if len(distance_between_pairs) > 0:
            threading1 = []
            for key, value in distance_between_pairs.items():
                t1 = threading.Thread(target=check_current_value,
                                      args=[key, value, minimum_distance],
                                      daemon=True)
                t1.start()
                threading1.append(t1)
            for thread1 in threading1:
                t1.join()

            t = timer_for_each_pairs.values()
            t_max = max(t)
            if t_max >= seconds and time_to_wait == 0:
                threading.Thread(
                    target=play_warning,
                    args=[start_time, audio_path, frame_per_seconds],
                    daemon=True).start()
                threading.Thread(target=waiting_time,
                                 args=[audio_length, waits, frame_per_seconds],
                                 daemon=True).start()
            #Update dictionary to remove far away pairs. Check for it in only 10 loop to save computation power.
            if loop_count >= 10:
                for k, v in distance_between_pairs.items():
                    if v > int(minimum_distance) * 2:
                        del distance_between_pairs[k]
                        del timer_for_each_pairs[k]
                loop_count = 0
        loop_count += 1

        cv2.imshow("Output", frame)
        # cv2.namedWindow("Final_Output", cv2.WND_PROP_FULLSCREEN)
        # cv2.setWindowProperty("Final_Output",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
        # cv2.imshow("Final_Output", frame)
        key = cv2.waitKey(1) & 0xFF
        if video_path != 0:
            if output_video_1 is None:
                fourcc1 = cv2.VideoWriter_fourcc(*"MJPG")
                output_video_1 = cv2.VideoWriter(
                    "../output_video/video.avi", fourcc1, 25,
                    (frame.shape[1], frame.shape[0]), True)
            elif output_video_1 is not None:
                output_video_1.write(frame)

            # Break the loop
            if key == ord("q"):
                break

    if video_path != 0 and good_to_write == True:
        # while cv2.getWindowProperty('Output', cv2.WND_PROP_VISIBLE)==1.0:
        # 	cv2.destroyWindow("Output")
        end = time.time()
        time.sleep(1)
        time_elapsed = int(end - start_time)
        print(f"Time consumed: {time_elapsed} seconds.")
        print("Successful execution. Video saved in output_folder.")
        os._exit(0)
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 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()