-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpi_inference.py
50 lines (42 loc) · 1.23 KB
/
rpi_inference.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
#!/usr/bin/env python3
import torch
import numpy as np
import cv2
import gpiod
# Get GPIO pins for LED's: 23, 24 are pins 16, 18
leds = gpiod.Chip('gpiochip0').get_lines([23,24])
leds.request(
consumer='pi_ai',
type=gpiod.LINE_REQ_DIR_OUT,
default_vals=[1,1]
)
# Set up camera capture
camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 224)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 224)
# Turn off LEDs
leds.set_values([0,0])
# Load model
torch.backends.quantized.engine = 'qnnpack'
model = torch.jit.load('sfdc_tutorial_classifier.pth')
torch.no_grad()
def opencv_image_to_tensor(img):
# Convert BGR to RGB and add batch channel
img = img[None,:,:,[2,1,0]]
# Move the color channel to dim 1
img = img.transpose(0,3,1,2)
# Convert to a torch tensor
return torch.tensor(img)
while True:
# Get an image from the camera
ret, image = camera.read()
assert ret, "Could not read frame"
# Process the image through the model
inputs = opencv_image_to_tensor(image)
outputs = model(inputs)
prediction = outputs[0].max()[1].item()
# Change LED's based on our prediction
if prediction==0:
leds.set_values([1,0])
elif prediction==1:
leds.set_values([0,1])