Giter Club home page Giter Club logo

hayatiyrtgl / shooting_simulator_python Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 429 KB

Overall, this project provides a flexible framework for implementing computer vision applications with a focus on object detection, tracking, and point scoring. Its versatility and modularity make it suitable for a wide range of use cases across different industries and domains.

License: MIT License

Python 100.00%
image-processing opencv python-script simulation target-tracking

shooting_simulator_python's Introduction

How to Use the Project:

  1. Setup:

    • Clone the GitHub repository containing the project files.
    • Ensure you have Python and necessary dependencies (OpenCV, NumPy) installed.
  2. Understand the Components:

    • Familiarize yourself with the provided code files (detection.py, masking.py, contours_detection.py, point_calculator.py).
    • Understand the functionalities of each component: object detection, background masking, contour detection, and point calculation.
  3. Customize or Extend:

    • If needed, modify the code to fit your specific use case. For example, you might want to detect different objects or use different contour shapes for point calculation.
  4. Run the Application:

    • Execute the main script (e.g., main.py or main_loop function in one of the files) to start the application.
    • Ensure your webcam is connected and functioning properly.
  5. Interact with the Application:

    • The application should start capturing video from your webcam.
    • It will perform object detection, background subtraction, contour detection, and point calculation in real-time.
    • You can interact with the application through the graphical user interface provided by OpenCV's window(s).
  6. Monitor Output:

    • Monitor the application's output to observe the detected objects, tracked points, or any other relevant information.
    • This output could be useful for various purposes such as surveillance, motion tracking, or interactive installations.

Why Use This Project:

  1. Object Detection:

    • You can use this project for real-time object detection, which could be valuable for security surveillance, object tracking, or augmented reality applications.
  2. Background Subtraction:

    • Background subtraction is a common preprocessing step in computer vision tasks. This project provides a robust implementation for foreground extraction, useful in scenarios like motion detection, tracking, or segmentation.
  3. Contour Detection:

    • Contour detection is useful for identifying object boundaries. This can be applied in various fields like medical imaging, industrial automation, or robotics for object manipulation.
  4. Point Calculation:

    • The ability to calculate points based on contours can be used for various scoring or analysis tasks. For example, in sports analytics, tracking player movements and determining their positions relative to specific points on the field.
  5. Educational Purposes:

    • This project can serve as a learning resource for understanding fundamental computer vision concepts and their practical implementation using OpenCV and Python.

Overall, this project provides a framework for building custom computer vision applications, offering flexibility for adaptation to different use cases and scenarios. Whether you're a researcher, developer, or hobbyist, this project can be a valuable tool in your computer vision toolkit.


Benefits and Advantages:

Real-time Processing: The project provides real-time processing capabilities, making it suitable for applications requiring timely responses.

Modularity: The project is modular, allowing for easy integration and customization to fit different use cases and environments.

Scalability: The project can be scaled to handle larger datasets or more complex tasks by optimizing algorithms or deploying on more powerful hardware.

Versatility: The project can be adapted for various applications ranging from simple object detection to more sophisticated tasks like gesture recognition or augmented reality.


Modules


This Python script is a part of a computer vision project for video processing, likely aimed at some sort of object detection or tracking application.

  1. Import Statements:

    • cv2: OpenCV library for computer vision tasks.
    • numpy as np: NumPy library for numerical computations.
    • DetectionClass, BackgroundMasking, and contours: These are modules or classes imported from separate Python files.
  2. MainClass Definition:

    • MainClass is defined, initializing several components.
    • __init__ method:
      • Initializes an instance of DetectionClass for object detection.
      • Calls detect_and_extract method from DetectionClass to obtain coordinates.
      • Releases the video capture from DetectionClass.
      • Initializes an instance of BackgroundMasking.
      • Initializes a video capture object from the webcam (assuming camera index 0).
  3. Main Loop (main_loop method):

    • Enters an infinite loop for continuous video processing.
    • Captures a frame from the webcam feed.
    • Resizes the frame to a standard size of 640x480 pixels.
    • Defines source and destination points for perspective transformation.
    • Calculates the perspective transformation matrix using cv2.getPerspectiveTransform.
    • Applies the perspective transformation to the frame using cv2.warpPerspective.
    • Applies background subtraction using the background_subtracted method from BackgroundMasking to get a background-masked frame.
    • Calls the contours function to detect contours on the background-masked frame.
    • If contours are detected, it prints the coordinates of the contours.
    • Displays the perspective-transformed frame in a window named "frame".
    • Breaks the loop if the "Esc" key (27) is pressed.
    • Releases the video capture object and closes all OpenCV windows when the loop exits.
  4. Main Execution Block:

    • Instantiates MainClass.
    • Calls the main_loop method to start the video processing loop.

This script is processing a video stream from a webcam, applying perspective transformation, background subtraction, and contour detection. It's likely being used for some sort of object tracking or detection application.


DetectionClass that is responsible for detecting and extracting a specific target image from a video stream captured by a webcam. Let's break down the code:

  1. Import Statements:

    • cv2: OpenCV library for computer vision tasks.
    • numpy as np: NumPy library for numerical computations.
  2. DetectionClass Definition:

    • Initializes with an image read from file ("targets/head_target.jpg") and pre-processes it by applying Gaussian blur.
    • Sets up a video capture object from the webcam (cv2.VideoCapture(0)).
    • Initializes a SIFT (Scale-Invariant Feature Transform) object detector (cv2.SIFT.create()).
    • Computes key points (kp_image) and descriptors (desc_image) for the target image.
    • Sets up parameters for FLANN (Fast Library for Approximate Nearest Neighbors) matching algorithm.
    • Initializes FLANN matcher (cv2.FlannBasedMatcher).
    • Initializes an empty list self.coordinates to store the coordinates of the detected target.
  3. detect_and_extract Method:

    • Enters an infinite loop to continuously capture frames from the webcam.
    • Converts the captured frame to grayscale.
    • Detects key points (kp_frame) and descriptors (desc_frame) in the grayscale frame.
    • Matches descriptors between the target image and the frame using FLANN matcher.
    • Filters and selects good matches based on Lowe's ratio test.
    • If enough good matches are found:
      • Computes homography matrix (matrix) using RANSAC algorithm.
      • Performs perspective transformation on the corners of the target image.
      • Draws a polygon around the detected target on the frame.
      • Appends the coordinates of the detected target to self.coordinates.
      • If 10 sets of coordinates are collected, returns the first set and exits the loop.
    • Handles exceptions such as camera instability.
    • Releases the camera and closes all OpenCV windows when the loop exits.

This class essentially performs object detection using SIFT features and homography estimation to locate the target image in a video stream. It's part of a larger system likely used for tracking or recognition applications.


BackgroundMasking responsible for background subtraction in a video stream. Here's a breakdown of the code:

  1. Import Statements:

    • cv2: OpenCV library for computer vision tasks.
    • numpy as np: NumPy library for numerical computations.
  2. BackgroundMasking Class Definition:

    • Initializes with the creation of a MOG2 (Mixture of Gaussians) background subtractor using cv2.createBackgroundSubtractorMOG2.
    • Parameters passed to createBackgroundSubtractorMOG2:
      • history: Length of the history, or how many previous frames are considered for background modeling.
      • varThreshold: Threshold on the squared Mahalanobis distance to decide whether it is well-described by the background model. Lower values mean higher sensitivity to detect moving objects.
      • detectShadows: Boolean flag indicating whether to detect shadows or not. If set to False, shadows will not be detected.
  3. background_subtracted Method:

    • Takes a single parameter frame, which represents the input frame from the video stream.
    • Applies the background subtractor (self.fgbg) to the input frame using the apply method, which calculates the foreground mask (fgmask).
    • Returns the foreground mask, which represents the parts of the frame where the background has been subtracted, leaving only the foreground objects.

This class essentially provides a method to perform background subtraction, which is a common preprocessing step in various computer vision tasks such as object detection, tracking, and motion analysis.


calculate_points that is responsible for determining the point associated with a given coordinate (x, y) based on the contours stored in the numpy files loaded at the beginning of the script (p7, p8, p9, p10). Let's break down the code:

  1. Import Statements:

    • cv2: OpenCV library for computer vision tasks.
    • numpy as np: NumPy library for numerical computations.
  2. Loading Contour Points:

    • Loads contour points from numpy files ("target_contours/head/7p.npy", "target_contours/head/8p.npy", etc.) into variables p7, p8, p9, and p10.
  3. calculate_points Function:

    • Takes two parameters, x and y, representing the coordinates of a point.
    • Initializes the puan variable with the default value '6', representing the lowest point score.
    • Creates a tuple coord with the input coordinates (x, y).
    • Iterates through each set of contour points (p7, p8, p9, p10) to determine if the given coordinates lie within any of these contours.
    • Uses cv2.pointPolygonTest to check if the point lies inside the contour. If it does, updates puan accordingly.
    • Returns the determined point score (puan).

This function essentially maps a given coordinate to a specific point score (6, 7, 8, 9, or 10) based on the contours defined in the numpy files. It's likely used in a larger system for some sort of point-based analysis or scoring.


This Python code defines a function named contours responsible for finding contours in an input frame (get_frame) and calculating points associated with those contours using the calculate_points function. Let's break down the code:

  1. Import Statement:

    • cv2: OpenCV library for computer vision tasks.
  2. Function Definition:

    • contours(get_frame, write_frame): This function takes two parameters:
      • get_frame: Input frame containing contours.
      • write_frame: Output frame where contours and centroid points are drawn.
  3. Finding Contours:

    • Uses cv2.findContours to find contours in the input frame (get_frame).
    • The contours are retrieved with the RETR_EXTERNAL retrieval mode, meaning only the external contours are retrieved.
    • Contours are approximated using the CHAIN_APPROX_SIMPLE method.
  4. Contour Processing:

    • Iterates through each contour found.
    • If the contour area is greater than 1000 pixels:
      • Calculates the centroid of the contour using cv2.moments.
      • Draws a green circle at the centroid on the output frame (write_frame).
  5. Point Calculation:

    • Calls the calculate_points function to determine the point associated with the centroid of the contour.
    • Passes the centroid coordinates (cX, cY) to the calculate_points function.
    • Returns the point variable calculated by calculate_points.

This function essentially performs contour detection and centroid calculation, then calculates the associated point score using the calculate_points function based on the centroid coordinates. The point score is determined based on predefined contours, likely loaded from external files.

shooting_simulator_python's People

Contributors

hayatiyrtgl avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.