Научная статья на тему 'Real-Time Moving Object Detection from Video Streams in Python: Techniques and Implementation'

Real-Time Moving Object Detection from Video Streams in Python: Techniques and Implementation Текст научной статьи по специальности «Компьютерные и информационные науки»

CC BY
46
16
i Надоели баннеры? Вы всегда можете отключить рекламу.
Ключевые слова
Moving Object Detection / Video Streams / Python / OpenCV / Real-Time / Computer Vision / Surveillance / Robotics

Аннотация научной статьи по компьютерным и информационным наукам, автор научной работы — Soliev Bakhromjon Nabijonovich

This article explores real-time moving object detection from video streams using Python. We delve into various techniques and their implementation, providing a comprehensive guide for practitioners and researchers. By leveraging libraries like OpenCV, we demonstrate practical examples and real-world applications. This study aims to enhance understanding and application of moving object detection in diverse fields such as surveillance, traffic monitoring, and robotics.

i Надоели баннеры? Вы всегда можете отключить рекламу.
iНе можете найти то, что вам нужно? Попробуйте сервис подбора литературы.
i Надоели баннеры? Вы всегда можете отключить рекламу.

Текст научной работы на тему «Real-Time Moving Object Detection from Video Streams in Python: Techniques and Implementation»

Muhammad al-Xorazmiy nomidagi TATU Farg'ona filiali "Al-Farg'oniy avlodlari" elektron ilmiy jurnali ISSN 2181-4252 Tom: 1 | Son: 2 | 2024-yil

"Descendants of Al-Farghani" electronic scientific journal of Fergana branch of TATU named after Muhammad al-Khorazmi. ISSN 2181-4252 Vol: 1 | Iss: 2 | 2024 year

Электронный научный журнал "Потомки Аль-Фаргани" Ферганского филиала ТАТУ имени Мухаммада аль-Хоразми ISSN 2181-4252 Том: 1 | Выпуск: 2 | 2024 год

Real-Time Moving Object Detection from Video Streams in Python: Techniques and

Implementation

I

Introduction

In the rapidly evolving field of computer vision, real-time moving object detection from video streams has emerged as a crucial area of research and application. The ability to detect and track moving objects is fundamental to numerous practical applications including surveillance, autonomous driving, human-computer interaction, and more. With the advancement in computational power and the availability of powerful libraries, Python has become a popular language for implementing these techniques.

Real-time object detection involves identifying and locating objects in a sequence of frames in a video. Unlike still image object detection, it requires handling continuous streams of data, demanding efficient and robust algorithms. The challenges are manifold, ranging from handling varying lighting conditions, camera movements, to distinguishing between background and moving objects.

This article aims to provide a detailed overview of the techniques used in real-time moving object detection from video streams using Python. We will explore different methodologies, review relevant literature, and discuss the implementation of these techniques using Python libraries such as OpenCV. By providing practical examples and real-world applications, this guide intends to equip readers with

Soliev Bakhromjon Nabijonovich,

Senior lecturer of the Fergana branch of the Tashkent University of Information Technologies named after Muhammad al-Khorazmi [email protected]

the knowledge and tools necessary to implement effective moving object detection systems.

Literature Review and Methodology

The detection of moving objects in video streams has been a subject of extensive research. Various methods have been proposed over the years, each with its own strengths and weaknesses. Traditional methods include background subtraction, optical flow, and frame differencing.

1. Background Subtraction: This technique involves modeling the background and detecting moving objects as deviations from this model. Popular methods include Gaussian Mixture Models (GMM) and the Background Subtractor MOG2. Research such as "A survey of background subtraction techniques" (Elhabian et al., 2008) provides a comprehensive review of these methods.

2. Optical Flow: This method calculates the motion of objects between consecutive frames. While it is computationally intensive, it is useful for capturing motion patterns. Notable works include "Lucas-Kanade 20 years on: A unifying framework" (Baker et al., 2004).

3. Frame Differencing: This simple yet effective method involves subtracting consecutive frames to detect changes. It is often used in combination with other techniques to improve

330

Abstract. This article explores real-time moving object detection from video streams using Python. We delve into various techniques and their implementation, providing a comprehensive guide for practitioners and researchers. By leveraging libraries like OpenCV, we demonstrate practical examples and real-world applications. This study aims to enhance understanding and application of moving object detection in diverse fields such as surveillance, traffic monitoring, and robotics.

Keywords: Moving Object Detection, Video Streams, Python, OpenCV, Real-Time, Computer Vision, Surveillance, Robotics

Muhammad al-Xorazmiy nomidagi TATU Farg'ona filiali "Al-Farg'oniy avlodlari" elektron ilmiy jurnali ISSN 2181-4252 Tom: 1 | Son: 2 | 2024-yil

"Descendants of Al-Farghani" electronic scientific journal of Fergana branch of TATU named after Muhammad al-Khorazmi. ISSN 2181-4252 Vol: 1 | Iss: 2 | 2024 year

Электронный научный журнал "Потомки Аль-Фаргани" Ферганского филиала ТАТУ имени Мухаммада аль-Хоразми ISSN 2181-4252 Том: 1 | Выпуск: 2 | 2024 год

robustness. The book "Computer Vision: Algorithms and Applications" by Richard Szeliski offers insights into this approach.

4. Deep Learning Approaches: Recently, deep learning methods have gained popularity due to their high accuracy and robustness. Techniques such as convolutional neural networks (CNNs) are employed for detecting and tracking objects. The paper "You Only Look Once: Unified, Real-Time Object Detection" (Redmon et al., 2016) is a seminal work in this area.

Methodology: Our methodology involves implementing these techniques using Python and OpenCV. We will create a pipeline that captures video streams, processes frames, and detects moving objects in real-time. The steps include:

• Capturing video using OpenCV

• Preprocessing frames (grayscale conversion, noise reduction)

• Applying detection techniques (background subtraction, frame differencing)

• Enhancing detection using morphological operations

• Tracking objects across frames Mathematical Formulation

1. Frame Differencing: Frame differencing involves computing the absolute difference between consecutive frames to detect motion. Let It and I-i represent the current frame and the previous frame at time t, respectively. The frame difference Dt is given by:

2. Background Subtraction: Background subtraction involves modeling the background and subtracting it from the current frame to detect foreground objects. Let Bt represent the background model at time t. The foreground mask /•', is given by:

Here, Bt can be updated over time to adapt to changes in the background. A common method is to use a running average:

where a is a learning rate that determines how quickly the background model adapts.

3. Thresholding: To obtain a binary mask that highlights moving objects, a threshold t is applied to the difference images Dt or Ft : For frame differencing:

For background subtraction:

4. Morphological Operations: Morphological operations such as dilation and erosion are applied to the binary mask Mt to reduce noise and fill gaps:

71

Let i be the result after applying morphological operations:

Combining the Steps

Combining these steps, the overall process of moving object detection can be summarized by the following formulas:

• Compute frame difference:

Apply thresholding to obtain binary mask:

Compute background subtraction:

Ft = \It-Bt\

331

Muhammad al-Xorazmiy nomidagi TATU Farg'ona filiali "Al-Farg'oniy avlodlari" elektron ilmiy jurnali ISSN 2181-4252 Tom: 1 | Son: 2 | 2024-yil

"Descendants of Al-Farghani" electronic scientific journal of Fergana branch of TATU named after Muhammad al-Khorazmi. ISSN 2181-4252 Vol: 1 | Iss: 2 | 2024 year

Электронный научный журнал "Потомки Аль-Фаргани" Ферганского филиала ТАТУ имени Мухаммада аль-Хоразми ISSN 2181-4252 Том: 1 | Выпуск: 2 | 2024 год

Update background model:

Apply thresholding to obtain binary mask for background subtraction:

м+ =

1 if Ff > т

otherwise

• Perform morphological operations:

M't = morphological operations (Aff)

Example Calculation

Consider two consecutive grayscale frames It and It-i of size 640*480640. Suppose we compute the frame difference and apply a threshold t=20. The background model is updated with a=0.01. The process will involve:

1. Compute the absolute difference between It and

It-i.

2. Apply a threshold to the difference image to obtain a binary mask.

3. Update the background model.

4. Apply background subtraction and thresholding.

5. Perform morphological operations to refine the mask.

This sequence of operations effectively detects moving objects in real-time from the video stream.

Figure 1. UML diagram whole progress

Description 1. VideoCapture Class: o Attributes:

■ video_source: Source of the video (file path or camera index).

■ capture: OpenCV VideoCapture object.

o Methods:

■ _init_(video_source: str):

Initializes the video capture with the provided source.

332

Muhammad al-Xorazmiy nomidagi TATU Farg'ona filiali "Al-Farg'oniy avlodlari" elektron ilmiy jurnali ISSN 2181-4252 Tom: 1 | Son: 2 | 2024-yil

"Descendants of Al-Farghani" electronic scientific journal of Fergana branch of TATU named after Muhammad al-Khorazmi. ISSN 2181-4252 Vol: 1 | Iss: 2 | 2024 year

Электронный научный журнал "Потомки Аль-Фаргани" Ферганского филиала ТАТУ имени Мухаммада аль-Хоразми ISSN 2181-4252 Том: 1 | Выпуск: 2 | 2024 год

■ read_frame() -> np.ndarray: Reads the next frame from the video.

■ release(): Releases the video capture object.

2. FrameProcessor Class:

o Attributes:

■ current_frame: Current frame being processed.

■ previous_frame: Previous frame for frame differencing.

o Methods:

■ _init_(): Initializes the frame

processor.

■ preprocess(frame: np.ndarray): Preprocesses the frame (e.g., grayscale conversion, noise reduction).

■ compute_frame_difference(): Computes the difference between consecutive frames.

■ apply_background_subtraction( ): Applies background subtraction technique.

3. ObjectDetector Class:

o Attributes:

■ contours: List of detected contours.

■ background_subtractor: OpenCV background subtractor object.

o Methods:

■ _init_(): Initializes the object

detector.

■ detect_moving_objects(frame: np.ndarray): Detects moving objects in the frame.

■ draw_contours(frame: np.ndarray): Draws contours around detected objects.

4. Main Class:

o Methods:

iНе можете найти то, что вам нужно? Попробуйте сервис подбора литературы.

■ main(): Main function to run the detection process.

di splay_frame(frame: np.ndarray): Displays processed frame.

the

Results

Below are examples of Python code implementing moving object detection using OpenCV:

1. Background Subtraction:

import cv2

cap = cv2.VideoCapture('video.mp4')

fgbg =

cv2.createBackgroundSubtractorMOG2()

while True:

ret, frame = cap.read() if not ret: break

fgmask = fgbg.apply(frame) cv2.imshow('Frame', fgmask) if cv2.waitKey(30) & 0xFF == 27: break

cap.release()

cv2.destroyAllWindows()

2. Frame Differencing:

import cv2

cap = cv2.VideoCapture('video.mp4')

ret, framel = cap.read()

ret, frame2 = cap.read()

while cap.isOpened():

diff = cv2.absdiff(frame1, frame2) gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)

blur = cv2.GaussianBlur(gray, (5, 5), 0) _, thresh = cv2.threshold(blur, 20, 255, cv2.THRE SH_BINARY)

dilated = cv2.dilate(thresh, None, iterations=3)

333

"Descendants of Al-Farghani" electronic scientific Электронный научный журнал "Потомки Аль-

journal of Fergana branch of TATU named after Фаргани" Ферганского филиала ТАТУ имени

Muhammad al-Khorazmi. ISSN 2181-4252 Мухаммада аль-Хоразми ISSN 2181-4252

Vol: 1 | Iss: 2 | 2024 year Том: 1 | Выпуск: 2 | 2024 год

Muhammad al-Xorazmiy nomidagi TATU Farg'ona filiali "Al-Farg'oniy avlodlari" elektron ilmiy jurnali ISSN 2181-4252 Tom: 1 | Son: 2 | 2024-yil

contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for contour in contours:

if cv2.contourArea(contour) < 700: continue

x, y, w, h = cv2.boundingRect(contour) cv2.rectangle(frame1, (x, y), (x + w, y + h), (0, 255, 0), 2)

cv2.imshow('Frame', framel) frame1 = frame2 ret, frame2 = cap.read() if not ret or cv2.waitKey(30) & 0xFF == 27: break

cap.release()

cv2.destroyAllWindows()

Conclusion

Real-time moving object detection from video streams is a vital technology with applications across various domains. By leveraging Python and libraries like OpenCV, we can implement robust and efficient detection systems. This article has explored traditional methods such as background subtraction and frame differencing, providing practical examples and real-world applications.

The results demonstrate the feasibility of using Python for real-time moving object detection. The code snippets illustrate the implementation process, highlighting the ease with which these techniques can be applied. However, it is essential to recognize the challenges associated with real-time processing, such as handling varying lighting conditions, dynamic backgrounds, and the computational demands of more advanced methods like optical flow and deep learning.

Future research should focus on enhancing the robustness and accuracy of detection algorithms. Integrating machine learning and deep learning techniques can significantly improve performance, especially in complex environments. Moreover, optimizing these algorithms for real-time applications on resource-constrained devices remains a critical area of development.

In conclusion, the field of real-time moving object detection is rapidly advancing, with Python providing a powerful platform for implementation. By continuing to innovate and refine these techniques, we can unlock new possibilities and applications in surveillance, autonomous systems, and beyond.

References

1. Akhundjanov, U., Soliyev, B., Kayumov, A., Kholmatov, A., Musayev, K., & Ermatova, Z. (2024). Distribution of local curvature values as a sign for static signature verification. In E3S Web of Conferences (Vol. 508, p. 03003). EDP Sciences.

2. Zulunov, R., Soliyev, B., Kayumov, A., Asraev, M., Musayev, K., & Abdurasulova, D. (2024, November). Detecting mobile objects with ai using edge detection and background subtraction techniques. In E3S Web of Conferences (Vol. 508, p. 03004). EDP Sciences.

3. Zulunov, R., Akhundjanov, U., Musayev, K., Soliyev, B., Kayumov, A., & Asraev, M. (2024, November). Building and predicting a neural network in python. In E3S Web of Conferences (Vol. 508, p. 04005). EDP Sciences.

4. Nabijonovich S. B., Shuxratjonovich S. R. Diplom ishi mavzularini ro 'yxatini shakillantirish uchun dasturiy ta'minotni joriy etish tahlili //Miasto Przyszlosci. - 2024. - T. 47. - C. 1139-1145.

5. Nabijonovich S. B., Najmiddin G. OPTIMIZING PYQT5 DEVELOPMENT WITH QT DESIGNER //Web of Teachers: Inderscience Research. - 2024. - T. 2. - №. 4. - C. 254-259.

6. Nabijonovich S. B., Kakhramonovna E. Z. RECOGNIZING OBJECTS IN IMAGES USING ARTIFICIAL INTELLIGENCE AND PYTHON //Galaxy International Interdisciplinary Research Journal. - 2023. - T. 11. - №. 11. - C. 88-92.

7. Nabijonovich S. B., Muhammadyo'ldoshali A. Empowering Patient Diagnosis with Advanced Software in Healthcare //Miasto Przyszlosci. -2024. - T. 48. - C. 201-205.

334

Muhammad al-Xorazmiy nomidagi TATU Farg'ona filiali "Al-Farg'oniy avlodlari" elektron ilmiy jurnali ISSN 2181-4252 Tom: 1 | Son: 2 | 2024-yil

"Descendants of Al-Farghani" electronic scientific journal of Fergana branch of TATU named after Muhammad al-Khorazmi. ISSN 2181-4252 Vol: 1 | Iss: 2 | 2024 year

Электронный научный журнал "Потомки Аль-Фаргани" Ферганского филиала ТАТУ имени Мухаммада аль-Хоразми ISSN 2181-4252 Том: 1 | Выпуск: 2 | 2024 год

8. Xudoyberdiyev S., Soliyev B. O 'ZBEKISTONDA SUN'IY INTELLEKT VA RAQAMLI IQTISODIYOT ISTIQBOLLARI //Conference on Digital Innovation:" Modern Problems and Solutions". - 2023.

9. Nabijonovich S. B. EMPOWERING VIDEO ANALYTICS WITH AI-DRIVEN TEXT RECOGNITION IN PYTHON FOR STREAMLINED INSIGHTS //Galaxy International Interdisciplinary Research Journal.

- 2023. - T. 11. - №. 11. - C. 25-30.

10. Soliev B. Coding the Path to E-Commerce Excellence: A Web Programming Odyssey //Journal of technical research and development.

- 2023. - T. 1. - №. 2. - C. 471-475.

11. Soliev B. Python-Powered E-Commerce Solutions in Uzbekistan //Journal of technical research and development. - 2023. - T. 1. - №. 1. - C. 51-54.

12. Soliev B. Python's Role in Revolutionizing ECommerce in Uzbekistan //Journal of technical research and development. - 2023. - T. 1. - №. 1. - C. 51-54.

13. Soliev B. Navigating the E-Commerce Landscape in Uzbekistan with Python //Journal of technical research and development. - 2023. -T. 1. - №. 1. - C. 46-50.

335

i Надоели баннеры? Вы всегда можете отключить рекламу.