We have been able to implement code from OpenCV and online to create a system that can detect faces. Though we are not currently detecting plastic with this code, we will be able to use this code as a basis for the plastic detection system. Below is the implemented code from GitHub user Radames.
import cv2
cap = cv2.VideoCapture(0)
cap.set(3, 640) #WIDTH
cap.set(4, 480) #HEIGHT
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
print(len(faces))
# Display the resulting frame
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
This month, we did not work too much on the programming aspect of GRIP, as we were focusing on receiving our parts and wrapping up planning. However, in the past week with the Raspberry Pi in our hands, we were able to figure our how to use the Pi's built in IDE called Nano to start coding in Python. Additionally, we were able to download OpenCV and the Pi's OS onto the computer, which took about two hours combined. Unfortunately, we will have to these downloads again as we are using a temporary Micro-SD with limited storage. However, the download process should go more quickly as our team now has experience navigating through the Pi's software and configuring it.
This month, we were able to work a lot on the programming aspect of GRIP, as we have finished the planning aspect and have received our major parts. The code has the same purpose of detecting faces like the code from October 18th, except it is adapted for use with the Raspberry Pis and the camera associated with it. Below is that adapted code.
import cv2
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
# allow the camera to warmup
time.sleep(0.1)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
image = frame.array
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
print(len(faces))
for (x,y,w,h) in faces:
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = image[y:y+h, x:x+w]
cv2.imshow('frame',image)
# show the frame
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the q
key was pressed, break from the loop
if key == ord("q"):
break
Our current code is still the same from that of last month's update. This is because the framework for the code is identical for any object that has to be detected; all that has to be changed is the referenced XML file that contains the information for particular object detection (plastic vs. faces). We are currently working on collecting a large dataset of positive and negative images that will decoded into the XML for accurate image detection of plastic.
This month, we were able to code up the plastic detection code on our laptops. Using a dataset called COCO and an object detection software called YOLO, we were able to detect various plastics on land. With the plastic code working on our laptops, all we need to do is upload the code onto the Raspberry Pi and test the detection system with underwater plastic. Within the next month, the detection system should be done and uploaded onto the Pi. Below is our updated code that we adapted off of a website called PySource.
import cv2
import numpy as np
import time
# Load Yolo
net = cv2.dnn.readNet("yolov3-tiny.weights", "yolov3-tiny.cfg")
classes = []
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))
# Loading camera
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_PLAIN
starting_time = time.time()
frame_id = 0
while True:
_, frame = cap.read()
frame_id += 1
height, width, channels = frame.shape
# Detecting objects
blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
# Showing informations on the screen
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.2:
# Object detected
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# Rectangle coordinates
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.4, 0.3)
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
confidence = confidences[i]
color = colors[class_ids[i]]
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv2.rectangle(frame, (x, y), (x + w, y + 30), color, -1)
cv2.putText(frame, label + " " + str(round(confidence, 2)), (x, y + 30), font, 3, (255,255,255), 3)
elapsed_time = time.time() - starting_time
fps = frame_id / elapsed_time
cv2.putText(frame, "FPS: " + str(round(fps, 2)), (10, 50), font, 3, (0, 0, 0), 3)
cv2.imshow("Image", frame)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
Below is an image of the program detecting an object made up of plastic: water bottles.