Last active
May 24, 2021 22:12
-
-
Save kylemcdonald/9c8a2c60beebf36d2a7c344c730a8519 to your computer and use it in GitHub Desktop.
Build OpenCV for Raspberry Pi under Ubuntu 20.10 64-bit server, aand test v4l2 camera frame rate.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CV_VERSION=4.5.1 | |
cd ~ | |
sudo apt update | |
sudo apt-get install -y \ | |
build-essential \ | |
cmake \ | |
gfortran \ | |
libatlas-base-dev \ | |
libavcodec-dev \ | |
libavcodec-dev \ | |
libavformat-dev \ | |
libavformat-dev \ | |
libcairo2-dev \ | |
libeigen3-dev \ | |
libfontconfig1-dev \ | |
libgdk-pixbuf2.0-dev \ | |
libgtk-3-dev \ | |
libgtk2.0-dev \ | |
libgtk2.0-dev \ | |
libhdf5-103 \ | |
libhdf5-dev \ | |
libhdf5-serial-dev \ | |
libjasper-dev \ | |
libjpeg-dev \ | |
libpango1.0-dev \ | |
libpng-dev \ | |
libqt4-test \ | |
libqtgui4 \ | |
libqtwebkit4 \ | |
libswscale-dev \ | |
libswscale-dev \ | |
libtbb-dev \ | |
libtiff5-dev \ | |
libv4l-dev \ | |
libx264-dev \ | |
libxvidcore-dev \ | |
pkg-config \ | |
python3-dev \ | |
python3-pyqt5 \ | |
python3-pip \ | |
v4l-utils | |
pip3 install numpy | |
git clone --branch $CV_VERSION --depth 1 https://github.com/opencv/opencv.git | |
cd ~/opencv | |
mkdir build | |
cd build | |
cmake \ | |
-D CMAKE_BUILD_TYPE=RELEASE \ | |
-D CMAKE_INSTALL_PREFIX=/usr/local \ | |
-D ENABLE_NEON=ON \ | |
-D WITH_TBB=ON \ | |
-D ENABLE_VFPV3=ON \ | |
-D BUILD_TESTS=OFF \ | |
-D INSTALL_PYTHON_EXAMPLES=OFF \ | |
-D BUILD_OPENCV_PYTHON3=YES \ | |
-D OPENCV_ENABLE_NONFREE=ON \ | |
-D CMAKE_SHARED_LINKER_FLAGS=-latomic \ | |
-D BUILD_EXAMPLES=OFF .. | |
# on raspios 32-bit, add | |
# -D WITH_V4L=ON \ | |
# on raspios 32-bit | |
# sudo sed -i 's/CONF_SWAPSIZE=100/CONF_SWAPSIZE=2048/g' /etc/dphys-swapfile | |
# sudo /etc/init.d/dphys-swapfile stop | |
# sudo /etc/init.d/dphys-swapfile start | |
make -j`nproc` | |
sudo make install | |
# on raspios 32-bit | |
# sudo sed -i 's/CONF_SWAPSIZE=2048/CONF_SWAPSIZE=100/g' /etc/dphys-swapfile | |
# sudo /etc/init.d/dphys-swapfile stop | |
# sudo /etc/init.d/dphys-swapfile start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import time | |
import numpy as np | |
fourcc = 'MJPG' | |
width = 1920 | |
height = 1080 | |
fps = 30 | |
cap = cv2.VideoCapture(0, cv2.CAP_V4L2) | |
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*fourcc)) | |
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width) | |
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height) | |
cap.set(cv2.CAP_PROP_FPS, fps) | |
# cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1) # 3 is on, 1 is off | |
# cap.set(cv2.CAP_PROP_EXPOSURE, 100) | |
def capture(cap): | |
while True: | |
ret, frame = cap.read() | |
yield frame | |
def progress(itr, update_interval=1): | |
start_time = None | |
last_time = None | |
for i, x in enumerate(itr): | |
cur_time = time.time() | |
if start_time is None: | |
start_time = cur_time | |
last_time = cur_time | |
yield x | |
if cur_time - last_time > update_interval: | |
duration = cur_time - start_time | |
speed = (i + 1) / duration | |
print(round(speed, 1), 'fps') | |
last_time = cur_time | |
from utils.imutil import imwrite | |
try: | |
for i,e in enumerate(progress(capture(cap))): | |
# if i % 20 == 0: | |
# cv2.imwrite('out.jpg', e) | |
continue | |
except KeyboardInterrupt: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi when running this script I get the following error. Any idea what could cause this? I have Ubuntu 20.04: