-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrhlf.py
77 lines (73 loc) · 2.59 KB
/
rhlf.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
from general import (
tqdm,
json,
torch,
relative_path,
os,
NeuralNet,
DEVICE,
IMAGE_SIZE,
Image,
torchvision,
)
def main() -> None:
"""
Run the model over all images.
:return: None
"""
# select the most recent model
model_name = sorted([i for i in os.listdir(relative_path('models')) if i.endswith('.pt')])[-1]
# load the model
model_weights = torch.load(relative_path(f'models/{model_name}'))
model = NeuralNet()
model.load_state_dict(model_weights)
model.to(DEVICE)
transform = torchvision.transforms.Compose([
torchvision.transforms.ToTensor()
])
# set model to evaluation mode
model.eval()
with torch.no_grad():
for file in tqdm(os.listdir(relative_path('data/full_images/frames'))):
if file.endswith('.png'):
image = Image.open(relative_path(f"data/full_images/frames/{file}"))
image_size = image.size
image = image.resize(IMAGE_SIZE, Image.Resampling.LANCZOS)
image = transform(image).to(DEVICE)
output = model(image).tolist()[0]
data = {
'top': {
'left': {
'x': output[0] * image_size[0],
'y': output[1] * image_size[1],
},
'right': {
'x': output[2] * image_size[0],
'y': output[3] * image_size[1],
},
},
'bottom': {
'left': {
'x': output[4] * image_size[0],
'y': output[5] * image_size[1],
},
'right': {
'x': output[6] * image_size[0],
'y': output[7] * image_size[1],
},
},
'curvature': {
'top': {
'x': output[8] * image_size[0],
'y': output[9] * image_size[1],
},
'bottom': {
'x': output[10] * image_size[0],
'y': output[11] * image_size[1],
},
},
}
with open(relative_path(f"data/full_images/rhlf_json/{file.split('.')[0]}.json"), 'w') as f:
json.dump(data, f, indent=2)
if __name__ == '__main__':
main()