forked from PaddlePaddle/PaddleHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
133 lines (109 loc) · 3.85 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import shutil
import unittest
import cv2
import requests
import paddlehub as hub
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
class TestHubModule(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
img_url = 'https://unsplash.com/photos/iFgRcqHznqg/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8MXx8ZmFjZXxlbnwwfHx8fDE2NjE5ODAyMTc&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)
cls.module = hub.Module(name="pyramidbox_lite_server")
@classmethod
def tearDownClass(cls) -> None:
shutil.rmtree('tests')
shutil.rmtree('inference')
shutil.rmtree('detection_result')
def test_face_detection1(self):
results = self.module.face_detection(
paths=['tests/test.jpg'],
use_gpu=False,
visualization=False
)
bbox = results[0]['data'][0]
confidence = bbox['confidence']
left = bbox['left']
right = bbox['right']
top = bbox['top']
bottom = bbox['bottom']
self.assertTrue(confidence > 0.5)
self.assertTrue(0 < left < 2000)
self.assertTrue(0 < right < 2000)
self.assertTrue(0 < top < 2000)
self.assertTrue(0 < bottom < 2000)
def test_face_detection2(self):
results = self.module.face_detection(
images=[cv2.imread('tests/test.jpg')],
use_gpu=False,
visualization=False
)
bbox = results[0]['data'][0]
confidence = bbox['confidence']
left = bbox['left']
right = bbox['right']
top = bbox['top']
bottom = bbox['bottom']
self.assertTrue(confidence > 0.5)
self.assertTrue(0 < left < 2000)
self.assertTrue(0 < right < 2000)
self.assertTrue(0 < top < 2000)
self.assertTrue(0 < bottom < 2000)
def test_face_detection3(self):
results = self.module.face_detection(
images=[cv2.imread('tests/test.jpg')],
use_gpu=False,
visualization=True
)
bbox = results[0]['data'][0]
confidence = bbox['confidence']
left = bbox['left']
right = bbox['right']
top = bbox['top']
bottom = bbox['bottom']
self.assertTrue(confidence > 0.5)
self.assertTrue(0 < left < 2000)
self.assertTrue(0 < right < 2000)
self.assertTrue(0 < top < 2000)
self.assertTrue(0 < bottom < 2000)
def test_face_detection4(self):
results = self.module.face_detection(
images=[cv2.imread('tests/test.jpg')],
use_gpu=True,
visualization=False
)
bbox = results[0]['data'][0]
confidence = bbox['confidence']
left = bbox['left']
right = bbox['right']
top = bbox['top']
bottom = bbox['bottom']
self.assertTrue(confidence > 0.5)
self.assertTrue(0 < left < 2000)
self.assertTrue(0 < right < 2000)
self.assertTrue(0 < top < 2000)
self.assertTrue(0 < bottom < 2000)
def test_face_detection5(self):
self.assertRaises(
AssertionError,
self.module.face_detection,
paths=['no.jpg']
)
def test_face_detection6(self):
self.assertRaises(
AttributeError,
self.module.face_detection,
images=['test.jpg']
)
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()