-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.py
41 lines (28 loc) · 893 Bytes
/
utility.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
import numpy as np
import torch
import os
from torchvision import transforms
transformation = transforms.Compose(
[transforms.ToPILImage(), transforms.ToTensor()])
def normalize(image):
return transforms.Normalize(mean=[0.473408, 0.44432889, 0.42011778], std=[0.23041105, 0.22339764, 0.22698703])(
transformation(image)
)
def preprocess_image(image):
image = normalize(image)
return torch.unsqueeze(image, dim=0)
def inference(frame, model):
current_path = os.path.dirname(os.path.abspath(__file__))
w, h, _ = frame.shape
import matplotlib.pyplot as plt
image = preprocess_image(frame)
with torch.no_grad():
prediction = model(image, (w, h))
prediction = (
torch.argmax(prediction["output"][0], dim=0)
.cpu()
.squeeze(dim=0)
.numpy()
.astype(np.uint8)
)
return prediction