Spaces:
Sleeping
Sleeping
Samanta Das
commited on
Create helpers.py
Browse files- helpers.py +65 -0
helpers.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
import cv2 # Ensure OpenCV is installed
|
5 |
+
|
6 |
+
# Set up logging configuration
|
7 |
+
logging.basicConfig(level=logging.INFO, filename='image_processing.log', filemode='a')
|
8 |
+
|
9 |
+
def load_image(image_path):
|
10 |
+
"""Load an image from the specified path."""
|
11 |
+
try:
|
12 |
+
image = Image.open(image_path)
|
13 |
+
logging.info(f"Image loaded successfully from {image_path}.")
|
14 |
+
return image
|
15 |
+
except Exception as e:
|
16 |
+
logging.error(f"Failed to load image from {image_path}: {e}")
|
17 |
+
return None
|
18 |
+
|
19 |
+
def preprocess_image(image, target_size=(640, 640)):
|
20 |
+
"""Preprocess the image for YOLO model."""
|
21 |
+
try:
|
22 |
+
# Resize and convert to RGB
|
23 |
+
image = image.resize(target_size)
|
24 |
+
image = image.convert("RGB")
|
25 |
+
logging.info(f"Image preprocessed to size {target_size}.")
|
26 |
+
return np.array(image) / 255.0 # Normalize to [0, 1]
|
27 |
+
except Exception as e:
|
28 |
+
logging.error(f"Error in preprocessing image: {e}")
|
29 |
+
return None
|
30 |
+
|
31 |
+
def draw_bounding_boxes(image, detections):
|
32 |
+
"""Draw bounding boxes on the image based on YOLO detections."""
|
33 |
+
try:
|
34 |
+
image = np.array(image)
|
35 |
+
for detection in detections:
|
36 |
+
x1, y1, x2, y2 = map(int, detection['coordinates']) # Ensure coordinates are integers
|
37 |
+
label = detection.get('name', 'Unknown')
|
38 |
+
color = (255, 0, 0) # Red color for bounding boxes
|
39 |
+
|
40 |
+
# Draw rectangle
|
41 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
|
42 |
+
|
43 |
+
# Draw label
|
44 |
+
cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
45 |
+
|
46 |
+
logging.info("Bounding boxes drawn on the image.")
|
47 |
+
return Image.fromarray(image)
|
48 |
+
except Exception as e:
|
49 |
+
logging.error(f"Error in drawing bounding boxes: {e}")
|
50 |
+
return image
|
51 |
+
|
52 |
+
def format_detection_output(detections):
|
53 |
+
"""Format the YOLO detection output for reporting."""
|
54 |
+
formatted_detections = []
|
55 |
+
for detection in detections:
|
56 |
+
formatted_detection = {
|
57 |
+
"name": detection.get("name", "Unknown"),
|
58 |
+
"class": detection.get("class", "Unknown"),
|
59 |
+
"confidence": round(detection.get("confidence", 0.0), 2),
|
60 |
+
"coordinates": detection.get("coordinates", [])
|
61 |
+
}
|
62 |
+
formatted_detections.append(formatted_detection)
|
63 |
+
|
64 |
+
logging.info("Detection output formatted successfully.")
|
65 |
+
return formatted_detections
|