Survey conducted and the code used
The Automatic Trash Bin
Year
Two Group Project, University of Liverpool, Electrical and Electronics
Engineering Department.
The developments of
this project were supervised by Dr. James Bradley
The Group members are
Haoran Cheng, Xuhui Gong, Shekhah
Mobarak, A-ro Han
In this section of the blog you will find the survey that was conducted by the group along with the code used for the entire project.
The questions of the survey are written below;
1. Do you think that recycling helps preserve the environment?
-
Yes
-
No
-
If you chose no, please explain why?
-
Yes
-
Sometimes
-
No
-
If you chose no, please explain why?
3. In your opinion what do you think
happens if recycling is not promoted?
4. Do you think the recycling system
nowadays (color codes) is straightforward and comprehensible?
-
Yes
-
No
-
If you chose no, please explain why?
5. Do you think an automatic sorting
bin may ease the operation of recycling?
-
Yes
-
No
-
If you chose no, please explain why?
6. Would you buy an automatic sorting
bin?
-
Yes
-
No
-
If you chose no, please explain why?
7. If you were to design this product
what would you include?
-
Motion detector for cover
-
AI voice to y3ln what object has
been thrown into the bin
-
Brail instructions of the sides of
the bin for the visually impaired
-
Screen to show identification of
object
-
Easy dispensing of the rubbish
-
Other
Screenshots of the survey
As for the codes which are used to operate the system.
There were four main codes;
test.py
import keras
import shutil
import tensorflow as tf
import sys
import os
import argparse
import datetime
import time
import cv2
import numpy as np
def sort():
Cans = 0
Paper = 0
Plastic_Bottle = 0
# lists all files to be tested in test_input folder
list_images = os.listdir(image_path)
with tf.compat.v1.Session() as sess:
for igs in list_images:
# load the image data
image_data = tf.compat.v1.gfile.FastGFile(image_path + igs, 'rb').read()
# Feed the image_data as input to the graph and get first prediction
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
predictions = sess.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data})
# Sort to show labels of first prediction in order of confidence
top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]
# print('\n' + igs + ' :')
human_string = label_lines[0]
score = predictions[0][0]
# print('%s (score = %.5f)' % (human_string, score))
if human_string == "cans" and score >= 0.65:
Cans += 1
elif human_string == "paper" and score >= 0.97:
Paper += 1
elif human_string == "plastic bottle" and score >= 0.65:
Plastic_Bottle += 1
print(Cans)
print(Paper)
print(Plastic_Bottle)
if Cans > Paper and Cans > Plastic_Bottle:
print("can")
elif Paper > Cans and Paper > Plastic_Bottle:
print("paper")
elif Plastic_Bottle > Cans and Plastic_Bottle > Paper:
print("plastic_bottle")
def detect():
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="path to the video file")
ap.add_argument("-a", "--min-area", type=int, default=1000, help="minimum area size")
args = vars(ap.parse_args())
# if the video argument is None, then we are reading from webcam
if args.get("video", None) is None:
camera = cv2.VideoCapture(0)
time.sleep(0.25)
# otherwise, we are reading from a video file
else:
camera = cv2.VideoCapture(args["video"])
# initialize the first frame in the video stream
firstFrame = None
# Define the codec
fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
framecount = 0
frame = np.zeros((640, 480))
out = cv2.VideoWriter(
'./videos/' + 'calm_down_video_' + datetime.datetime.now().strftime("%A_%d_%B_%Y_%I_%M_%S%p") + '.avi', fourcc,
5.0, np.shape(frame))
# to begin with, the light is not stable, calm it down
tc = 40
while tc:
ret, frame = camera.read()
out.write(frame)
# cv2.imshow("vw",frame)
cv2.waitKey(10)
tc -= 1
totalc = 2000
tc = totalc
out.release()
# loop over the frames of the video
while True:
# grab the current frame and initialize the occupied/unoccupied
# text
(grabbed, frame) = camera.read()
text = "Unoccupied"
# if the frame could not be grabbed, then we have reached the end
# of the video
if not grabbed:
time.sleep(0.25)
continue
# resize the frame, convert it to grayscale, and blur it
# frame = imutils.resize(frame, width=500)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
# update firstFrame for every while
if tc % totalc == 0:
firstFrame = gray
tc = (tc + 1) % totalc
continue
else:
tc = (tc + 1) % totalc
# print tc
# compute the absolute difference between the current frame and
# first frame
frameDelta = cv2.absdiff(firstFrame, gray)
thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
# dilate the thresholded image to fill in holes, then find contours
# on thresholded image
thresh = cv2.dilate(thresh, None, iterations=2)
# _, cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:]
# _, contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# loop over the contours
for c in contours:
print(framecount)
# if the contour is too small, ignore it
if cv2.contourArea(c) < args["min_area"]:
continue
# compute the bounding box for the contour, draw it on the frame,
# and update the text
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
text = "Occupied"
# draw the text and timestamp on the frame
cv2.putText(frame, "Monitoring Area Status: {}".format(text), (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(0, 0, 255), 2)
cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"), (10, frame.shape[0] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
# show the frame and record if the user presses a key
cv2.imshow("Security Feed", frame)
cv2.imshow("Thresh", thresh)
cv2.imshow("Frame Delta", frameDelta)
# save the detection result
if text == "Occupied":
if framecount == 0:
# # create VideoWriter object
# out = cv2.VideoWriter('./videos/' + datetime.datetime.now().strftime("%A_%d_%B_%Y_%I_%M_%S%p") + '.avi',
# fourcc, 10.0, np.shape(gray)[::-1])
# cv2.imwrite('./images/' + datetime.datetime.now().strftime("%A_%d_%B_%Y_%I_%M_%S%p") + '.jpg', frame)
# # write the flipped frame
# out.write(frame)
framecount += 1
elif framecount > 200:
break
else:
# write the flipped frame
out.write(frame)
if framecount % 10 == 0:
cv2.imwrite('./images/' + datetime.datetime.now().strftime("%A_%d_%B_%Y_%I_%M_%S%p") + '.jpg',
frame)
framecount += 1
elif framecount < 200 and framecount > 1:
# write the flipped frame
out.write(frame)
# if framecount%10 == 0:
cv2.imwrite('./images/'+datetime.datetime.now().strftime("%A_%d_%B_%Y_%I_%M_%S%p")+'.jpg',frame)
framecount += 1
key = cv2.waitKey(1) & 0xFF
# if the `ESC` key is pressed, break from the lop
if key == 27:
break
# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()
def clear():
shutil.rmtree("/Users/xg/Desktop/DL-wastesort-master-2/images")
os.mkdir("/Users/xg/Desktop/DL-wastesort-master-2/images")
def loop():
while True:
detect()
sort()
clear()
if __name__ == '__main__':
# Disable tensorflow compilation warnings
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
image_path = '/Users/xg/Desktop/DL-wastesort-master-2/images/'
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line
in tf.compat.v1.io.gfile.GFile("tf_files/retrained_labels.txt")]
# Unpersists graph from file
with tf.compat.v1.gfile.FastGFile("tf_files/retrained_graph.pb", 'rb') as f:
graph_def = tf.compat.v1.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
try:
loop()
except KeyboardInterrupt:
sys.exit()
train.sh
python3 retrain.py \
--bottleneck_dir=tf_files/bottlenecks \
--how_many_training_steps=6000 \
--model_dir=inception \
--summaries_dir=tf_files/training_summaries/basic \
--output_graph=tf_files/retrained_graph.pb \
--output_labels=tf_files/retrained_labels.txt \
--image_dir=training_dataset
wastesort.py
import keras
import tensorflow as tf
import sys
import os
# Disable tensorflow compilation warnings
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
image_path = '/Users/xg/Desktop/DL-wastesort-master-2/test_input/'
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line
in tf.compat.v1.io.gfile.GFile("tf_files/retrained_labels.txt")]
# Unpersists graph from file
with tf.compat.v1.gfile.FastGFile("tf_files/retrained_graph.pb", 'rb') as f:
graph_def = tf.compat.v1.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
# lists all files to be tested in test_input folder
list_images = os.listdir(image_path)
with tf.compat.v1.Session() as sess:
for igs in list_images:
# load the image data
image_data = tf.compat.v1.gfile.FastGFile(image_path + igs, 'rb').read()
# Feed the image_data as input to the graph and get first prediction
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
predictions = sess.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data})
# Sort to show labels of first prediction in order of confidence
top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]
print('\n' + igs + ' :')
for node_id in top_k:
human_string = label_lines[node_id]
score = predictions[0][node_id]
print('%s (score = %.5f)' % (human_string, score))
# if __name__ == '__main__':
#
ImageAugmenter.py
# import imgaug as ia
# from imgaug import augmenters as iaa
# import numpy as np
# import imageio
# import cv2
# from skimage.io import imread_collection
# ia.seed(1)
#
# # path= '/Volumes/MDisk/dataset/test_data/*.jpg'
# # img=imread_collection(path)
# img = imageio.imread("/Volumes/MDisk/dataset/test_data/test.jpg") #read you image
#
# print(img)
# images = np.array(
# [img for _ in range(32)], dtype=np.uint8) # 32 means creat 32 enhanced images using following methods.
#
# seq = iaa.Sequential(
# [
# iaa.Fliplr(0.5),
# iaa.Crop(percent=(0, 0.1)),
# iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.5))),
# iaa.ContrastNormalization((0.75, 1.5)),
# iaa.AdditiveGaussianNoise(
# loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
# iaa.Multiply((0.8, 1.2), per_channel=0.2),
# iaa.Affine(
# scale={
# "x": (0.8, 1.2),
# "y": (0.8, 1.2)
# },
# translate_percent={
# "x": (-0.2, 0.2),
# "y": (-0.2, 0.2)
# },
# rotate=(-25, 25),
# shear=(-8, 8))
# ],
# random_order=True) # apply augmenters in random order
#
# images_aug = seq.augment_images(images)
#
# for i in range(32):
# imageio.imwrite(str(i)+'new.jpg', images_aug[i]) #write all changed images
#
import matplotlib
from imgaug import augmenters as iaa
import numpy as np
import imageio
import cv2
from pasta.augment import inline
from skimage.io import imread_collection
import imgaug as ia
from imgaug import augmenters as iaa
import numpy as np
import imageio
from glob import glob
import matplotlib.pyplot as plt
import cv2
import scipy.misc
def get_img(img_paths, img_size):
X = np.zeros((len(img_paths), img_size, img_size, 3), dtype=np.uint8)
i = 0
for img_path in img_paths:
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (img_size, img_size), interpolation=cv2.INTER_AREA)
X[i, :, :, :] = img
i += 1
return X
def get_X_batch(X_path, batch_size, img_size):
while 1:
for i in range(0, len(X_path), batch_size):
X = get_img(X_path[i:i + batch_size], img_size)
yield X
if __name__ == '__main__':
img_paths = glob('/Volumes/MDisk/dataset/test_data/*.jpg') # 得到所有图片的路径列表
print(img_paths)
images = get_X_batch(img_paths, 16, 300) # 得到一个batch的图片,形式为generator
images = next(images) # next(generator),得到一个batch的ndarray
ia.seed(1)
seq = iaa.Sequential([
iaa.Fliplr(0.5), # 0.5的概率水平翻转
iaa.Crop(percent=(0, 0.1)), # random crops
# sigma在0~0.5间随机高斯模糊,且每张图纸生效的概率是0.5
iaa.Sometimes(0.5,
iaa.GaussianBlur(sigma=(0, 0.5))
),
# 增大或减小每张图像的对比度
iaa.ContrastNormalization((0.75, 1.5)),
# 高斯噪点
iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
# 给每个像素乘上0.8-1.2之间的数来使图片变暗或变亮
# 20%的图片在每个channel上乘以不同的因子
iaa.Multiply((0.8, 1.2), per_channel=0.2),
# 对每张图片进行仿射变换,包括缩放、平移、旋转、修剪等
iaa.Affine(
scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
rotate=(-90, 90),
shear=(-8, 8)
)
], random_order=True) # 随机应用以上的图片增强方法
images_aug = seq.augment_images(images) # 得到增强后的图片ndarray
print((len(images_aug)))
for i in range(len(images_aug)):
imageio.imwrite(str(i)+'new.jpg', images_aug[i]) #write all changed images
Comments
Post a Comment