File size: 1,657 Bytes
46a598e
 
 
 
278b1cd
 
 
46a598e
 
 
 
 
 
 
278b1cd
 
46a598e
 
 
 
 
278b1cd
46a598e
 
 
278b1cd
46a598e
 
 
 
 
 
 
278b1cd
46a598e
278b1cd
 
46a598e
278b1cd
 
46a598e
 
 
 
278b1cd
46a598e
278b1cd
46a598e
 
 
278b1cd
46a598e
 
 
 
 
 
 
 
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
import sys
import cv2
import numpy

sys.dont_write_bytecode = True

# Source
from helper import onnxSessionBuild

pathModel = "./PP-LCNet_x1_0_table_cls/"

imageSizeShort = 256
imageSizeCrop = 224

meanList = numpy.array([0.485, 0.456, 0.406], dtype=numpy.float32)
standardList = numpy.array([0.229, 0.224, 0.225], dtype=numpy.float32)

labelList = ["wired", "wireless"]

onnxSession = onnxSessionBuild(f"{pathModel}onnx/pp-lcNet_x1_0_table_cls.onnx")

def imageResize(imageRgb):
    imageHeight, imageWidth = imageRgb.shape[0:2]

    ratio = imageSizeShort / float(min(imageHeight, imageWidth))

    imageResized = cv2.resize(imageRgb, (int(round(imageWidth * ratio)), int(round(imageHeight * ratio))))

    resizedHeight, resizedWidth = imageResized.shape[0:2]

    cropX = int(round((resizedWidth - imageSizeCrop) / 2))
    cropY = int(round((resizedHeight - imageSizeCrop) / 2))

    return imageResized[cropY:cropY + imageSizeCrop, cropX:cropX + imageSizeCrop]

def inference(imageRgb):
    imageCrop = imageResize(imageRgb)

    tensor = imageCrop.astype(numpy.float32) / 255.0
    tensor = (tensor - meanList) / standardList
    tensor = numpy.expand_dims(tensor.transpose((2, 0, 1)), axis=0).astype(numpy.float32)

    tensorOutputList = onnxSession.run(None, {"x": tensor})

    probabilityList = tensorOutputList[0][0]

    index = int(numpy.argmax(probabilityList))

    return {
        "label": labelList[index],
        "score": float(probabilityList[index])
    }

image = cv2.imread(sys.argv[1])
imageRgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

itemObject = inference(imageRgb)

print(f"{itemObject['score']:.6f} | {itemObject['label']}")