Introduction
In recent years, object detection has revolutionized computer vision by enabling machines to identify and locate objects in digital images. The goal of object is to detect and classify multiple objects of interest within an image, as well as provide accurate bounding box coordinates that outline the objects’ location. This allows for a more detailed understanding of visual content and enables subsequent analysis and decision making. This blog provides a simplified yet detailed overview of object detection, its significance, and its real-world applications
Python Implementation on Object Classification and Detection
We must install some dependencies. The initial step is to have Python installed on the system. Download and install Python3 from Python’s official website: https://www.python.org/. Once we have installed Python on the system successfully, and use IDE like Jupyter Notebook, Spyder or other ide to execute the code. We must install the following dependencies with the help of the pip installer:
Following things are needed to execute the code we will be writing:
NumPy is the python library which is used for working with array objects adding support for large, multi-dimensional arrays and matrices, along with an enormous collection of high-level mathematical functions to operate on these arrays.
OpenCV is the huge open-source library for computer vision, machine learning, and image processing. OpenCV is a library of programming functions for real-time computer vision.
Steps to download the requirements below:
Run The following command in the terminal to install NumPy and OpenCV
pip install numpy
pip install opencv-python
The script requires four input arguments.
All these files are available on the GitHub repository. This model is trained on COCO dataset (common objects in context) from Microsoft. It can detect 80 common objects.
Source Code:
In the below code using object detection, by taking the image converting into Gray scale and counting number of circles has been demonstrated using canny edge detection and noise reduction is done using Gaussian filter and image, by using contours we get the image from Gray scale image to RGB image then counting will done using len() function.
import cv2 import numpy as np import matplotlib.pyplot as plt image = cv2.imread('coin1.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) plt.imshow(gray, cmap='gray'); blur = cv2.GaussianBlur(gray, (11,11), 0) plt.imshow(blur, cmap='gray') canny = cv2.Canny(blur, 30, 150, 3) plt.imshow(canny, cmap='gray') dilated = cv2.dilate(canny, (1,1), iterations = 2) plt.imshow(dilated, cmap='gray') (cnt, heirarchy) = cv2.findContours(dilated.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) cv2.drawContours(rgb, cnt, -1, (0,255,0), 2) plt.imshow(rgb) print('Coins in the image: ', len(cnt)) plt.show()
Output:
2.Object Detection and Classification
import cv2 import numpy as np # Load Yolo net = cv2.dnn.readNet("yolov3.weights", "yolov3.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 image img = cv2.imread("kitchen1.jpg") img = cv2.resize(img, None, fx=0.4, fy=0.4) height, width, channels = img.shape cv2.imshow("img",img) # Detecting objects blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False) for b in blob: for n,img_blob in enumerate(b): cv2.imshow(str(n),img_blob) net.setInput(blob) outs = net.forward(output_layers) print(outs) # 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.5: # 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.5, 0.4) print(indexes) font = cv2.FONT_HERSHEY_PLAIN for i in range(len(boxes)): if i in indexes: x, y, w, h = boxes[i] label = str(classes[class_ids[i]]) color = colors[class_ids[i]] cv2.rectangle(img, (x, y), (x + w, y + h), color, 2) cv2.putText(img, label, (x, y + 30), font, 3, color, 3) print(label) cv2.imshow("Image", img) cv2.waitKey(0) cv2.destroyAllWindows()
Output:
3.Circle Detection using Webcam
OpenCV and Hough Circle algorithm are used to detect the circle from live video using webcam , below program shows how circles are detected from webcam image.
#Circle detection using opencv and HoughCircle import numpy as np import cv2 #Detect circle on webcam cap = cv2.VideoCapture(0,cv2.CAP_DSHOW) while True: img1 = cap.read() img2 = img.copy() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.medianBlur(gray, 5) #parameters---(img,circle_method,dp,mindist,parm1,parm2[p1<p2],) circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 10, param1=50, param2=30, minRadius=0,maxRadius=0) if circles is not None: data = np.uint16(np.around(circles)) for (x, y ,r) in data[0, :]: cv2.circle(img2, (x, y), r, (50, 10, 50), 3) #outer circle cv2.circle(img2, (x, y), 2, (0, 255, 100), -1) #center cv2.imshow("res",img2) if cv2.waitKey(25) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows()
Output:
Applications of Object Detection
Object detection plays a vital role in facial recognition systems, enabling accurate identification and authentication. It finds applications in security systems, access control, and digital surveillance, enhancing safety and convenience.
Object detection enables automated people counting, which has applications in crowd analysis, event management, and public safety. It provides valuable insights into crowd behavior, density estimation, and resource allocation.
In industrial settings, object detection helps optimize quality control processes. It enables the identification and classification of products, ensuring accuracy in inventory management, sorting, and production line optimization.
Object detection is a fundamental component of self-driving car technology. By accurately detecting and classifying objects in real-time, it enhances the perception and decision-making capabilities of autonomous vehicles, ensuring safe navigation on roads.
Security systems rely on object detection to detect and identify potential threats in real time. By analysing surveillance footage and identifying objects of interest, security personnel can respond effectively to potential security breaches.
Conclusion
In this article, based on experimental results we can detect object more precisely and identify the objects individually with exact location of an object in the picture in x,y axis. This paper also provides experimental results on different methods for object detection and identification and compares each method for their efficiencies.
Object detection has emerged as a transformative technology within the field of computer vision. By enabling machines to identify and locate objects accurately, it has found applications in areas such as security, transportation, manufacturing, and more. With ongoing advancements in algorithms, hardware, and deep learning techniques, the future of object detection looks promising. Embracing these innovations will unleash the full potential of computer vision and open new avenues for solving complex problems in various industries.
neha.annam