Skip to main content

2 posts tagged with "object detection"

View All Tags

· 4 min read
Sparsh Agarwal

We are going to discuss the following 4 use cases:

  1. Detect faces, eyes, pedestrians, cars, and number plates using OpenCV haar cascade classifiers
  2. Streamlit app for MobileNet SSD Caffe Pre-trained model
  3. Streamlit app for various object detection models and use cases
  4. Detect COCO-80 class objects in videos using TFHub MobileNet SSD model

Use Case 1 - Object detection with OpenCV

Face detection - We will use the frontal face Haar cascade classifier model to detect faces in the given image. The following function first passes the given image into the classifier model to detect a list of face bounding boxes and then runs a loop to draw a red rectangle box around each detected face in the image:

def detect_faces(fix_img):
face_rects = face_classifier.detectMultiScale(fix_img)
for (x, y, w, h) in face_rects:
cv2.rectangle(fix_img,
(x,y),
(x+w, y+h),
(255,0,0),
10)
return fix_img

Eyes detection - The process is almost similar to the face detection process. Instead of frontal face Haar cascade, we will use the eye detection Haar cascade model.

Input image

Input image

detected faces and eyes in the image

detected faces and eyes in the image

Pedestrian detection - We will use the full-body Haar cascade classifier model for pedestrian detection. We will apply this model to a video this time. The following function will run the model on each frame of the video to detect the pedestrians:

# While Loop
while cap.isOpened():
# Read the capture
ret, frame = cap.read()
# Pass the Frame to the Classifier
bodies = body_classifier.detectMultiScale(frame, 1.2, 3)
# if Statement
if ret == True:
# Bound Boxes to Identified Bodies
for (x,y,w,h) in bodies:
cv2.rectangle(frame,
(x,y),
(x+w, y+h),
(25,125,225),
5)
cv2.imshow('Pedestrians', frame)
# Exit with Esc button
if cv2.waitKey(1) == 27:
break
# else Statement
else:
break

# Release the Capture & Destroy All Windows
cap.release()
cv2.destroyAllWindows()

Car detection - The process is almost similar to the pedestrian detection process. Again, we will use this model on a video. Instead of people Haar cascade, we will use the car cascade model.

Car number plate detection - The process is almost similar to the face and eye detection process. We will use the car number plate cascade model.

You can find the code here on Github.

Use Case 2 - MobileNet SSD Caffe Pre-trained model

You can play with the live app here. Souce code is available here on Github.

Use Case 3 - YOLO Object Detection App

You can play with the live app *here. Source code is available here on Github.*

This app can detect COCO 80-classes using three different models - Caffe MobileNet SSD, Yolo3-tiny, and Yolo3. It can also detect faces using two different models - SSD Res10 and OpenCV face detector. Yolo3-tiny can also detect fires.

/img/content-blog-raw-blog-object-detection-with-yolo3-untitled.png

/img/content-blog-raw-blog-object-detection-with-yolo3-untitled-1.png

Use Case 4 - TFHub MobileNet SSD on Videos

In this section, we will use the MobileNet SSD object detection model from TFHub. We will apply it to videos. We can load the model using the following command:

module_handle = "https://tfhub.dev/google/openimages_v4/ssd/mobilenet_v2/1"
detector = hub.load(module_handle).signatures['default']

After loading the model, we will capture frames using OpenCV video capture method, and pass each frame through the detection model:

cap = cv2.VideoCapture('/content/Spectre_opening_highest_for_a_James_Bond_film_in_India.mp4')
for i in range(1,total_frames,200):
cap.set(cv2.CAP_PROP_POS_FRAMES,i)
ret,frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
run_detector(detector,frame)

Here are some detected objects in frames:

/img/content-blog-raw-blog-object-detection-hands-on-exercises-untitled.png

/img/content-blog-raw-blog-object-detection-hands-on-exercises-untitled-1.png

/img/content-blog-raw-blog-object-detection-hands-on-exercises-untitled-2.png

You can find the code here on Github.


Congrats! In the next post of this series, we will cover 5 exciting use cases - 1) detectron 2 object detection fine-tuning on custom class, 2) Tensorflow Object detection API inference, fine-tuning, and few-shot learning, 3) Inference with 6 pre-trained models, 4) Mask R-CNN object detection app, and 5) Logo detection app deployment as a Rest API using AWS elastic Beanstalk.

· 2 min read
Sparsh Agarwal

Face detection

We will use the frontal face Haar cascade classifier model to detect faces in the given image. The following function first passes the given image into the classifier model to detect a list of face bounding boxes and then runs a loop to draw a red rectangle box around each detected face in the image:

def detect_faces(fix_img):
face_rects = face_classifier.detectMultiScale(fix_img)
for (x, y, w, h) in face_rects:
cv2.rectangle(fix_img,
(x,y),
(x+w, y+h),
(255,0,0),
10)
return fix_img

Eyes detection

The process is almost similar to the face detection process. Instead of frontal face Haar cascade, we will use the eye detection Haar cascade model.

Input image

Input image

detected faces and eyes in the image

detected faces and eyes in the image

Pedestrian detection

We will use the full-body Haar cascade classifier model for pedestrian detection. We will apply this model to a video this time. The following function will run the model on each frame of the video to detect the pedestrians:

# While Loop
while cap.isOpened():
# Read the capture
ret, frame = cap.read()
# Pass the Frame to the Classifier
bodies = body_classifier.detectMultiScale(frame, 1.2, 3)
# if Statement
if ret == True:
# Bound Boxes to Identified Bodies
for (x,y,w,h) in bodies:
cv2.rectangle(frame,
(x,y),
(x+w, y+h),
(25,125,225),
5)
cv2.imshow('Pedestrians', frame)
# Exit with Esc button
if cv2.waitKey(1) == 27:
break
# else Statement
else:
break

# Release the Capture & Destroy All Windows
cap.release()
cv2.destroyAllWindows()

Car detection

The process is almost similar to the pedestrian detection process. Again, we will use this model on a video. Instead of people Haar cascade, we will use the car cascade model.

Car number plate detection

The process is almost similar to the face and eye detection process. We will use the car number plate cascade model.

You can find the code here on Github.