-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera_test_model.py
74 lines (59 loc) · 2.23 KB
/
camera_test_model.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
from rich.console import Console
console = Console()
console.log("[green3]Program starting...")
with console.status("Loading libraries...",spinner="moon"):
import torch
import torch.nn as nn
from torchvision.transforms import Resize
from torchvision import models
console.log("[green3]Torch loaded")
# from libs import Car
import cv2
console.log("[green3]CV2 loaded")
# Model setup
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
with console.status("Started to load the model...",spinner="moon"):
model = models.resnet18()
model.fc = nn.Linear(model.fc.in_features,1)
model.load_state_dict(torch.load("./Models/3_0-07Steer 5E 42BS LR0-0001 gpu_dict.pth"))
# model.to(DEVICE)
model.eval()
console.log("[green3]Model loaded successfully!")
# Car setup
# CAR = Car.Car((32), (11, 13, 33, True), (31, 29, 33, True))
# CAR_SPEED = 87
# CAR_STEER = 0.5
# console.log("[green3]CAR initialized successfully!")
# Camera setup
camera = cv2.VideoCapture(0, cv2.CAP_V4L)
# Camera image settings
camera.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0.25)
camera.set(cv2.CAP_PROP_EXPOSURE, -4)
camera.set(cv2.CAP_PROP_FRAME_WIDTH,224)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT,224)
camera.set(cv2.CAP_PROP_BUFFERSIZE,1)
console.log("[green3]Camera initialized successfully!")
# for i in range(10):
# ret,frame = camera.retrieve(camera.grab())
DRIVING = True
with torch.no_grad():
while DRIVING:
# ********** IMAGE PROCCESSING ********** #
#ret, frame = camera.read()
ret,frame = camera.retrieve(camera.grab())
# console.log("Retrieved a frame")
# print(frame,ret)
input_img = torch.from_numpy(frame)
# input_img = input_img.to(DEVICE)
# console.log(input_img.shape)
input_img = input_img.transpose(0,2).transpose(1,2)
input_img.unsqueeze_(0)
input_img = Resize((244,244))(input_img)
input_img = input_img.float() / 255.0
# console.log("Image is preped. Shape: ",input_img.shape)
output = model(input_img)
# console.log(output.shape)
output = torch.reshape(output,(-1,))
# console.log(output.shape)
console.log(output)
# CAR.update_car(output.item(), CAR_SPEED)