NMPhap commited on
Commit
82df0e1
2 Parent(s): dd73539 c5fcb51

Merge pull request #5 from HunyyDev/NMPhap/testing

Browse files
Files changed (1) hide show
  1. app/routers/test_image.py +68 -0
app/routers/test_image.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import mmcv
3
+ import numpy as np
4
+ from app.routers.image import inferenceImage
5
+ import pytest
6
+ import pytest
7
+ from fastapi.testclient import TestClient
8
+ from app.main import app
9
+
10
+ @pytest.fixture
11
+ def client():
12
+ client = TestClient(app, "http://0.0.0.0:3000")
13
+ yield client
14
+ class TestImageRoute():
15
+ img = mmcv.imread('demo.jpg')
16
+ url = "http://0.0.0.0:3000/image"
17
+ def test_inferenceImage(self):
18
+ bboxes, labels = inferenceImage(mmcv.imread('demo.jpg'), 0.3, True)
19
+ assert len(bboxes.tolist()) > 0 and len(labels.tolist()) > 0 and len(bboxes.tolist()) == len(labels.tolist())
20
+ result = inferenceImage(self.img, 0.3)
21
+ assert type(result) is np.ndarray and result.shape == self.img.shape
22
+ def test_ImageAPI(self, client):
23
+ payload = {}
24
+ files=[
25
+ ('file',('demo.jpg',open('demo.jpg','rb'),'image/jpeg'))
26
+ ]
27
+ headers = {
28
+ 'accept': 'application/json'
29
+ }
30
+ response = client.request("POST", "image", headers=headers, data=payload, files=files)
31
+ result = mmcv.imfrombytes(response.read())
32
+ assert response.status_code == 200 and result.shape == self.img.shape
33
+ def test_ImageAPI_one_channel_array(self, client):
34
+ np.zeros((1,640,640)).dump("one_channel.jpg")
35
+ payload = {}
36
+ files=[
37
+ ('file', ("demo.jpg",open("one_channel.jpg", "rb"),'image/jpeg'))
38
+ ]
39
+ headers = {
40
+ 'accept': 'application/json'
41
+ }
42
+ response = client.request("POST", "image", headers=headers, data=payload, files=files)
43
+ assert response.status_code != 200
44
+ def test_ImageAPIWithThresHold(self, client):
45
+ payload = {}
46
+ files=[
47
+ ('file',('demo.jpg',open('demo.jpg','rb'),'image/jpeg'))
48
+ ]
49
+ headers = {
50
+ 'accept': 'application/json'
51
+ }
52
+ response = client.request("POST", "image?threshold=1&raw=True", headers=headers, data=payload, files=files)
53
+ thresHold = 0.4
54
+ assert response.status_code == 200 # The result with threshold equal 0 is 0
55
+ # No detected object has 100% accuracy
56
+ assert len(response.json()["labels"]) == 0
57
+ payload = {}
58
+ files=[
59
+ ('file',('demo.jpg',open('demo.jpg','rb'),'image/jpeg'))
60
+ ]
61
+ headers = {
62
+ 'accept': 'application/json'
63
+ }
64
+ response = client.request("POST", "image?threshold=" + str(thresHold) + "&raw=True", headers=headers, data=payload, files=files)
65
+ assert response.status_code == 200
66
+ for bbox in response.json()['bboxes']:
67
+ assert bbox[4] >= thresHold
68
+