-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathinfer_rt.py
180 lines (134 loc) · 5.2 KB
/
infer_rt.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import torch
from torchvision.transforms import transforms
import numpy as np
from torch.autograd import Variable
from torchvision.models import squeezenet1_1
from io import open
import os
import sys
from PIL import Image
from train import Net
import cv2
from time import sleep
import requests
import json
import configparser
config = configparser.ConfigParser()
config.read(os.path.abspath(os.path.dirname(sys.argv[0])) + '/params.cfg')
hue_user = config.get('general', 'hue_user')
hue_bridge = config.get('general', 'hue_bridge')
light_id = config.get('general', 'light_id')
music_host = config.get('general', 'music_host')
img_width = 300
img_height = 300
trained_model_objects = "objects_223_9817-3576.model"
trained_model_gestures = "gestures_173_12311-6836.model"
num_classes_objects = 3
num_classes_gestures = 2
objs = []
objs.append("Google")
objs.append("Lamp")
objs.append("Nothing")
gestures = []
gestures.append("Wave")
gestures.append("Nothing")
# Load the saved models.
checkpoint_objects = torch.load(trained_model_objects)
model_objects = Net(num_classes=num_classes_objects)
model_objects.load_state_dict(checkpoint_objects)
model_objects.eval()
checkpoint_gestures = torch.load(trained_model_gestures)
model_gestures = Net(num_classes=num_classes_gestures)
model_gestures.load_state_dict(checkpoint_gestures)
model_gestures.eval()
transformation = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
def predict_image_class(image, classifier_type):
# Preprocess the image.
image_tensor = transformation(image).float()
# Add an extra batch dimension since pytorch treats all images as batches.
image_tensor = image_tensor.unsqueeze_(0)
image_tensor.cuda()
# Turn the input into a Variable.
input = Variable(image_tensor)
# Predict the class of the image.
if classifier_type == "objects":
output = model_objects(input)
elif classifier_type == "gestures":
output = model_gestures(input)
index = output.data.numpy().argmax()
score = output[0, index].item()
return index, score
def gstreamer_pipeline (capture_width=3280, capture_height=2464, display_width=img_width, display_height=img_height, framerate=21, flip_method=0) :
return ('nvarguscamerasrc ! '
'video/x-raw(memory:NVMM), '
'width=(int)%d, height=(int)%d, '
'format=(string)NV12, framerate=(fraction)%d/1 ! '
'nvvidconv flip-method=%d ! '
'video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! '
'videoconvert ! '
'video/x-raw, format=(string)BGR ! appsink' % (capture_width,capture_height,framerate,flip_method,display_width,display_height))
def toggle_lamp():
print("TOGGLE LAMP")
# Check to see if light is already on.
light_state = "off"
req = requests.get('http://' + hue_bridge + '/api/' + hue_user + '/lights')
json_data = req.json()
if json_data[light_id]["state"]["on"]:
light_state = "on"
# Prepare and send request to toggle light.
if light_state == "off":
json_data = json.dumps({'on': True})
else:
json_data = json.dumps({'on': False})
req = requests.put('http://' + hue_bridge + '/api/' + hue_user + '/lights/' + light_id + '/state', data=json_data)
print(req.text)
def toggle_music():
print("TOGGLE MUSIC")
requests.get("http://{}/toggle_play".format(music_host))
def reset_lookback():
return [-1, -1, -1]
def push_lookback(recent, index):
recent[1:] = recent[0:2]
recent[0] = index
return recent
def lookback_contains(recent, value):
for i in range(len(recent)):
if recent[i] == value:
return True
return False
def main():
cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
# Remember recently detected objects and gestures.
recent_objects = reset_lookback()
recent_gestures = reset_lookback()
if cap.isOpened():
while True:
ret_val, img_in = cap.read()
cv2.imwrite("out.jpg", img_in)
img = Image.open('out.jpg')
index_objects, score_objects = predict_image_class(img, "objects")
index_gestures, score_gestures = predict_image_class(img, "gestures")
# print("Object Class: ", objs[index_objects], index_objects)
# print("Object Score: ", score_objects)
# print("Gestures Class: ", gestures[index_gestures], index_gestures)
# print("Gestures Score: ", score_gestures)
recent_objects = push_lookback(recent_objects, index_objects)
recent_gestures = push_lookback(recent_gestures, index_gestures)
# Lamp, Wave
if lookback_contains(recent_objects, 1) and lookback_contains(recent_gestures, 0):
recent_objects = reset_lookback()
recent_gestures = reset_lookback()
toggle_lamp()
# Google, Wave
elif lookback_contains(recent_objects, 0) and lookback_contains(recent_gestures, 0):
recent_objects = reset_lookback()
recent_gestures = reset_lookback()
toggle_music()
cap.release()
else:
print('Unable to open camera.')
if __name__ == "__main__":
main()