Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash

is_raspberry_pi() {
if grep -q "Raspberry" /proc/cpuinfo || grep -q "BCM" /proc/cpuinfo; then
return 0 # Is raspberry Pi
else
return 1
fi
}

if is_raspberry_pi; then
echo "Raspberry Pi detected. Running Raspberry-specific script..."

echo "Update packages"
sudo apt-get update

echo "Installing libopencv-dev and libatlas-base-dev..."
sudo apt-get install -y libopencv-dev libatlas-base-dev

if ! command -v pip3 &> /dev/null; then
echo "pip3 not found. Installing pip3..."
sudo apt-get install -y python3-pip
fi

echo "Installing Pillow, numpy, scipy, matplotlib..."
pip3 install Pillow numpy scipy matplotlib

echo "Installing opencv-python and opencv-contrib-python..."
pip3 install opencv-python opencv-contrib-python

echo "Installation of dependencies completed for Raspberry Pi!"

else
echo "Non-Raspberry Pi system detected. Running standard script..."

echo "Updating package list..."
sudo apt-get update

echo "Installing libopencv-dev and libatlas-base-dev..."
sudo apt-get install -y libopencv-dev libatlas-base-dev

if ! command -v pip3 &> /dev/null; then
echo "pip3 not found. Installing pip3..."
sudo apt-get install -y python3-pip
fi

if ! command -v virtualenv &> /dev/null; then
echo "virtualenv not found. Installing virtualenv..."
pip3 install virtualenv
fi

echo "Creating a virtual environment..."
virtualenv venv

echo "Activating virtual environment..."
source venv/bin/activate

echo "Installing dependencies: Pillow, numpy, scipy, matplotlib, opencv-python, and opencv-contrib-python..."
pip install Pillow numpy scipy matplotlib opencv-python opencv-contrib-python

if [ -f requirements.txt ]; then
echo "Installing dependencies from requirements.txt..."
pip install -r requirements.txt
else
echo "requirements.txt not found. Skipping installation from requirements file."
fi

echo "Installation of dependencies completed for non-Raspberry Pi system!"
fi
12 changes: 12 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
contourpy
cycler
fonttools
kiwisolver
matplotlib
numpy
opencv-contrib-python
opencv-python
packaging
pillow
pyparsing
python-dateutil
scipy
six
33 changes: 24 additions & 9 deletions src/camera-test/cv_camera_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,25 @@
# Twitter: @yfrobotics
# Website: https://www.yfrl.org
# ------------------------------------------------------------------------------
# Add src directory to the path
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import cv2
import time
import numpy as np
import time

from utils.picamera_utils import is_raspberry_camera, get_picamera

CAMERA_DEVICE_ID = 0
IMAGE_WIDTH = 320
IMAGE_HEIGHT = 240
IS_RASPI_CAMERA = is_raspberry_camera()
fps = 0

print("Using raspi camera: ", IS_RASPI_CAMERA)

def visualize_fps(image, fps: int):
if len(np.shape(image)) < 3:
Expand All @@ -39,15 +48,18 @@ def visualize_fps(image, fps: int):

return image


if __name__ == "__main__":
try:
# create video capture
cap = cv2.VideoCapture(CAMERA_DEVICE_ID)

# set resolution to 320x240 to reduce latency
cap.set(3, IMAGE_WIDTH)
cap.set(4, IMAGE_HEIGHT)

if IS_RASPI_CAMERA:
cap = get_picamera(IMAGE_WIDTH, IMAGE_HEIGHT)
cap.start()
else:
# create video capture
cap = cv2.VideoCapture(CAMERA_DEVICE_ID)
# set resolution to 320x240 to reduce latency
cap.set(3, IMAGE_WIDTH)
cap.set(4, IMAGE_HEIGHT)

# Loop to continuously get images
while True:
Expand All @@ -56,7 +68,10 @@ def visualize_fps(image, fps: int):
start_time = time.time()

# Read the frames from a camera
_, frame = cap.read()
if IS_RASPI_CAMERA:
frame = cap.capture_array()
else:
_, frame = cap.read()

# show image
cv2.imshow('frame', visualize_fps(frame, fps))
Expand All @@ -78,4 +93,4 @@ def visualize_fps(image, fps: int):
finally:
# Clean up and exit the program
cv2.destroyAllWindows()
cap.release()
cap.close() if IS_RASPI_CAMERA else cap.release()
64 changes: 64 additions & 0 deletions src/face-detection/pi_camera_face_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import cv2
import numpy as np
import time
from picamera2 import Picamera2


def visualize_fps(image, fps: int):
if len(np.shape(image)) < 3:
text_color = (255, 255, 255) # white
else:
text_color = (0, 255, 0) # green
row_size = 20 # pixels
left_margin = 24 # pixels

font_size = 1
font_thickness = 1

# Draw the FPS counter
fps_text = 'FPS = {:.1f}'.format(fps)
text_location = (left_margin, row_size)
cv2.putText(image, fps_text, text_location, cv2.FONT_HERSHEY_PLAIN,
font_size, text_color, font_thickness)

return image

# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# Initialize Picamera2 and configure the camera
picam2 = Picamera2()
picam2.configure(picam2.create_preview_configuration(main={"format": 'XRGB8888', "size": (640, 480)}))
picam2.start()
fps = 0

while True:
# ----------------------------------------------------------------------
# record start time
start_time = time.time()
# Read the frame
img = picam2.capture_array()
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw the rectangle around each face
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Display
cv2.imshow('img', visualize_fps(img, fps))
# ----------------------------------------------------------------------
# record end time
end_time = time.time()
# calculate FPS
seconds = end_time - start_time
fps = 1.0 / seconds
print("Estimated fps:{0:0.1f}".format(fps))
# Stop if escape key is pressed
k = cv2.waitKey(30) & 0xff
if k==27:
break

# Release the VideoCapture object
picam2.close()
cv2.destroyAllWindows()
125 changes: 125 additions & 0 deletions src/motion-detection/pi_camera_cv_motion_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/usr/bin/python3

# ------------------------------------------------------------------------------
# Detect any motion in the frame.
# ------------------------------------------------------------------------------
# automaticdai
# YF Robotics Labrotary
# Instagram: yfrobotics
# Twitter: @yfrobotics
# Website: https://www.yfrl.org
# --------------------------------------------#!/usr/bin/python3

# ------------------------------------------------------------------------------
# Detect any motion in the frame.
# ------------------------------------------------------------------------------
# automaticdai
# YF Robotics Labrotary
# Instagram: yfrobotics
# Twitter: @yfrobotics
# Website: https://www.yfrl.org
# ------------------------------------------------------------------------------

import cv2
import time
import numpy as np

from picamera2 import Picamera2

MOTION_BLUR = True

cnt_frame = 0
fps = 0

# Initialize Picamera2 and configure the camera
picam2 = Picamera2()
picam2.configure(picam2.create_preview_configuration(main={"format": 'XRGB8888', "size": (640, 480)}))
picam2.start()

def mse(image_a, image_b):
# the 'Mean Squared Error' between the two images is the
# sum of the squared difference between the two images;
# NOTE: the two images must have the same dimension
err = np.sum((image_a.astype("float") - image_b.astype("float")) ** 2)
err /= float(image_a.shape[0] * image_a.shape[1])

# return the MSE, the lower the error, the more "similar"
# the two images are
return err


def visualize_fps(image, fps: int):
if len(np.shape(image)) < 3:
text_color = (255, 255, 255) # white
else:
text_color = (0, 255, 0) # green
row_size = 20 # pixels
left_margin = 24 # pixels

font_size = 1
font_thickness = 1

# Draw the FPS counter
fps_text = 'FPS = {:.1f}'.format(fps)
text_location = (left_margin, row_size)
cv2.putText(image, fps_text, text_location, cv2.FONT_HERSHEY_PLAIN,
font_size, text_color, font_thickness)

return image


if __name__ == "__main__":
try:

while True:
# ----------------------------------------------------------------------
# record start time
start_time = time.time()
# ----------------------------------------------------------------------
# Read the frames from a camera
frame_raw = picam2.capture_array()

if MOTION_BLUR:
# Denoise the frame
frame = cv2.GaussianBlur(frame_raw, (3,3),0)
else:
frame = frame_raw

# Convert to gray image
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Find edges
edges = cv2.Canny(frame_gray,100,200)

# Show the original and processed image
cv2.imshow('gray', visualize_fps(frame_gray, fps))
cv2.imshow('edge', visualize_fps(edges, fps))

# Calculate MSE
if cnt_frame > 0:
if mse(frame_gray, frame_gray_p) > 100:
print('Frame{0}: Motion Detected!'.format(cnt_frame))

# ----------------------------------------------------------------------
# record end time
end_time = time.time()

# calculate FPS
seconds = end_time - start_time
fps = 1.0 / seconds
print("Estimated fps:{0:0.1f}".format(fps));

cnt_frame = cnt_frame + 1
edges_p = edges
frame_gray_p = frame_gray
# ----------------------------------------------------------------------

# if key pressed is 'Esc' then exit the loop
if cv2.waitKey(1)== 27:
break
except Exception as e:
print(e)
finally:
# Clean up and exit the program
cv2.destroyAllWindows()
picam2.close()
Loading