コード例 #1
0
def test():
    # cpu or gpu?
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    print('using device {}'.format(device))

    # load net
    net = Net()
    net.to(device)
    net.load_state_dict(torch.load('trained_net.pt'))
    net.train(False)

    # sample data
    val_idx = np.load('val_idx.npy')
    val_sampler = SubsetRandomSampler(val_idx)
    pose_dataset = PoseDataset('panoptic_dataset.pickle')
    val_loader = DataLoader(dataset=pose_dataset,
                            batch_size=1,
                            sampler=val_sampler)

    while True:
        data_iter = iter(val_loader)
        skel_2d, skel_z = next(data_iter)
        print(skel_2d)

        # inference
        skel_2d = skel_2d.to(device)
        z_out = net(skel_2d)

        # show
        skel_2d = skel_2d.cpu().numpy()
        skel_2d = skel_2d.reshape((2, -1), order='F')  # [(x,y) x n_joint]
        z_out = z_out.detach().cpu().numpy()
        z_out = z_out.reshape(-1)
        z_gt = skel_z.numpy().reshape(-1)
        show_skeletons(skel_2d, z_out, z_gt)
コード例 #2
0
def show_camera(args):
    # To flip the image, modify the flip_method parameter (0 and 2 are the most common)
    print(gstreamer_pipeline(flip_method=0))
    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")
    model = Net()
    model.load_state_dict(torch.load(args.model_path))
    model = model.to(device)

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

    cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0),
                           cv2.CAP_GSTREAMER)
    if cap.isOpened():
        window_handle = cv2.namedWindow('Camera', cv2.WINDOW_AUTOSIZE)
        cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
        cv2.namedWindow('inputs', cv2.WINDOW_AUTOSIZE)
        # Window
        while cv2.getWindowProperty('Camera', 0) >= 0:
            ret_val, img = cap.read()
            # Transform inputs
            image0, image, inputs = transform_inputs(img, device)
            # Run Model Evaluation
            output = model(inputs)
            index = output.data.cpu().numpy().argmax()
            cv2.putText(img, str(index), org, font, fontScale, color,
                        thickness, cv2.LINE_AA)
            cv2.imshow('Camera', img)
            cv2.imshow("image", cv2.resize(image0, (200, 200)))
            cv2.imshow("inputs", cv2.resize(image, (200, 200)))
            # This also acts as
            keyCode = cv2.waitKey(30) & 0xff
            # Stop the program on the ESC key
            if keyCode == 27:
                break
        cap.release()
        cv2.destroyAllWindows()
    else:
        print('Unable to open camera')
コード例 #3
0
test_path = 'D:\\DeapLearn Project\\ License plate recognition\\single_num\\resize\\'

test_data = MyDataSet(test_path)
new_test_loader = DataLoader(test_data,
                             batch_size=32,
                             shuffle=False,
                             pin_memory=True,
                             num_workers=0)

# 创造一个一模一样的模型
model = Net()
# 加载预训练模型的参数
model.load_state_dict(torch.load('Model.pth'))

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)


def test():
    correct = 0
    total = 0
    with torch.no_grad():
        for _, data in enumerate(new_test_loader, 0):
            inputs, _ = data[0], data[1]
            inputs = inputs.to(device)
            outputs = model(inputs)
            # print(outputs.shape)
            _, prediction = torch.max(outputs.data, dim=1)
            print('-' * 40)
            # print(target)
            # print(prediction)
コード例 #4
0
def show_camera(args):
    # To flip the image, modify the flip_method parameter (0 and 2 are the most common)
    print(gstreamer_pipeline(flip_method=0))
    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")
    model = Net()
    model.load_state_dict(torch.load(args.model_path))
    model = model.to(device)

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

    cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
    if cap.isOpened():
        window_handle = cv2.namedWindow('Camera', cv2.WINDOW_AUTOSIZE)

        # Window 
        while cv2.getWindowProperty('Camera',0) >= 0:
            ret_val, img = cap.read()

            # Convert to grayscale and apply Gaussian filtering
            im_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            im_gray = cv2.GaussianBlur(im_gray, (5, 5), 0)

            # Threshold the image
            ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV)

            # Find contours in the image
            im2, ctrs, hier = cv2.findContours(im_th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

            # Get rectangles contains each contour
            rects = [cv2.boundingRect(ctr) for ctr in ctrs]

            # For each rectangular region, calculate HOG features and predict
            # the digit using Linear SVM.
            for rect in rects:
                # Draw the rectangles
                cv2.rectangle(img, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3) 
                # Make the rectangular region around the digit
                leng = int(rect[3] * 1.6)
                pt1 = int(rect[1] + rect[3] // 2 - leng // 2)
                pt2 = int(rect[0] + rect[2] // 2 - leng // 2)
                roi = im_gray[pt1:pt1+leng, pt2:pt2+leng]
                # Resize the image
                h, w = roi.shape
                if h > 10 and w > 10:
                    # Transform inputs
                    inputs = transform_inputs(roi, device)
                    # Run Model Evaluation
                    output = model(inputs)
                    result = output.data.cpu().numpy().argmax()
                    cv2.putText(img, str(result), (rect[0], rect[1]),cv2.FONT_HERSHEY_DUPLEX, 2, (0, 255, 255), 3)
            
            cv2.imshow("Camera", img)

	    # This also acts as 
            keyCode = cv2.waitKey(30) & 0xff
            # Stop the program on the ESC key
            if keyCode == 27:
               break
        cap.release()
        cv2.destroyAllWindows()
    else:
        print('Unable to open camera')
コード例 #5
0
class Inferencing():
    def __init__(self, network_path, dataset_path, validation_set):
        self.network_path = network_path
        self.dataset_path = dataset_path
        self.validation_set = validation_set
        self.device =\
            torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self.net = Net()
        self.net.to(self.device)
        self.net.load_state_dict(
            torch.load(self.network_path, map_location='cpu'))
        self.net.train(False)

    def get3Dcoordinates(self, skel_2d):
        """Returns Z(3D) coordinates of a 2D pose"""
        skel_2d = skel_2d.to(self.device)
        z_out = self.net(skel_2d)
        z_out = z_out.detach().cpu().numpy()
        z_out = z_out.reshape(-1)
        return z_out

    def testSample(self):
        """Inferences on 5 random samples."""
        val_idx = np.load(self.validation_set)
        val_sampler = SubsetRandomSampler(val_idx)
        pose_dataset = PoseDataset(self.dataset_path)
        val_loader = DataLoader(dataset=pose_dataset, batch_size=1,\
            sampler=val_sampler)
        for i in range(5):
            data_iter = iter(val_loader)
            skel_2d, skel_z = next(data_iter)

            # inference
            skel_2d = skel_2d.to(self.device)
            z_out = self.net(skel_2d)

            # show
            skel_2d = skel_2d.cpu().numpy()
            skel_2d = skel_2d.reshape((2, -1), order='F')  # [(x,y) x n_joint]
            z_out = z_out.detach().cpu().numpy()
            z_out = z_out.reshape(-1)
            z_gt = skel_z.numpy().reshape(-1)
            self.show_skeletons(skel_2d, z_out, z_gt)

    def show_skeletons(self, skel_2d, z_out, z_gt=None):
        """Show skeleton in 2D and 3D, includes full upper body and headself.

        Keyword Arguments:
        skel_2d - skeleton with x,y coordinates
        z_out - predicted z coordinates (for 3d)
        z_gt - ground truth z coordinates
        """
        fig = plt.figure(figsize=(20, 20))
        ax1 = fig.add_subplot(1, 2, 1)
        ax2 = fig.add_subplot(1, 2, 2, projection='3d')
        edges = np.array([[1, 0], [0, 2], [2, 3], [3, 4], [0, 5], [5, 6],
                          [6, 7]])

        ax_2d = ax1
        ax_3d = ax2

        # draw 3d
        for edge in edges:
            ax_3d.plot(skel_2d[0, edge],
                       z_out[edge],
                       skel_2d[1, edge],
                       color='r')
            if z_gt is not None:
                ax_3d.plot(skel_2d[0, edge],
                           z_gt[edge],
                           skel_2d[1, edge],
                           color='g')

        ax_3d.set_aspect('equal')
        ax_3d.set_xlabel("x"), ax_3d.set_ylabel("z"), ax_3d.set_zlabel("y")
        ax_3d.set_xlim3d([-2,
                          2]), ax_3d.set_ylim3d([2, -2
                                                 ]), ax_3d.set_zlim3d([2, -2])
        ax_3d.view_init(elev=10, azim=-45)

        # draw 2d
        for edge in edges:
            ax_2d.plot(skel_2d[0, edge], skel_2d[1, edge], color='r')

        ax_2d.set_aspect('equal')
        ax_2d.set_xlabel("x"), ax_2d.set_ylabel("y")
        ax_2d.set_xlim([-2, 2]), ax_2d.set_ylim([2, -2])

        plt.show()