Beispiel #1
0
 def show_detections(self, head_pose_detections, frame):
     for det in head_pose_detections:
         utils.draw_axis(frame,
                         det.yaw,
                         det.pitch,
                         det.roll,
                         tdx=(det.x_min + det.x_max) / 2,
                         tdy=(det.y_min + det.y_max) / 2,
                         size=det.bbox_height / 2)
         cv2.rectangle(frame, (det.x_min, det.y_min),
                       (det.x_max, det.y_max), (0, 255, 0), 1)
Beispiel #2
0
 def save_test(self, name, save_dir, prediction):
     img_path = os.path.join(self.data_dir, name + '.png')
     print("IMG PATH:", img_path)
 
     cv2_img = cv2.imread(img_path)
     print("PREDICT:", prediction)
     cv2_img = utils.draw_axis(cv2_img, prediction[0], prediction[1], prediction[2],
                         size=50)
     save_path = os.path.join(save_dir, name + '.png')
     print("SAVE PATH:", save_path)
     cv2.imwrite(save_path, cv2_img)
Beispiel #3
0
def draw_detection(img, bbox, yaw, pitch, roll):
    y_min, x_min, y_max, x_max = bbox
    y_min = max(0, y_min - abs(y_min - y_max) / 10)
    y_max = min(img.shape[0], y_max + abs(y_min - y_max) / 10)
    x_min = max(0, x_min - abs(x_min - x_max) / 5)
    x_max = min(img.shape[1], x_max + abs(x_min - x_max) / 5)
    x_max = min(x_max, img.shape[1])

    draw_axis(img,
              yaw,
              pitch,
              roll,
              tdx=(x_min + x_max) / 2,
              tdy=(y_min + y_max) / 2,
              size=abs(x_max - x_min) // 2)

    cv2.putText(img, "yaw: {}".format(np.round(yaw)), (int(x_min), int(y_min)),
                cv2.FONT_HERSHEY_SIMPLEX, 0.4, (100, 255, 0), 1)
    cv2.putText(img, "pitch: {}".format(np.round(pitch)),
                (int(x_min), int(y_min) - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                (100, 255, 0), 1)
    cv2.putText(img, "roll: {}".format(np.round(roll)),
                (int(x_min), int(y_min) - 30), cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                (100, 255, 0), 1)
Beispiel #4
0
 def save_result(self, name, yaw, pitch, roll):
     image = cv2.imread(os.path.join(self.test_dir, name + self.test_format))
     cv2_img = utils.draw_axis(image, yaw, pitch, roll, tdx=image.shape[0]//2, tdy=image.shape[1]//2,size=100)
     save_path = os.path.join(self.result_dir, name + self.test_format)
     cv2.imwrite(save_path, cv2_img)
                    draw = cv2.rectangle(draw,
                                         (face_boxes[i][0], face_boxes[i][1]),
                                         (face_boxes[i][2], face_boxes[i][3]),
                                         (0, 0, 255), 2)

                    face_box_width = face_boxes[i][2] - face_boxes[i][0]
                    axis_x, axis_y = utils.unnormalize_landmark_point(
                        (landmark[4], landmark[5]),
                        net_input_size,
                        scale=scale)
                    axis_x += face_boxes[i][0]
                    axis_y += face_boxes[i][1]
                    draw = utils.draw_axis(
                        draw,
                        yaw,
                        pitch,
                        roll,
                        tdx=face_boxes[i][0] + face_box_width // 5,
                        tdy=face_boxes[i][1] + face_box_width // 5,
                        size=face_box_width // 2)

                    for j in range(5):
                        x = landmark[2 * j]
                        y = landmark[2 * j + 1]
                        x, y = utils.unnormalize_landmark_point((x, y),
                                                                net_input_size,
                                                                scale=scale)
                        x += face_boxes[i][0]
                        y += face_boxes[i][1]
                        x = int(x)
                        y = int(y)
                        draw = cv2.circle(draw, (x, y), 2, (255, 0, 0), 2)
                pitch_predicted = softmax(pitch)
                roll_predicted = softmax(roll)
                # Get continuous predictions in degrees.
                yaw_predicted = torch.sum(
                    yaw_predicted.data[0] * idx_tensor) * 3 - 99
                pitch_predicted = torch.sum(
                    pitch_predicted.data[0] * idx_tensor) * 3 - 99
                roll_predicted = torch.sum(
                    roll_predicted.data[0] * idx_tensor) * 3 - 99

                # Print new frame with cube and axis
                # utils.plot_pose_cube(frame, yaw_predicted, pitch_predicted, roll_predicted, (xw1 + xw2) / 2, (yw1 + yw2) / 2, size = w)
                utils.draw_axis(frame,
                                yaw_predicted,
                                pitch_predicted,
                                roll_predicted,
                                tdx=(xw1 + xw2) / 2,
                                tdy=(yw1 + yw2) / 2,
                                size=w / 2)

                bounding_box = d['box']

                cv2.rectangle(frame,(bounding_box[0], bounding_box[1]), \
                              (bounding_box[0]+bounding_box[2], bounding_box[1] + bounding_box[3]), \
                              (140,255,255), 3)

        out.write(frame)
        frame_num += 1

    out.release()
    video.release()