This commit is contained in:
Matthew Frost 2023-11-07 12:38:19 +01:00
commit 4b0e8b49f6
13 changed files with 434 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

Binary file not shown.

BIN
blurred.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 476 KiB

BIN
clean.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1,023 KiB

BIN
clean1.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

129
dd.py Normal file
View file

@ -0,0 +1,129 @@
import numpy as np
import argparse
import cv2
import copy
import os
import time
#from gpiozero import Buzzer
## Define our config values
# What is our min dish count to alarm on?
file = "dirty.jpeg"
min_dishes = 2
# Define areas we want to ignore
# First value is the x range, second is the y range
ignore_list = ["339-345,257-260"]
# Set the GPIO our buzzer is connected to
#buzzer = Buzzer(21)
# Set how long we want to buzz
buzz_seconds = 180
# Set our timestamp
time_stamp = time.strftime("%Y%m%d%H%M%S")
# Set our circle detection variables
circle_sensitivity = 40 # Larger numbers increase false positives
min_rad = 30 # Tweak this if you're detecting circles that are too small
max_rad = 75 # Tweak if you're detecting circles that are too big (Ie: round sinks)
# Cropping the image allows us to only process areas of the image
# that should have images. Set our crop values
crop_left = 100
crop_right = 2000
crop_top = 0
crop_bottom = 2000
def should_ignore(ignore_list, x, y):
# Loop through our ignore_list and check for this x/y
ignore = False
for range in ignore_list:
x_range = range.split(',')[0]
y_range = range.split(',')[1]
x_min = int(x_range.split('-')[0])
x_max = int(x_range.split('-')[1])
y_min = int(y_range.split('-')[0])
y_max = int(y_range.split('-')[1])
if (x >= x_min and x <= x_max and y >= y_min and y <= y_max):
ignore = True
return ignore
def main():
print("Acquiring Image")
# Note: Larger images require more processing power and have more false positives
# os.system("raspistill -w 1024 -h 768 -o /var/www/html/images/process.jpg")
image_original = cv2.imread(file)
print("Cropping image to limit processing to just the sink")
image = image_original[crop_left:crop_right, crop_top:crop_bottom]
#image = image_original
print("Copying image")
output = copy.copy(image)
print("Blurring image")
blurred = cv2.GaussianBlur(image, (9, 9), 2, 2)
cv2.imwrite('blurred.jpg', blurred)
print("Converting to grey")
gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
cv2.imwrite('gray.jpg', gray)
print("Detecting circles in blurred and greyed image")
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20,
param1=100,
param2=circle_sensitivity,
minRadius=min_rad,
maxRadius=max_rad)
print("Checking if we found images")
if circles is not None:
dish_count = 0
print("Dishes Found!")
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
print("Tracing circle x:%s, y:%s, r:%s" % (x,y,r))
# draw the circle in the output image, then draw a rectangle
# corresponding to the center of the circle
cv2.circle(output, (x, y), r, (0, 255, 0), 4)
cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
# Check our ignore_list
if (should_ignore(ignore_list, x, y)):
print("Circle in ignore_list: Ignoring")
else:
dish_count += 1
print("Dish count:%s" % (str(dish_count)))
print("Writing detected image")
cv2.imwrite('/var/www/html/images/detected.jpg', output)
if dish_count >= min_dishes:
print("Starting Buzzer")
timeout_start = time.time()
while time.time() < timeout_start + buzz_seconds:
buzzer.on()
time.sleep(2)
buzzer.off()
time.sleep(1)
else:
print("No Dishes Found!")
if __name__ == "__main__":
main()

BIN
dirty copy.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 867 KiB

BIN
dirty.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 867 KiB

127
dishdectector.py Normal file
View file

@ -0,0 +1,127 @@
import numpy as np
import argparse
import cv2
import copy
import os
import time
from slackclient import SlackClient
from gpiozero import Buzzer
## Define our config values
# What is our min dish count to alarm on?
min_dishes = 2
# Define areas we want to ignore
# First value is the x range, second is the y range
ignore_list = ["339-345,257-260"]
# Set the GPIO our buzzer is connected to
buzzer = Buzzer(21)
# Set how long we want to buzz
buzz_seconds = 180
# Set our timestamp
time_stamp = time.strftime("%Y%m%d%H%M%S")
# Set our circle detection variables
circle_sensitivity = 40 # Larger numbers increase false positives
min_rad = 30 # Tweak this if you're detecting circles that are too small
max_rad = 75 # Tweak if you're detecting circles that are too big (Ie: round sinks)
# Cropping the image allows us to only process areas of the image
# that should have images. Set our crop values
crop_left = 0
crop_right = 360
crop_top = 150
crop_bottom = 850
def should_ignore(ignore_list, x, y):
# Loop through our ignore_list and check for this x/y
ignore = False
for range in ignore_list:
x_range = range.split(',')[0]
y_range = range.split(',')[1]
x_min = int(x_range.split('-')[0])
x_max = int(x_range.split('-')[1])
y_min = int(y_range.split('-')[0])
y_max = int(y_range.split('-')[1])
if (x >= x_min and x <= x_max and y >= y_min and y <= y_max):
ignore = True
return ignore
def main():
print("Acquiring Image")
# Note: Larger images require more processing power and have more false positives
os.system("raspistill -w 1024 -h 768 -o /var/www/html/images/process.jpg")
image_original = cv2.imread("/var/www/html/images/process.jpg")
print("Cropping image to limit processing to just the sink")
image = image_original[crop_left:crop_right, crop_top:crop_bottom]
print("Copying image")
output = copy.copy(image)
print("Blurring image")
blurred = cv2.GaussianBlur(image, (9, 9), 2, 2)
cv2.imwrite('/var/www/html/images/blurred.jpg', blurred)
print("Converting to grey")
gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
cv2.imwrite('/var/www/html/images/gray.jpg', gray)
print("Detecting circles in blurred and greyed image")
circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1, 20,
param1=100,
param2=circle_sensitivity,
minRadius=min_rad,
maxRadius=max_rad)
print("Checking if we found images")
if circles is not None:
dish_count = 0
print("Dishes Found!")
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
print("Tracing circle x:%s, y:%s, r:%s" % (x,y,r))
# draw the circle in the output image, then draw a rectangle
# corresponding to the center of the circle
cv2.circle(output, (x, y), r, (0, 255, 0), 4)
cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
# Check our ignore_list
if (should_ignore(ignore_list, x, y)):
print("Circle in ignore_list: Ignoring")
else:
dish_count += 1
print("Dish count:%s" % (str(dish_count)))
print("Writing detected image")
cv2.imwrite('/var/www/html/images/detected.jpg', output)
if dish_count >= min_dishes:
print("Starting Buzzer")
timeout_start = time.time()
while time.time() < timeout_start + buzz_seconds:
buzzer.on()
time.sleep(2)
buzzer.off()
time.sleep(1)
else:
print("No Dishes Found!")
if __name__ == "__main__":
main()

141
dishsnitch.py Normal file
View file

@ -0,0 +1,141 @@
import copy
import os
import time
import cv2
import numpy as np
#from playsound import playsound
#import pygame as pg
## Define our config values
# What is our min dish count to alarm on?
min_dishes = 1
# Define areas we want to ignore
# First value is the x range, second is the y range
ignore_list = ["339-345,257-260"]
# Set our timestamp
time_stamp = time.strftime("%Y%m%d%H%M%S")
# Set our circle detection variables
circle_sensitivity = 40 # Larger numbers increase false positives
#circle_sensitivity = 60 # Larger numbers increase false positives
min_rad = 30 # Tweak this if you're detecting circles that are too small
max_rad = 75 # Tweak if you're detecting circles that are too big (Ie: round sinks)
# Cropping the image allows us to only process areas of the image
# that should have images. Set our crop values
crop_left = 100
crop_right = 2000
crop_top = 0
crop_bottom = 2000
def should_ignore(ignore_list, x, y):
# Loop through our ignore_list and check for this x/y
ignore = False
for range in ignore_list:
x_range = range.split(',')[0]
y_range = range.split(',')[1]
x_min = int(x_range.split('-')[0])
x_max = int(x_range.split('-')[1])
y_min = int(y_range.split('-')[0])
y_max = int(y_range.split('-')[1])
if (x >= x_min and x <= x_max and y >= y_min and y <= y_max):
ignore = True
return ignore
def play_music(music_file, volume=0.8):
'''
stream music with mixer.music module in a blocking manner
this will stream the sound from disk while playing
'''
# set up the mixer
freq = 44100 # audio CD quality
bitsize = -16 # unsigned 16 bit
channels = 2 # 1 is mono, 2 is stereo
buffer = 2048 # number of samples (experiment to get best sound)
pg.mixer.init(freq, bitsize, channels, buffer)
# volume value 0.0 to 1.0
pg.mixer.music.set_volume(volume)
clock = pg.time.Clock()
try:
pg.mixer.music.load(music_file)
#print("Music file {} loaded!".format(music_file))
except pg.error:
print("File {} not found! ({})".format(music_file, pg.get_error()))
return
pg.mixer.music.play()
while pg.mixer.music.get_busy():
# check if playback has finished
clock.tick(30)
def check_if_dishes_exist():
# Note: Larger images require more processing power and have more false positives
image_original = cv2.imread(os.path.join('dirty.jpeg'))
#print("Cropping image to limit processing to just the sink")
image = image_original[crop_left:crop_right, crop_top:crop_bottom]
image = image_original
#print("Copying image")
output = copy.copy(image)
#print("Blurring image")
blurred = cv2.GaussianBlur(image, (9, 9), 2, 2)
cv2.imwrite(os.path.join('blurred.jpg'), blurred)
#print("Converting to grey")
gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
cv2.imwrite(os.path.join('gray.jpg'), gray)
#print("Detecting circles in blurred and greyed image")
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20,
param1=100,
param2=circle_sensitivity,
minRadius=min_rad,
maxRadius=max_rad)
#print("Checking if we found images")
if circles is not None:
dish_count = 0
print("Dishes Found!")
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle
# corresponding to the center of the circle
cv2.circle(output, (x, y), r, (0, 255, 0), 4)
cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
# Check our ignore_list
if (should_ignore(ignore_list, x, y)):
print("Circle in ignore_list: Ignoring")
else:
dish_count += 1
print("Dish count:%s" % (str(dish_count)))
cv2.imwrite(os.path.join('detected.jpg'), output)
if dish_count >= min_dishes:
print("Playing dirty dishes sound..")
global_vars.current_sink_status = "dirty"
# optional volume 0 to 1.0
print("test")
else:
print("No Dishes Found!")
if __name__ == "__main__":
check_if_dishes_exist()

BIN
gray.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 406 KiB

37
main.py Normal file
View file

@ -0,0 +1,37 @@
import cv2
import numpy as np
# Load the image
image = cv2.imread('dirty.jpeg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply Gaussian blur to reduce noise
gray = cv2.GaussianBlur(gray, (9, 9), 2)
# Detect circles using the Hough Circles method
circles = cv2.HoughCircles(
gray,
cv2.HOUGH_GRADIENT,
dp=1, # Inverse ratio of accumulator resolution to image resolution
minDist=20, # Minimum distance between the centers of detected circles
param1=50, # Higher threshold for edge detection
param2=30, # Accumulator threshold for circle detection
minRadius=10, # Minimum radius
maxRadius=100 # Maximum radius
)
if circles is not None:
circles = np.uint16(np.around(circles))
for circle in circles[0, :]:
center = (circle[0], circle[1])
radius = circle[2]
# Draw the circle and its center
cv2.circle(image, center, radius, (0, 255, 0), 2)
cv2.circle(image, center, 2, (0, 0, 255), 3)
# Display the result
cv2.imshow('Hough Circles', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

BIN
test.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB