Close

First step--load the first tool OpenCV:

A project log for Anna2 aka A2 the agbot!

Anna2 is a general purpose agricultural robot with an advanced vision system, OpenCV and deep neural network models.

dennisDennis 03/31/2017 at 00:350 Comments

I loaded OpenCV on the Raspberry Pi. I didn’t think there was a need to go through too many details with a lot of tutorials on loading OpenCV on Raspberrys online. Below is a short program to test OpenCV. I used pygame to display the results on my remote desktop.

# Set up haarcascades
HAAR_PATH = "/home/pi/opencv-3.1.0/data/haarcascades"
# Face
FACE_HAAR = os.path.join(HAAR_PATH, "haarcascade_frontalface_default.xml")
face_cascade = cv2.CascadeClassifier(FACE_HAAR)

#Set up cam and pygame
cam = cv2.VideoCapture(0)
pygame.init()


#create fullscreen display 640x480
screen = pygame.display.set_mode((640,480),0)

while(True):
ret, frame = cam.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray_small = cv2.resize(gray, (160,120))
faces = face_cascade.detectMultiScale(gray_small, 1.3 , 5)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x*4,y*4),((x*4) + (w*4),(y*4) + (h*4)),(255,0,0),2)
pg_image = pygame.image.frombuffer(frame.tostring(),(640,480) ,"RGB")
screen.blit(pg_image, (0, 0)) #Load new image on screen
pygame.display.update()
print len(faces)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
cam.release()
cv2.destroyAllWindows()
sys.exit()

I plugged in a USB Camera and ran the program.

Here’s the results…It works!

Discussions