test.py 5.2 KB
Newer Older
jm_12138's avatar
jm_12138 已提交
1 2 3 4 5 6
import os
import shutil
import unittest

import cv2
import numpy as np
jm_12138's avatar
jm_12138 已提交
7
import requests
jm_12138's avatar
jm_12138 已提交
8

jm_12138's avatar
jm_12138 已提交
9
import paddlehub as hub
jm_12138's avatar
jm_12138 已提交
10 11 12 13 14

os.environ['CUDA_VISIBLE_DEVICES'] = '0'


class TestHubModule(unittest.TestCase):
jm_12138's avatar
jm_12138 已提交
15

jm_12138's avatar
jm_12138 已提交
16 17 18 19 20 21 22 23 24 25 26
    @classmethod
    def setUpClass(cls) -> None:
        img_url = 'https://unsplash.com/photos/pg_WCHWSdT8/download?ixid=MnwxMjA3fDB8MXxhbGx8fHx8fHx8fHwxNjYyNDM2ODI4&force=true&w=640'
        if not os.path.exists('tests'):
            os.makedirs('tests')
        response = requests.get(img_url)
        assert response.status_code == 200, 'Network Error.'
        with open('tests/test.jpg', 'wb') as f:
            f.write(response.content)
        fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
        img = cv2.imread('tests/test.jpg')
jm_12138's avatar
jm_12138 已提交
27
        video = cv2.VideoWriter('tests/test.avi', fourcc, 20.0, tuple(img.shape[:2]))
jm_12138's avatar
jm_12138 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40
        for i in range(40):
            video.write(img)
        video.release()
        cls.module = hub.Module(name="humanseg_lite")

    @classmethod
    def tearDownClass(cls) -> None:
        shutil.rmtree('tests')
        shutil.rmtree('inference')
        shutil.rmtree('humanseg_lite_output')
        shutil.rmtree('humanseg_lite_video_result')

    def test_segment1(self):
jm_12138's avatar
jm_12138 已提交
41
        results = self.module.segment(paths=['tests/test.jpg'], use_gpu=False, visualization=False)
jm_12138's avatar
jm_12138 已提交
42 43 44
        self.assertIsInstance(results[0]['data'], np.ndarray)

    def test_segment2(self):
jm_12138's avatar
jm_12138 已提交
45
        results = self.module.segment(images=[cv2.imread('tests/test.jpg')], use_gpu=False, visualization=False)
jm_12138's avatar
jm_12138 已提交
46 47 48
        self.assertIsInstance(results[0]['data'], np.ndarray)

    def test_segment3(self):
jm_12138's avatar
jm_12138 已提交
49
        results = self.module.segment(images=[cv2.imread('tests/test.jpg')], use_gpu=False, visualization=True)
jm_12138's avatar
jm_12138 已提交
50 51 52
        self.assertIsInstance(results[0]['data'], np.ndarray)

    def test_segment4(self):
jm_12138's avatar
jm_12138 已提交
53
        results = self.module.segment(images=[cv2.imread('tests/test.jpg')], use_gpu=True, visualization=False)
jm_12138's avatar
jm_12138 已提交
54 55 56
        self.assertIsInstance(results[0]['data'], np.ndarray)

    def test_segment5(self):
jm_12138's avatar
jm_12138 已提交
57
        self.assertRaises(AssertionError, self.module.segment, paths=['no.jpg'])
jm_12138's avatar
jm_12138 已提交
58 59

    def test_segment6(self):
jm_12138's avatar
jm_12138 已提交
60
        self.assertRaises(AttributeError, self.module.segment, images=['test.jpg'])
jm_12138's avatar
jm_12138 已提交
61 62

    def test_video_stream_segment1(self):
jm_12138's avatar
jm_12138 已提交
63 64 65 66 67
        img_matting, cur_gray, optflow_map = self.module.video_stream_segment(frame_org=cv2.imread('tests/test.jpg'),
                                                                              frame_id=1,
                                                                              prev_gray=None,
                                                                              prev_cfd=None,
                                                                              use_gpu=False)
jm_12138's avatar
jm_12138 已提交
68 69 70
        self.assertIsInstance(img_matting, np.ndarray)
        self.assertIsInstance(cur_gray, np.ndarray)
        self.assertIsInstance(optflow_map, np.ndarray)
jm_12138's avatar
jm_12138 已提交
71 72 73 74 75
        img_matting, cur_gray, optflow_map = self.module.video_stream_segment(frame_org=cv2.imread('tests/test.jpg'),
                                                                              frame_id=2,
                                                                              prev_gray=cur_gray,
                                                                              prev_cfd=optflow_map,
                                                                              use_gpu=False)
jm_12138's avatar
jm_12138 已提交
76 77 78 79 80
        self.assertIsInstance(img_matting, np.ndarray)
        self.assertIsInstance(cur_gray, np.ndarray)
        self.assertIsInstance(optflow_map, np.ndarray)

    def test_video_stream_segment2(self):
jm_12138's avatar
jm_12138 已提交
81 82 83 84 85
        img_matting, cur_gray, optflow_map = self.module.video_stream_segment(frame_org=cv2.imread('tests/test.jpg'),
                                                                              frame_id=1,
                                                                              prev_gray=None,
                                                                              prev_cfd=None,
                                                                              use_gpu=True)
jm_12138's avatar
jm_12138 已提交
86 87 88
        self.assertIsInstance(img_matting, np.ndarray)
        self.assertIsInstance(cur_gray, np.ndarray)
        self.assertIsInstance(optflow_map, np.ndarray)
jm_12138's avatar
jm_12138 已提交
89 90 91 92 93
        img_matting, cur_gray, optflow_map = self.module.video_stream_segment(frame_org=cv2.imread('tests/test.jpg'),
                                                                              frame_id=2,
                                                                              prev_gray=cur_gray,
                                                                              prev_cfd=optflow_map,
                                                                              use_gpu=True)
jm_12138's avatar
jm_12138 已提交
94 95 96 97 98
        self.assertIsInstance(img_matting, np.ndarray)
        self.assertIsInstance(cur_gray, np.ndarray)
        self.assertIsInstance(optflow_map, np.ndarray)

    def test_video_segment1(self):
jm_12138's avatar
jm_12138 已提交
99
        self.module.video_segment(video_path="tests/test.avi", use_gpu=False, save_dir='humanseg_lite_video_result')
jm_12138's avatar
jm_12138 已提交
100 101 102 103 104 105 106 107 108 109

    def test_save_inference_model(self):
        self.module.save_inference_model('./inference/model')

        self.assertTrue(os.path.exists('./inference/model.pdmodel'))
        self.assertTrue(os.path.exists('./inference/model.pdiparams'))


if __name__ == "__main__":
    unittest.main()