Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
276 changes: 276 additions & 0 deletions AbhishekBhosale_C#/AbhishekBhosale_Objectdetection_OpenCV.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Imports"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import cv2 as cv"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Loading the Haar Cascade Classifier (xml) files and images"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# Loads all the classifiers\n",
"face_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml')\n",
"eye_cascade = cv.CascadeClassifier('haarcascade_eye.xml')\n",
"fullbody_cascade = cv.CascadeClassifier('haarcascade_fullbody.xml')\n",
"cars_cascade = cv.CascadeClassifier('cars.xml')\n",
"bus_cascade = cv.CascadeClassifier('bus.xml')\n",
"\n",
"# Loads all the images\n",
"img = cv.imread('face.jpg')\n",
"body = cv.imread('body1.jpg')\n",
"car = cv.imread('cars1.jpeg')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Defining object detection function"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def object_detection(image, cascade, text, color = (255, 0, 0)):\n",
" \n",
" # Our operations on the frame come here\n",
" gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)\n",
" objects = cascade.detectMultiScale(gray, 1.3, 5)\n",
" for (x, y, w, h) in objects:\n",
" \n",
" # Draws rectangle around the objects\n",
" image = cv.rectangle(image, (x,y), (x+w , y+h), color, 2)\n",
" \n",
" # Display text on top of rectangle\n",
" cv.putText(image, text, (x, y-10), cv.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)\n",
" \n",
" # Display the resulting frame\n",
" cv.imshow('img', image)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Face detection and eye detection"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Detects the faces\n",
"object_detection(img, face_cascade, 'face')\n",
"cv.waitKey(0)\n",
"# Closes the image window\n",
"cv.destroyAllWindows()\n",
"\n",
"# Detects the eyes\n",
"object_detection(img, eye_cascade, 'eye')\n",
"cv.waitKey(0)\n",
"# Closes the image window\n",
"cv.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"cap = cv.VideoCapture(0)\n",
"\n",
"while(True):\n",
" # Capture frame-by-frame\n",
" ret, frame = cap.read()\n",
"\n",
" # Our operations on the frame come here\n",
"# gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)\n",
" \n",
"# faces = face_cascade.detectMultiScale(gray, 1.3, 5)\n",
"# for (x, y, w, h) in faces:\n",
"# frame = cv.rectangle(frame, (x,y), (x+w , y+h), (255, 0, 0), 2)\n",
"# roi_gray = gray[y:y+h, x:x+w]\n",
"# roi_color = frame[y:y+h, x:x+w]\n",
"# eyes = eye_cascade.detectMultiScale(roi_gray)\n",
"# for (ex, ey, ew, eh) in eyes:\n",
"# cv.rectangle(roi_color, (ex,ey), (ex+ew , ey+eh), (0, 255, 0), 2)\n",
"\n",
"# # Display the resulting frame\n",
"# cv.imshow('frame', frame)\n",
"\n",
" object_detection(frame, face_cascade, 'face')\n",
" # Waits for Q key to quit\n",
" if cv.waitKey(1) & 0xFF == ord('q'):\n",
" break\n",
"\n",
"# When everything done, release the capture\n",
"cap.release()\n",
"cv.destroyAllWindows()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Car detection"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Loads the video\n",
"cap = cv.VideoCapture('cars.avi')\n",
"\n",
"while(True):\n",
" \n",
" # Capture frame-by-frame\n",
" ret, frame = cap.read()\n",
" \n",
" # Breaks at the end of video\n",
" if (type(frame) == type(None)):\n",
" break\n",
" \n",
" # Calls the object detection function\n",
" object_detection(frame, cars_cascade, 'car')\n",
" \n",
" if cv.waitKey(1) & 0xFF == ord('q'):\n",
" break\n",
"\n",
"# When everything done, release the capture\n",
"cap.release()\n",
"cv.destroyAllWindows()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Human detection"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Loads the video\n",
"cap = cv.VideoCapture('pedestrians.avi')\n",
"\n",
"while(True):\n",
" \n",
" # Capture frame-by-frame\n",
" ret, frame = cap.read()\n",
" \n",
" # Breaks at the end of video\n",
" if (type(frame) == type(None)):\n",
" break\n",
" \n",
" # Calls the object detection function\n",
" object_detection(frame, fullbody_cascade, 'human')\n",
" \n",
" if cv.waitKey(1) & 0xFF == ord('q'):\n",
" break\n",
"\n",
"# When everything done, release the capture\n",
"cap.release()\n",
"cv.destroyAllWindows()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Bus detection"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Loads the video\n",
"cap = cv.VideoCapture('bus1.mp4')\n",
"\n",
"while(True):\n",
" \n",
" # Capture frame-by-frame\n",
" ret, frame = cap.read()\n",
" \n",
" # Breaks at the end of video\n",
" if (type(frame) == type(None)):\n",
" break\n",
" \n",
" # Calls the object detection function\n",
" object_detection(frame, bus_cascade, 'bus')\n",
" \n",
" if cv.waitKey(1) & 0xFF == ord('q'):\n",
" break\n",
"\n",
"# When everything done, release the capture\n",
"cap.release()\n",
"cv.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
40 changes: 40 additions & 0 deletions AbhishekBhosale_C#/AbhishekBhosale_Objectdetection_OpenCV.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Created by Abhishek

## Object Detection

In this page for Object Detection we will be using Haar feature-based Cascade classifiers.

**What is that?**

It is an effective object detection method proposed by Paul Viola and Michael Jones in their paper, “Rapid Object Detection using a Boosted Cascade of Simple Features” in 2001. It is a machine learning based approach where a cascade function is trained from a lot of positive and negative images. It is then used to detect objects in other images.

Initially, the algorithim need a lot of positive images and negative images to train the classifier. Then we need to extract features from it so for this we use haar features as shown the image below.Now all possible sizes and locations of each kernel s used to calculate plenty of features. But among all these features we calculated, most of them are irrelevant.For this, we apply each and every feature on all the training images. For each feature, it finds the best threshold which will classify the faces to positive and negative. But obviously, there will be errors or misclassifications. We select the features with minimum error rate, which means they are the features that best classifies the face and non-face images.Final classifier is a weighted sum of these weak classifiers. It is called weak because it alone can’t classify the image, but together with others forms a strong classifier. The paper says even 200 features provide detection with 95% accuracy. Their final setup had around 6000 features.

![image](https://rahullpatell.files.wordpress.com/2015/04/main23.png)

In an image, most of the image region is non-face region. So it is a better idea to have a simple method to check if a window is not a face region. If it is not, discard it in a single shot. Don’t process it again. Instead focus on region where there can be a face. This way, we can find more time to check a possible face region.

For this they introduced the concept of Cascade of Classifiers. Instead of applying all the 6000 features on a window, group the features into different stages of classifiers and apply one-by-one.If a window fails the first stage, discard it. We don’t consider remaining features on it. If it passes, apply the second stage of features and continue the process. The window which passes all stages is a face region.

So this is a simple intuitive explanation of how Viola-Jones face detection works.

---

**Now lets take an example like face detection and eye detection and see what are the process that needs to be done.**

* A image when we import using OpenCV it is of the color format BGR. So we need to change it to grayscale to apply the Haar Cascade Classifier.
* Now to find the faces we use the **detectMultiScale()** function and it returns values of the format **(x, y, w, h)** where x is the starting x-coordinate, y is the starting y-coordinate and w is the width and h is the height.
* Then to make it visible on the photo we use **rectangle()** function and draw rectangle around it like the below picture.
![image](https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTF9LipPzwRM-yh3DO7tNk4hHgB6MHmVKtorg&usqp=CAU)
* Now we can jus take out the part of the image of the face using **roi()** funtion and then apply the eye cascade classifier for this part by repeating the above steps.


**We can also apply this classifier for a video as well.**

The steps are as follows:
* First the video capture needs to be turned on using **VideoCapture()** function.
* Next the current frame needs to extracted using **read** function.
* Now we need to do the same process as we did for an image.


The full code is [here](https://github.com/Abhixtrent28/Open-contributions/blob/2082bdc61628061754183107be7baf459e1deb62/AbhishekBhosale_C%23/AbhishekBhosale_Objectdetection_OpenCV.ipynb)
Loading