Upload 5 files
Browse files- src/__init__.py +0 -0
- src/exception.py +50 -0
- src/logger.py +21 -0
- src/yolo/__init__.py +0 -0
- src/yolo/predict_pose.py +68 -0
src/__init__.py
ADDED
File without changes
|
src/exception.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
This module defines a custom exception handling class and a function to get error message with details of the error.
|
3 |
+
"""
|
4 |
+
|
5 |
+
# Standard Library
|
6 |
+
import sys
|
7 |
+
|
8 |
+
# Local imports
|
9 |
+
from src.logger import logging
|
10 |
+
|
11 |
+
|
12 |
+
# Function Definition to get error message with details of the error (file name and line number) when an error occurs in the program
|
13 |
+
def get_error_message(error, error_detail: sys):
|
14 |
+
"""
|
15 |
+
Get error message with details of the error.
|
16 |
+
|
17 |
+
Args:
|
18 |
+
- error (Exception): The error that occurred.
|
19 |
+
- error_detail (sys): The details of the error.
|
20 |
+
|
21 |
+
Returns:
|
22 |
+
str: A string containing the error message along with the file name and line number where the error occurred.
|
23 |
+
"""
|
24 |
+
_, _, exc_tb = error_detail.exc_info()
|
25 |
+
|
26 |
+
# Get error details
|
27 |
+
file_name = exc_tb.tb_frame.f_code.co_filename
|
28 |
+
return "Error occured in python script name [{0}] line number [{1}] error message[{2}]".format(
|
29 |
+
file_name, exc_tb.tb_lineno, str(error)
|
30 |
+
)
|
31 |
+
|
32 |
+
|
33 |
+
# Custom Exception Handling Class Definition
|
34 |
+
class CustomExceptionHandling(Exception):
|
35 |
+
"""
|
36 |
+
Custom Exception Handling:
|
37 |
+
This class defines a custom exception that can be raised when an error occurs in the program.
|
38 |
+
It takes an error message and an error detail as input and returns a formatted error message when the exception is raised.
|
39 |
+
"""
|
40 |
+
|
41 |
+
# Constructor
|
42 |
+
def __init__(self, error_message, error_detail: sys):
|
43 |
+
"""Initialize the exception"""
|
44 |
+
super().__init__(error_message)
|
45 |
+
|
46 |
+
self.error_message = get_error_message(error_message, error_detail=error_detail)
|
47 |
+
|
48 |
+
def __str__(self):
|
49 |
+
"""String representation of the exception"""
|
50 |
+
return self.error_message
|
src/logger.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Importing the required modules
|
2 |
+
import os
|
3 |
+
import logging
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
# Creating a log file with the current date and time as the name of the file
|
7 |
+
LOG_FILE = f"{datetime.now().strftime('%m_%d_%Y_%H_%M_%S')}.log"
|
8 |
+
|
9 |
+
# Creating a logs folder if it does not exist
|
10 |
+
logs_path = os.path.join(os.getcwd(), "logs", LOG_FILE)
|
11 |
+
os.makedirs(logs_path, exist_ok=True)
|
12 |
+
|
13 |
+
# Setting the log file path and the log level
|
14 |
+
LOG_FILE_PATH = os.path.join(logs_path, LOG_FILE)
|
15 |
+
|
16 |
+
# Configuring the logger
|
17 |
+
logging.basicConfig(
|
18 |
+
filename=LOG_FILE_PATH,
|
19 |
+
format="[ %(asctime)s ] %(lineno)d %(name)s - %(levelname)s - %(message)s",
|
20 |
+
level=logging.INFO,
|
21 |
+
)
|
src/yolo/__init__.py
ADDED
File without changes
|
src/yolo/predict_pose.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
import PIL.Image as Image
|
3 |
+
from ultralytics import YOLO
|
4 |
+
import gradio as gr
|
5 |
+
import spaces
|
6 |
+
|
7 |
+
# Local imports
|
8 |
+
from src.logger import logging
|
9 |
+
from src.exception import CustomExceptionHandling
|
10 |
+
|
11 |
+
|
12 |
+
@spaces.GPU
|
13 |
+
def predict_pose(
|
14 |
+
img: str,
|
15 |
+
conf_threshold: float,
|
16 |
+
iou_threshold: float,
|
17 |
+
max_detections: int,
|
18 |
+
model_name: str,
|
19 |
+
) -> Image.Image:
|
20 |
+
"""
|
21 |
+
Predicts objects in an image using a YOLOv8 model with adjustable confidence and IOU thresholds.
|
22 |
+
|
23 |
+
Args:
|
24 |
+
- img (str or numpy.ndarray): The input image or path to the image file.
|
25 |
+
- conf_threshold (float): The confidence threshold for object detection.
|
26 |
+
- iou_threshold (float): The Intersection Over Union (IOU) threshold for non-max suppression.
|
27 |
+
- max_detections (int): The maximum number of detections allowed.
|
28 |
+
- model_name (str): The name or path of the YOLOv8 model to be used for prediction.
|
29 |
+
|
30 |
+
Returns:
|
31 |
+
PIL.Image.Image: The image with predicted objects plotted on it.
|
32 |
+
"""
|
33 |
+
try:
|
34 |
+
# Check if image is None
|
35 |
+
if img is None:
|
36 |
+
gr.Warning("Please provide an image.")
|
37 |
+
|
38 |
+
# Load the YOLO model
|
39 |
+
model = YOLO(model_name)
|
40 |
+
|
41 |
+
# Predict objects in the image
|
42 |
+
results = model.predict(
|
43 |
+
source=img,
|
44 |
+
conf=conf_threshold,
|
45 |
+
iou=iou_threshold,
|
46 |
+
max_det=max_detections,
|
47 |
+
show_labels=True,
|
48 |
+
show_conf=True,
|
49 |
+
imgsz=640,
|
50 |
+
half=True,
|
51 |
+
device="cuda:0",
|
52 |
+
)
|
53 |
+
|
54 |
+
# Plot the predicted objects on the image
|
55 |
+
for r in results:
|
56 |
+
im_array = r.plot()
|
57 |
+
im = Image.fromarray(im_array[..., ::-1])
|
58 |
+
|
59 |
+
# Log the successful prediction
|
60 |
+
logging.info("Pose estimated successfully.")
|
61 |
+
|
62 |
+
# Return the image
|
63 |
+
return im
|
64 |
+
|
65 |
+
# Handle exceptions that may occur during the process
|
66 |
+
except Exception as e:
|
67 |
+
# Custom exception handling
|
68 |
+
raise CustomExceptionHandling(e, sys) from e
|