Exemple #1
0
    def update(self):
        if self.opt.face:
            from alphapose.face.prnet import PRN
            face_3d_model = PRN(self.opt.device)
        if self.save_video:
            # initialize the file video stream, adapt ouput video resolution to original video
            stream = cv2.VideoWriter(*[
                self.video_save_opt[k]
                for k in ['savepath', 'fourcc', 'fps', 'frameSize']
            ])
            if not stream.isOpened():
                print("Try to use other video encoders...")
                ext = self.video_save_opt['savepath'].split('.')[-1]
                fourcc, _ext = self.recognize_video_ext(ext)
                self.video_save_opt['fourcc'] = fourcc
                self.video_save_opt[
                    'savepath'] = self.video_save_opt['savepath'][:-4] + _ext
                stream = cv2.VideoWriter(*[
                    self.video_save_opt[k]
                    for k in ['savepath', 'fourcc', 'fps', 'frameSize']
                ])
            assert stream.isOpened(), 'Cannot open video for writing'
        # keep looping infinitelyd
        while True:
            # ensure the queue is not empty and get item
            (boxes, scores, ids, hm_data, cropped_boxes, orig_img,
             im_name) = self.wait_and_get(self.result_queue)
            if orig_img is None:
                # if the thread indicator variable is set (img is None), stop the thread
                self.wait_and_put(self.final_result_queue, None)
                if self.save_video:
                    stream.release()
                return
            # image channel RGB->BGR
            orig_img = np.array(orig_img, dtype=np.uint8)[:, :, ::-1]
            if boxes is None:
                if self.opt.save_img or self.save_video or self.opt.vis:
                    self.write_image(
                        orig_img,
                        im_name,
                        stream=stream if self.save_video else None)
            else:
                # location prediction (n, kp, 2) | score prediction (n, kp, 1)
                pred = hm_data.cpu().data.numpy()
                assert pred.ndim == 4

                if hm_data.size()[1] == 49:
                    self.eval_joints = [*range(0, 49)]
                pose_coords = []
                pose_scores = []
                for i in range(hm_data.shape[0]):
                    bbox = cropped_boxes[i].tolist()
                    pose_coord, pose_score = self.heatmap_to_coord(
                        pred[i][self.eval_joints], bbox)
                    pose_coords.append(
                        torch.from_numpy(pose_coord).unsqueeze(0))
                    pose_scores.append(
                        torch.from_numpy(pose_score).unsqueeze(0))
                preds_img = torch.cat(pose_coords)
                preds_scores = torch.cat(pose_scores)
                result = pose_nms(boxes, scores, ids, preds_img, preds_scores,
                                  self.opt.min_box_area)

                ### jiasong update 2.24
                if self.opt.face:
                    result = face_process(face_3d_model, result, orig_img)
                ###
                ### jiasong update 5.7

                if self.opt.hand:
                    result = handDetect(result, orig_img)

                ###

                result = {'imgname': im_name, 'result': result}
                # print(result)
                if self.opt.pose_track:
                    poseflow_result = self.pose_flow_wrapper.step(
                        orig_img, result)
                    for i in range(len(poseflow_result)):
                        result['result'][i]['idx'] = poseflow_result[i]['idx']
                self.wait_and_put(self.final_result_queue, result)
                if self.opt.save_img or self.save_video or self.opt.vis:
                    if hm_data.size()[1] == 49:
                        from alphapose.utils.vis import vis_frame_dense as vis_frame
                    elif self.opt.vis_fast:
                        from alphapose.utils.vis import vis_frame_fast as vis_frame
                    else:
                        from alphapose.utils.vis import vis_frame
                    img = vis_frame(orig_img, result, self.opt)
                    self.write_image(
                        img,
                        im_name,
                        stream=stream if self.save_video else None)
    def update(self):
        final_result = []
        norm_type = self.cfg.LOSS.get('NORM_TYPE', None)
        hm_size = self.cfg.DATA_PRESET.HEATMAP_SIZE
        if self.save_video:
            # initialize the file video stream, adapt ouput video resolution to original video
            stream = cv2.VideoWriter(*[
                self.video_save_opt[k]
                for k in ['savepath', 'fourcc', 'fps', 'frameSize']
            ])
            if not stream.isOpened():
                print("Try to use other video encoders...")
                ext = self.video_save_opt['savepath'].split('.')[-1]
                fourcc, _ext = self.recognize_video_ext(ext)
                self.video_save_opt['fourcc'] = fourcc
                self.video_save_opt[
                    'savepath'] = self.video_save_opt['savepath'][:-4] + _ext
                stream = cv2.VideoWriter(*[
                    self.video_save_opt[k]
                    for k in ['savepath', 'fourcc', 'fps', 'frameSize']
                ])
            assert stream.isOpened(), 'Cannot open video for writing'
        # keep looping infinitelyd
        while True:
            # ensure the queue is not empty and get item
            (boxes, scores, ids, hm_data, cropped_boxes, orig_img,
             im_name) = self.wait_and_get(self.result_queue)
            if orig_img is None:
                # if the thread indicator variable is set (img is None), stop the thread
                if self.save_video:
                    stream.release()
                op_result = write_json(final_result,
                                       self.opt.outputpath,
                                       form=self.opt.format,
                                       for_eval=self.opt.eval)

                # Cheking the prediction value and assiging to the global variable
                print("Results have been written to json.")
                return
            # image channel RGB->BGR
            orig_img = np.array(orig_img, dtype=np.uint8)[:, :, ::-1]
            if boxes is None or len(boxes) == 0:
                if self.opt.save_img or self.save_video or self.opt.vis:
                    print("orignal first")
                    self.write_image(
                        orig_img,
                        im_name,
                        stream=stream if self.save_video else None)

            else:
                # location prediction (n, kp, 2) | score prediction (n, kp, 1)
                assert hm_data.dim() == 4
                #pred = hm_data.cpu().data.numpy()

                if hm_data.size()[1] == 136:
                    self.eval_joints = [*range(0, 136)]
                elif hm_data.size()[1] == 26:
                    self.eval_joints = [*range(0, 26)]
                pose_coords = []
                pose_scores = []
                for i in range(hm_data.shape[0]):
                    bbox = cropped_boxes[i].tolist()
                    pose_coord, pose_score = self.heatmap_to_coord(
                        hm_data[i][self.eval_joints],
                        bbox,
                        hm_shape=hm_size,
                        norm_type=norm_type)
                    pose_coords.append(
                        torch.from_numpy(pose_coord).unsqueeze(0))
                    pose_scores.append(
                        torch.from_numpy(pose_score).unsqueeze(0))
                preds_img = torch.cat(pose_coords)
                preds_scores = torch.cat(pose_scores)
                if not self.opt.pose_track:
                    boxes, scores, ids, preds_img, preds_scores, pick_ids = \
                     pose_nms(boxes, scores, ids, preds_img, preds_scores, self.opt.min_box_area)

                _result = []
                for k in range(len(scores)):
                    _result.append({
                        'keypoints':
                        preds_img[k],
                        'kp_score':
                        preds_scores[k],
                        'proposal_score':
                        torch.mean(preds_scores[k]) + scores[k] +
                        1.25 * max(preds_scores[k]),
                        'idx':
                        ids[k],
                        'bbox': [
                            boxes[k][0], boxes[k][1],
                            boxes[k][2] - boxes[k][0],
                            boxes[k][3] - boxes[k][1]
                        ]
                    })

                result = {'imgname': im_name, 'result': _result}

                if self.opt.pose_flow:
                    poseflow_result = self.pose_flow_wrapper.step(
                        orig_img, result)
                    for i in range(len(poseflow_result)):
                        result['result'][i]['idx'] = poseflow_result[i]['idx']

                final_result.append(result)
                if self.opt.save_img or self.save_video or self.opt.vis:
                    if hm_data.size()[1] == 49:
                        from alphapose.utils.vis import vis_frame_dense as vis_frame
                    elif self.opt.vis_fast:
                        from alphapose.utils.vis import vis_frame_fast as vis_frame
                    else:
                        from alphapose.utils.vis import vis_frame
                    img = vis_frame(orig_img, result, self.opt)
                    global number
                    number = number + 1
                    print("img second & image no :- ", number)
                    op_result = write_json(final_result,
                                           number,
                                           self.opt.outputpath,
                                           form=self.opt.format,
                                           for_eval=self.opt.eval)
                    output_prediction = op_result
                    print("after img second calling write json : result is ",
                          output_prediction)
                    self.write_image(
                        img,
                        output_prediction,
                        im_name,
                        stream=stream if self.save_video else None)
Exemple #3
0
    def update(self):
        ####
        person_height = 165
        frame_offset = 20
        max_diff_angle = 15
        max_diff_distance = 10
        N_angle = 23
        N_distance = 20
        #
        frames = []
        ground_points = []
        head_points = []
        final_result = []
        final_angles = {'Frame': []}
        final_min_angles = {'Frame': []}
        final_max_angles = {'Frame': []}
        final_distances = {'Frame': []}
        final_min_distances = {'Frame': []}
        final_max_distances = {'Frame': []}
        #
        for i in range(1, N_angle + 1):
            final_angles['Angle_' + str(i)] = []
            final_min_angles['Angle_' + str(i)] = []
            final_max_angles['Angle_' + str(i)] = []
        for i in range(1, N_distance + 1):
            final_distances['Distance_' + str(i)] = []
            final_min_distances['Distance_' + str(i)] = []
            final_max_distances['Distance_' + str(i)] = []
        #
        frame = 0
        min_angle = 180
        max_angle = 0
        min_distance = person_height + 100
        max_distance = 0
        #####
        norm_type = self.cfg.LOSS.get('NORM_TYPE', None)
        hm_size = self.cfg.DATA_PRESET.HEATMAP_SIZE
        if self.save_video:
            # initialize the file video stream, adapt ouput video resolution to original video
            stream = cv2.VideoWriter(*[
                self.video_save_opt[k]
                for k in ['savepath', 'fourcc', 'fps', 'frameSize']
            ])
            if not stream.isOpened():
                print("Try to use other video encoders...")
                ext = self.video_save_opt['savepath'].split('.')[-1]
                fourcc, _ext = self.recognize_video_ext(ext)
                self.video_save_opt['fourcc'] = fourcc
                self.video_save_opt[
                    'savepath'] = self.video_save_opt['savepath'][:-4] + _ext
                stream = cv2.VideoWriter(*[
                    self.video_save_opt[k]
                    for k in ['savepath', 'fourcc', 'fps', 'frameSize']
                ])
            assert stream.isOpened(), 'Cannot open video for writing'
        # keep looping infinitelyd
        while True:
            # ensure the queue is not empty and get item
            (boxes, scores, ids, hm_data, cropped_boxes, orig_img,
             im_name) = self.wait_and_get(self.result_queue)
            if orig_img is None:
                # if the thread indicator variable is set (img is None), stop the thread
                if self.save_video:
                    stream.release()
                write_json(final_result,
                           self.opt.outputpath,
                           form=self.opt.format,
                           for_eval=self.opt.eval)
                print("Results have been written to json.")
                return
            # image channel RGB->BGR
            orig_img = np.array(orig_img, dtype=np.uint8)[:, :, ::-1]
            if boxes is None or len(boxes) == 0:
                if self.opt.save_img or self.save_video or self.opt.vis:
                    self.write_image(
                        orig_img,
                        im_name,
                        stream=stream if self.save_video else None)
            else:
                # location prediction (n, kp, 2) | score prediction (n, kp, 1)
                assert hm_data.dim() == 4
                #pred = hm_data.cpu().data.numpy()

                if hm_data.size()[1] == 136:
                    self.eval_joints = [*range(0, 136)]
                elif hm_data.size()[1] == 26:
                    self.eval_joints = [*range(0, 26)]
                pose_coords = []
                pose_scores = []
                for i in range(hm_data.shape[0]):
                    bbox = cropped_boxes[i].tolist()
                    pose_coord, pose_score = self.heatmap_to_coord(
                        hm_data[i][self.eval_joints],
                        bbox,
                        hm_shape=hm_size,
                        norm_type=norm_type)
                    pose_coords.append(
                        torch.from_numpy(pose_coord).unsqueeze(0))
                    pose_scores.append(
                        torch.from_numpy(pose_score).unsqueeze(0))
                preds_img = torch.cat(pose_coords)
                preds_scores = torch.cat(pose_scores)
                if not self.opt.pose_track:
                    boxes, scores, ids, preds_img, preds_scores, pick_ids = \
                        pose_nms(boxes, scores, ids, preds_img, preds_scores, self.opt.min_box_area)

                _result = []
                for k in range(len(scores)):
                    _result.append({
                        'keypoints':
                        preds_img[k],
                        'kp_score':
                        preds_scores[k],
                        'proposal_score':
                        torch.mean(preds_scores[k]) + scores[k] +
                        1.25 * max(preds_scores[k]),
                        'idx':
                        ids[k],
                        'box': [
                            boxes[k][0], boxes[k][1],
                            boxes[k][2] - boxes[k][0],
                            boxes[k][3] - boxes[k][1]
                        ]
                    })

                result = {'imgname': im_name, 'result': _result}

                if self.opt.pose_flow:
                    poseflow_result = self.pose_flow_wrapper.step(
                        orig_img, result)
                    for i in range(len(poseflow_result)):
                        result['result'][i]['idx'] = poseflow_result[i]['idx']

                final_result.append(result)
                if self.opt.save_img or self.save_video or self.opt.vis:
                    if hm_data.size()[1] == 49:
                        from alphapose.utils.vis import vis_frame_dense as vis_frame
                    elif self.opt.vis_fast:
                        from alphapose.utils.vis import vis_frame_fast as vis_frame
                    else:
                        from alphapose.utils.vis import vis_frame
                    img = vis_frame(orig_img, result, self.opt)
                    #####
                    frame += 1
                    if frame <= frame_offset:
                        ground_point, head_point = self.calc_bound_points(
                            result, vis_thres=0.4)
                        if ground_point is not None:
                            ground_points.append(ground_point)
                            x_point = [x for x, _ in ground_points]
                            y_point = [y for _, y in ground_points]
                            ground_point = (int(np.average(x_point)),
                                            int(np.average(y_point)))
                        if head_point is not None:
                            head_points.append(head_point)
                            x_point = [x for x, _ in head_points]
                            y_point = [y for _, y in head_points]
                            head_point = (int(np.average(x_point)),
                                          int(np.average(y_point)))
                        if ground_point is not None and head_point is not None:
                            dist_height = np.linalg.norm(
                                np.array(head_point) - np.array(ground_point))
                            height_ratio = person_height / (dist_height + 1e-6)
                        else:
                            height_ratio = 0

                    distances = self.calc_distances(result,
                                                    ground_point,
                                                    head_point,
                                                    height_ratio,
                                                    vis_thres=0.4)
                    angles = self.calc_angles(result, vis_thres=0.4)
                    frames.append(frame)
                    final_angles['Frame'].append(frame)
                    final_min_angles['Frame'].append(frame)
                    final_max_angles['Frame'].append(frame)
                    final_distances['Frame'].append(frame)
                    final_min_distances['Frame'].append(frame)
                    final_max_distances['Frame'].append(frame)
                    ##
                    for angle_name, angle in angles.items():
                        angle = int(angle)
                        if angle < 0 and frame > frame_offset:
                            angle = final_angles[angle_name][frame - 2]
                        ##

                        final_angles[angle_name].append(angle)
                        ##
                        if frame <= frame_offset:
                            if angle >= 0 and angle < min_angle:
                                final_min_angles[angle_name].append(angle)
                            else:
                                final_min_angles[angle_name].append(min_angle)
                            if angle >= 0 and angle > max_angle:
                                final_max_angles[angle_name].append(angle)
                            else:
                                final_max_angles[angle_name].append(max_angle)
                        else:
                            previous_min_angle = final_min_angles[angle_name][
                                frame - 2]
                            previous_max_angle = final_max_angles[angle_name][
                                frame - 2]
                            diff_angle = abs(
                                final_angles[angle_name][frame - 1] -
                                final_angles[angle_name][frame - 2])
                            if angle >= 0 and angle < previous_min_angle and diff_angle < max_diff_angle:
                                final_min_angles[angle_name].append(angle)
                            else:
                                final_min_angles[angle_name].append(
                                    previous_min_angle)
                            if angle >= 0 and angle > previous_max_angle and diff_angle < max_diff_angle:
                                final_max_angles[angle_name].append(angle)
                            else:
                                final_max_angles[angle_name].append(
                                    previous_max_angle)
                        ##
                        plt.figure()
                        plt.plot(frames[frame_offset + 1:],
                                 final_angles[angle_name][frame_offset + 1:])
                        plt.plot(frames[frame_offset + 1:],
                                 final_min_angles[angle_name][frame_offset +
                                                              1:],
                                 linestyle='--',
                                 dashes=(5, 3))
                        plt.plot(frames[frame_offset + 1:],
                                 final_max_angles[angle_name][frame_offset +
                                                              1:],
                                 linestyle='--',
                                 dashes=(5, 3))
                        plt.xlabel('Frames')
                        plt.ylabel('Angle (degree)')
                        plt.title(angle_name)
                        plt.grid(True)
                        plt.savefig(
                            os.path.join(self.opt.outputpath_plot,
                                         angle_name + ".jpg"))
                        plt.close()
                    ##
                    for distance_name, distance in distances.items():
                        distance = round(distance, 2)
                        if distance < 0 and frame > frame_offset:
                            distance = final_distances[distance_name][frame -
                                                                      2]
                        ##
                        final_distances[distance_name].append(distance)
                        ##
                        if frame <= frame_offset:
                            if distance >= 0 and distance < min_distance:
                                final_min_distances[distance_name].append(
                                    distance)
                            else:
                                final_min_distances[distance_name].append(
                                    min_distance)
                            if distance >= 0 and distance > max_distance:
                                final_max_distances[distance_name].append(
                                    distance)
                            else:
                                final_max_distances[distance_name].append(
                                    max_distance)
                        else:
                            previous_min_distance = final_min_distances[
                                distance_name][frame - 2]
                            previous_max_distance = final_max_distances[
                                distance_name][frame - 2]
                            diff_distance = abs(
                                final_distances[distance_name][frame - 1] -
                                final_distances[distance_name][frame - 2])
                            if distance_name is 'Distance_10' or distance_name is 'Distance_11':
                                diff_distance *= 100
                            if distance >= 0 and distance < previous_min_distance and diff_distance < max_diff_distance:
                                final_min_distances[distance_name].append(
                                    distance)
                            else:
                                final_min_distances[distance_name].append(
                                    previous_min_distance)
                            if distance >= 0 and distance > previous_max_distance and diff_distance < max_diff_distance:
                                final_max_distances[distance_name].append(
                                    distance)
                            else:
                                final_max_distances[distance_name].append(
                                    previous_max_distance)
                        ##
                        plt.figure()
                        plt.plot(
                            frames[frame_offset + 1:],
                            final_distances[distance_name][frame_offset + 1:])
                        plt.plot(
                            frames[frame_offset + 1:],
                            final_min_distances[distance_name][frame_offset +
                                                               1:],
                            linestyle='--',
                            dashes=(5, 3))
                        plt.plot(
                            frames[frame_offset + 1:],
                            final_max_distances[distance_name][frame_offset +
                                                               1:],
                            linestyle='--',
                            dashes=(5, 3))
                        plt.xlabel('Frames')
                        plt.ylabel('Distance (cm)')
                        plt.title(distance_name)
                        plt.grid(True)
                        plt.savefig(
                            os.path.join(self.opt.outputpath_plot,
                                         distance_name + ".jpg"))
                        plt.close()
                    ##
                    df_angle = pd.DataFrame.from_dict(final_angles)
                    df_min_angle = pd.DataFrame.from_dict(final_min_angles)
                    df_max_angle = pd.DataFrame.from_dict(final_max_angles)
                    with pd.ExcelWriter(
                            os.path.join(self.opt.outputpath_plot,
                                         "Angles.xlsx")) as writer:
                        df_angle.to_excel(writer,
                                          sheet_name='Angles',
                                          index=False)
                        df_min_angle.to_excel(writer,
                                              sheet_name='Min_Angles',
                                              index=False)
                        df_max_angle.to_excel(writer,
                                              sheet_name='Max_Angles',
                                              index=False)
                    ##
                    df_distance = pd.DataFrame.from_dict(final_distances)
                    df_min_distance = pd.DataFrame.from_dict(
                        final_min_distances)
                    df_max_distance = pd.DataFrame.from_dict(
                        final_max_distances)
                    with pd.ExcelWriter(
                            os.path.join(self.opt.outputpath_plot,
                                         "Distances.xlsx")) as writer:
                        df_distance.to_excel(writer,
                                             sheet_name='Distances',
                                             index=False)
                        df_min_distance.to_excel(writer,
                                                 sheet_name='Min_Distances',
                                                 index=False)
                        df_max_distance.to_excel(writer,
                                                 sheet_name='Max_Distances',
                                                 index=False)
                    #########
                    self.write_image(
                        img,
                        im_name,
                        stream=stream if self.save_video else None,
                        frame=frame)