Skip to content

Commit

Permalink
first commit. Code for doing low cost image analysis with Raspberry Pi.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmopencell committed Jul 19, 2019
0 parents commit 66aca6b
Show file tree
Hide file tree
Showing 33 changed files with 154 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is a repo for doing some basic colout analysis with a Raspberry Pi and some extra webcams.


28 changes: 28 additions & 0 deletions binary.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
2019-07-09-112544, 0
2019-07-09-112545, 0
2019-07-09-113009, 0
2019-07-09-113011, 0
2019-07-09-113052, 0
2019-07-09-113053, 0
2019-07-09-113904, 0
2019-07-09-113906, 0
2019-07-09-114437, 0
2019-07-09-114438, 0
2019-07-09-114536, 0
2019-07-09-114538, 0
2019-07-09-114705, 0
2019-07-09-114707, 0
2019-07-09-114830, 0
2019-07-09-114832, 0
2019-07-09-115134, 0
2019-07-09-115136, 0
2019-07-09-115358, 0
2019-07-09-115359, 0
2019-07-09-115603, 0
2019-07-09-115604, 0
2019-07-09-115741, 0
2019-07-09-115743, 0
2019-07-09-120010, 1
2019-07-09-120012, 1
2019-07-09-120844, 0
2019-07-09-120845, 0
Binary file added cam1/.jpg
Binary file not shown.
Binary file added cam1/2019-07-09-113052.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam1/2019-07-09-113904.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam1/2019-07-09-114437.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam1/2019-07-09-114536.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam1/2019-07-09-114705.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam1/2019-07-09-114830.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam1/2019-07-09-115134.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam1/2019-07-09-115358.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam1/2019-07-09-115603.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam1/2019-07-09-115741.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam1/2019-07-09-120010.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam1/2019-07-09-120844.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam1/timelapse.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam2/2019-07-09-113053.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam2/2019-07-09-113906.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam2/2019-07-09-114438.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam2/2019-07-09-114538.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam2/2019-07-09-114707.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam2/2019-07-09-114832.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam2/2019-07-09-115136.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam2/2019-07-09-115359.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam2/2019-07-09-115604.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam2/2019-07-09-115743.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam2/2019-07-09-120012.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam2/2019-07-09-120845.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cam2/timelapse.gif
52 changes: 52 additions & 0 deletions color_analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# From https://www.pyimagesearch.com/2014/08/04/opencv-python-color-detection/
# import the necessary packages
import sys
sys.path.append('/home/pi/.local/lib/python3.5/site-packages')
import numpy as np
import argparse
import cv2
import datetime
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help = "path to the image")
args = vars(ap.parse_args())
# load the image
image = cv2.imread(args["image"])
# define the list of boundaries
boundaries = [
([0, 0, 10], [50, 50, 255])
]
# loop over the boundaries
for (lower, upper) in boundaries:
# create NumPy arrays from the boundaries
lower = np.array(lower, dtype = "uint8")
upper = np.array(upper, dtype = "uint8")
# find the colors within the specified boundaries and apply
# the mask
mask = cv2.inRange(image, lower, upper)
output = cv2.bitwise_and(image, image, mask = mask)

# Save the images
timenow = datetime.datetime.now()
filename1 = timenow.strftime("%Y-%m-%d-%H%M%S")
cv2.imwrite(filename1+".jpg", output) # Or np.hstack([image,output])
#cv2.waitKey(0)
output_max = np.max(output)
output_min = np.min(output)
output_norm = (output - output_min)/(output_max - output_min)
total_pixels = output_norm.shape[0]*output_norm.shape[1]
abovethreshold_pixels = (output_norm > 1).sum()
norm_abovethreshold = (abovethreshold_pixels/total_pixels)*100
print(total_pixels, abovethreshold_pixels, norm_abovethreshold )
if norm_abovethreshold > 10:
print (filename1, "Red in frame")
f = open('binary.csv','a')
f.write(filename1+', 1 \n') #Give your csv text here.
## Python will convert \n to os.linesep
f.close()
elif norm_abovethreshold <= 10:
print (filename1, "NO Red in frame")
f = open('binary.csv','a')
f.write(filename1+', 0 \n') #Give your csv text here.
## Python will convert \n to os.linesep
f.close()
13 changes: 13 additions & 0 deletions image_analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from clarifai.rest import ClarifaiApp

image_directory = '/home/pi/webcam/cam1/'

app = ClarifaiApp(api_key='40d74b5ce16b47d794ee6b6955f7f930')
model = app.public_models.general_model
##response = model.predict_by_url('https://samples.clarifai.com/metro-north.jpg')
#response = model.predict_by_url('https://gb.gilson.com/pub/media/catalog/product/cache/c687aa7517cf01e65c009f6943c2b1e9/F/1/F123603_MAIN_Pipetman-Classic-Single-Channel-Pipette-P5000_8.jpg')
response = model.predict_by_filename(image_directory+'2019-06-05_140044.jpg')

concepts = response['outputs'][0]['data']['concepts']
for concept in concepts:
print(concept['name'], concept['value'])
32 changes: 32 additions & 0 deletions image_segment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
DATE=$(date +"%Y-%m-%d_%H%M%S")

# This handy command will simply delay the next command by a specified interval
echo "sleeping"
sleep 1
echo "done sleeping"

# This takes two photos and stores them in respective folders for each cam
sudo fswebcam -d /dev/video0 -r 352x288 --no-banner /home/pi/webcam/cam1/$DATE.jpg
sudo fswebcam -d /dev/video2 -r 352x288 --no-banner /home/pi/webcam/cam2/$DATE.jpg

python /home/pi/webcam/color_analysis.py --image /home/pi/webcam/cam1/$DATE.jpg
sudo rm /home/pi/webcam/cam1/$DATE.jpg
mv /home/pi/webcam/2019* /home/pi/webcam/cam1/
python /home/pi/webcam/color_analysis.py --image /home/pi/webcam/cam2/$DATE.jpg
sudo rm /home/pi/webcam/cam2/$DATE.jpg
mv /home/pi/webcam/2019*.jpg /home/pi/webcam/cam2/
# This command places another image (with time banner) into a folder that streams to the web note the file location “../../var/www”
sudo fswebcam -d /dev/video0 -r 352x288 /var/www/webcam/cam1/timelapse.jpg
sudo fswebcam -d /dev/video2 -r 352x288 /var/www/webcam/cam2/timelapse.jpg

# Now we use the convert command (see imagemagik) to creat gifs
# The -delay gives the interval between picture changes and -loop sets it to repeat
sudo convert -delay 20 -loop 0 /home/pi/webcam/cam1/*.jpg /home/pi/webcam/cam1/timelapse.gif
sudo convert -delay 20 -loop 0 /home/pi/webcam/cam2/*.jpg /home/pi/webcam/cam2/timelapse.gif
echo "creating gif"
# Now we copy these gifs to the webstream folder “../../var/www”
sudo cp /home/pi/webcam/cam1/timelapse.gif /var/www/webcam/cam1/timelapse.gif
sudo cp /home/pi/webcam/cam2/timelapse.gif /var/www/webcam/cam2/timelapse.gif
echo "copying gif to www folder!"
echo "DONE"
26 changes: 26 additions & 0 deletions webcam.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
DATE=$(date +"%Y-%m-%d_%H%M%S")

# This handy command will simply delay the next command by a specified interval
echo "sleeping"
sleep 1
echo "done sleeping"

# This takes two photos and stores them in respective folders for each cam
sudo fswebcam -d /dev/video0 -r 352x288 --no-banner /home/pi/webcam/cam1/$DATE.jpg
sudo fswebcam -d /dev/video2 -r 352x288 --no-banner /home/pi/webcam/cam2/$DATE.jpg

# This command places another image (with time banner) into a folder that streams to the web note the file location “../../var/www”
sudo fswebcam -d /dev/video0 -r 352x288 /var/www/webcam/cam1/timelapse.jpg
sudo fswebcam -d /dev/video2 -r 352x288 /var/www/webcam/cam2/timelapse.jpg

# Now we use the convert command (see imagemagik) to creat gifs
# The -delay gives the interval between picture changes and -loop sets it to repeat
sudo convert -delay 20 -loop 0 /home/pi/webcam/cam1/*.jpg /home/pi/webcam/cam1/timelapse.gif
sudo convert -delay 20 -loop 0 /home/pi/webcam/cam2/*.jpg /home/pi/webcam/cam2/timelapse.gif
echo "creating gif"
# Now we copy these gifs to the webstream folder “../../var/www”
sudo cp /home/pi/webcam/cam1/timelapse.gif /var/www/webcam/cam1/timelapse.gif
sudo cp /home/pi/webcam/cam2/timelapse.gif /var/www/webcam/cam2/timelapse.gif
echo "copying gif to www folder!"
echo "DONE"

0 comments on commit 66aca6b

Please sign in to comment.