You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This updates the raspberry pi tutorial with the latest `picamera2` and
Raspberry Pi 5.
Test plan:
manually ran instructions + code
CI
---------
Co-authored-by: Nikita Shulga <[email protected]>
Once that boots and you complete the initial setup you'll need to edit the ``/boot/config.txt`` file to enable the camera.
48
+
Raspberry Pi 4 Config
49
+
~~~~~~~~~~~~~~~~~~~~~~~~
50
+
51
+
If you're using a Raspberry Pi 4, you'll need some additional config changes. These changes are not required on Raspberry Pi 5.
52
+
53
+
Once the OS boots and you complete the initial setup you'll need to edit the ``/boot/config.txt`` file to enable the camera.
49
54
50
55
.. code:: toml
51
56
@@ -55,21 +60,17 @@ Once that boots and you complete the initial setup you'll need to edit the ``/bo
55
60
# This needs to be at least 128M for the camera processing, if it's bigger you can just leave it as is.
56
61
gpu_mem=128
57
62
58
-
# You need to commment/remove the existing camera_auto_detect line since this causes issues with OpenCV/V4L2 capture.
59
-
#camera_auto_detect=1
60
-
61
-
And then reboot. After you reboot the video4linux2 device ``/dev/video0`` should exist.
63
+
And then reboot.
62
64
63
-
Installing PyTorch and OpenCV
64
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65
+
Installing PyTorch and picamera2
66
+
-------------------------------
65
67
66
68
PyTorch and all the other libraries we need have ARM 64-bit/aarch64 variants so you can just install them via pip and have it work like any other Linux system.
@@ -84,41 +85,49 @@ We can now check that everything installed correctly:
84
85
85
86
86
87
Video Capture
87
-
~~~~~~~~~~~~~~
88
+
-------------------
88
89
89
-
For video capture we're going to be using OpenCV to stream the video frames
90
-
instead of the more common ``picamera``. `picamera` isn't available on 64-bit
91
-
Raspberry Pi OS and it's much slower than OpenCV. OpenCV directly accesses the
92
-
``/dev/video0`` device to grab frames.
90
+
Test the camera is working first, by running ``libcamera-hello`` in a terminal.
91
+
92
+
For video capture we're going to be using picamera2 to capture the video frames.
93
93
94
94
The model we're using (MobileNetV2) takes in image sizes of ``224x224`` so we
95
-
can request that directly from OpenCV at 36fps. We're targeting 30fps for the
95
+
can request that directly from picamera2 at 36fps. We're targeting 30fps for the
96
96
model but we request a slightly higher framerate than that so there's always
97
97
enough frames.
98
98
99
99
.. code:: python
100
100
101
-
import cv2
102
-
fromPILimport Image
101
+
from picamera2 import Picamera2
102
+
103
+
picam2 = Picamera2()
104
+
105
+
# print available sensor modes
106
+
print(picam2.sensor_modes)
103
107
104
-
cap = cv2.VideoCapture(0)
105
-
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 224)
106
-
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 224)
107
-
cap.set(cv2.CAP_PROP_FPS, 36)
108
+
config = picam2.create_still_configuration(main={
109
+
"size": (224, 224),
110
+
"format": "BGR888",
111
+
}, display="main")
112
+
picam2.configure(config)
113
+
picam2.set_controls({"FrameRate": 36})
114
+
picam2.start()
108
115
109
-
OpenCV returns a ``numpy`` array in BGR so we need to read and do a bit of
110
-
shuffling to get it into the expected RGB format.
116
+
To capture the frames we can call ``capture_image`` to return a ``PIL.Image``
117
+
object that we can use with PyTorch.
111
118
112
119
.. code:: python
113
120
114
-
ret, image = cap.read()
115
-
# convert opencv output from BGR to RGB
116
-
image = image[:, :, [2, 1, 0]]
121
+
# read frame
122
+
image = picam2.capture_image("main")
123
+
124
+
# show frame for testing
125
+
image.show()
117
126
118
127
This data reading and processing takes about ``3.5 ms``.
119
128
120
129
Image Preprocessing
121
-
~~~~~~~~~~~~~~~~~~~~
130
+
----------------------
122
131
123
132
We need to take the frames and transform them into the format the model expects. This is the same processing as you would do on any machine with the standard torchvision transforms.
124
133
@@ -139,7 +148,7 @@ We need to take the frames and transform them into the format the model expects.
139
148
input_batch = input_tensor.unsqueeze(0)
140
149
141
150
Model Choices
142
-
~~~~~~~~~~~~~~~
151
+
----------------
143
152
144
153
There's a number of models you can choose from to use with different performance
145
154
characteristics. Not all models provide a ``qnnpack`` pretrained variant so for
@@ -178,7 +187,7 @@ Raspberry Pi 4 Benchmark Results:
0 commit comments