element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
RoadTests & Reviews
  • Products
  • More
RoadTests & Reviews
Blog ML Capability of Raspberry Pi Zero 2W: Face Detection (using OpenCV)
  • Blog
  • RoadTest Forum
  • Documents
  • Events
  • RoadTests
  • Reviews
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • RoadTest
  • raspberry pi 2W
  • ml
  • raspberry pi
  • cbohra00627
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Related
Recommended

ML Capability of Raspberry Pi Zero 2W: Face Detection (using OpenCV)

cbohra00627
cbohra00627
23 Apr 2022

Introduction:

In this blog, I will implement face detection using Opencv on the Zero 2W.

Setup:

  • Install pip:
    sudo apt install python3-pip
  • Install Opencv:
    pip install opencv-python
  • Install libgl1 to avoid some errors:
    sudo apt install libgl1
  • Download a pre-trained model for frontal face detection from opencv github project and save it in the current working directory by name: 'haarcascade_frontlface_default.xml'. Here is the link.

Now, lets test the code with some images.

Code:

import cv2

#Load the photograph
pixels = cv2.imread('test.jpg')

#Load the pre-trained model
classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

#Perform face detection
bboxes = classifier.detectMultiScale(pixels)

#Print bounding box for each detected face
for box in bboxes:
	#Extract box lower left corner coordinates and width and height
	x, y, width, height = box
	x1, y1 = x + width, y + height
	#Draw a rectangle over the pixels
	cv2.rectangle(pixels, (x, y), (x1, y1), (0,0,255), 1)

#Save the image
cv2.imwrite('output.jpg', pixels)

Testing:

test1 output1
test2 output2
test3 output3
test4 output4
test5 output5

It can be seen from the table that all the people were detected in the 2nd and the 4th pic.

In the 1st pic, it seems the model was not able to pickup tilted faces. In the 3rd pic, the model couldn't detect faces because the resolution of the pic is very low. And in the 5th pic, the model could barely detect some of them due to tilted (upside down faces).

Implementing face detection in video:

Code:

import time
import cv2

start = time.time()
#Load the cascade model
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

#Capture video from the file
cap = cv2.VideoCapture("pexels.mp4")

#Video Resolution
frame_w = int(cap.get(3))
frame_h = int(cap.get(4))

size = (frame_w, frame_h)

#VideoWriter Object to save the video
result = cv2.VideoWriter("test.mp4", cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, size)

while True:
    #Read the frame
    ret, frame = cap.read()
    
    #Break out of loop if End Of File is received
    if not ret:
        print("End")
        break
        
    #Convert to grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
    #Detect the faces
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)
    
    #Draw the rectangle over the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
        
    #Write the frame to the file
    result.write(frame)
    
    #Stop if 's' key is pressed
    if cv2.waitKey(1) & 0xFF == ord('s'):
        break
        
#Release the VideoCapture and the VideoWriter Objects
cap.release()
result.release()

end = time.time()
print("Time taken: ", (end-start))

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image
You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

image

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image
You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

image

I ran the same code on Raspberry Pi 4B (2GB RAM) for comparison and the results are as follows: -

Time taken for: 1st video - 77.74 sec | 2nd video - 34.39 sec

Conclusion:

The Zero 2W board detected faces easily without taking much time. It couldn't detect faces in some images but that's the model's limitation not board's. It shows that image processing can be implemented efficiently on the Zero 2W board.

In case of video processing, the Zero 2W seems to be a lot slower.

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube