Update app.py
Browse files
app.py
CHANGED
|
@@ -25,63 +25,135 @@ class_names = {0: 'With Helmet', 1: 'Without Helmet', 2: 'License Plate'}
|
|
| 25 |
def crop_license_plates(image, detections):
|
| 26 |
cropped_plates = []
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
for detection in detections:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
dims = detection['Dimensions']
|
| 39 |
-
width, height = map(int, dims.split('x'))
|
| 40 |
x2, y2 = x1 + width, y1 + height
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
cropped_plate = image.crop((x1, y1, x2, y2))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
cropped_plates.append({
|
| 50 |
'image': cropped_plate,
|
| 51 |
'confidence': detection['Confidence'],
|
| 52 |
-
'position': detection['Position']
|
|
|
|
| 53 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
return cropped_plates
|
| 56 |
|
| 57 |
def create_download_files(annotated_image, cropped_plates, detections):
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
if
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
def yoloV8_func(
|
| 87 |
image=None,
|
|
@@ -137,15 +209,19 @@ def yoloV8_func(
|
|
| 137 |
download_files = None
|
| 138 |
|
| 139 |
if crop_plates and detections:
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
download_files, _, _ = create_download_files(annotated_image, cropped_plates, detections)
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
# Create stats text
|
| 151 |
stats_text = ""
|
|
|
|
| 25 |
def crop_license_plates(image, detections):
|
| 26 |
cropped_plates = []
|
| 27 |
|
| 28 |
+
try:
|
| 29 |
+
if isinstance(image, str):
|
| 30 |
+
if not os.path.exists(image):
|
| 31 |
+
print(f"Error: Image file not found: {image}")
|
| 32 |
+
return cropped_plates
|
| 33 |
+
image = Image.open(image)
|
| 34 |
+
elif isinstance(image, np.ndarray):
|
| 35 |
+
image = Image.fromarray(image)
|
| 36 |
+
elif not isinstance(image, Image.Image):
|
| 37 |
+
print(f"Error: Unsupported image type: {type(image)}")
|
| 38 |
+
return cropped_plates
|
| 39 |
+
|
| 40 |
+
if image.size[0] == 0 or image.size[1] == 0:
|
| 41 |
+
print("Error: Image has zero dimensions")
|
| 42 |
+
return cropped_plates
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"Error loading image: {e}")
|
| 46 |
+
return cropped_plates
|
| 47 |
|
| 48 |
+
for i, detection in enumerate(detections):
|
| 49 |
+
try:
|
| 50 |
+
if detection['Object'] != 'License Plate':
|
| 51 |
+
continue
|
| 52 |
+
|
| 53 |
+
pos_str = detection['Position'].strip('()')
|
| 54 |
+
if ',' not in pos_str:
|
| 55 |
+
print(f"Error: Invalid position format for detection {i}: {detection['Position']}")
|
| 56 |
+
continue
|
| 57 |
+
|
| 58 |
+
x1, y1 = map(int, pos_str.split(', '))
|
| 59 |
+
|
| 60 |
+
dims_str = detection['Dimensions']
|
| 61 |
+
if 'x' not in dims_str:
|
| 62 |
+
print(f"Error: Invalid dimensions format for detection {i}: {detection['Dimensions']}")
|
| 63 |
+
continue
|
| 64 |
+
|
| 65 |
+
width, height = map(int, dims_str.split('x'))
|
| 66 |
+
|
| 67 |
+
if width <= 0 or height <= 0:
|
| 68 |
+
print(f"Error: Invalid dimensions for detection {i}: {width}x{height}")
|
| 69 |
+
continue
|
| 70 |
|
|
|
|
|
|
|
| 71 |
x2, y2 = x1 + width, y1 + height
|
| 72 |
|
| 73 |
+
if x1 < 0 or y1 < 0 or x2 > image.width or y2 > image.height:
|
| 74 |
+
print(f"Warning: Bounding box extends beyond image boundaries for detection {i}")
|
| 75 |
+
x1 = max(0, x1)
|
| 76 |
+
y1 = max(0, y1)
|
| 77 |
+
x2 = min(image.width, x2)
|
| 78 |
+
y2 = min(image.height, y2)
|
| 79 |
+
|
| 80 |
+
if x2 <= x1 or y2 <= y1:
|
| 81 |
+
print(f"Error: Invalid crop coordinates for detection {i}: ({x1},{y1}) to ({x2},{y2})")
|
| 82 |
+
continue
|
| 83 |
|
| 84 |
cropped_plate = image.crop((x1, y1, x2, y2))
|
| 85 |
+
|
| 86 |
+
if cropped_plate.size[0] == 0 or cropped_plate.size[1] == 0:
|
| 87 |
+
print(f"Error: Cropped image has zero dimensions for detection {i}")
|
| 88 |
+
continue
|
| 89 |
+
|
| 90 |
cropped_plates.append({
|
| 91 |
'image': cropped_plate,
|
| 92 |
'confidence': detection['Confidence'],
|
| 93 |
+
'position': detection['Position'],
|
| 94 |
+
'crop_coords': f"({x1},{y1}) to ({x2},{y2})"
|
| 95 |
})
|
| 96 |
+
|
| 97 |
+
except ValueError as e:
|
| 98 |
+
print(f"Error parsing coordinates for detection {i}: {e}")
|
| 99 |
+
continue
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print(f"Error cropping license plate {i}: {e}")
|
| 102 |
+
continue
|
| 103 |
|
| 104 |
return cropped_plates
|
| 105 |
|
| 106 |
def create_download_files(annotated_image, cropped_plates, detections):
|
| 107 |
+
try:
|
| 108 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 109 |
+
|
| 110 |
+
os.makedirs("temp", exist_ok=True)
|
| 111 |
+
|
| 112 |
+
annotated_path = f"temp/annotated_image_{timestamp}.jpg"
|
| 113 |
+
try:
|
| 114 |
+
annotated_image.save(annotated_path, quality=95)
|
| 115 |
+
except Exception as e:
|
| 116 |
+
print(f"Error saving annotated image: {e}")
|
| 117 |
+
return None, None, []
|
| 118 |
+
|
| 119 |
+
plate_paths = []
|
| 120 |
+
for i, plate_data in enumerate(cropped_plates):
|
| 121 |
+
try:
|
| 122 |
+
plate_path = f"temp/license_plate_{i+1}_{timestamp}.jpg"
|
| 123 |
+
plate_data['image'].save(plate_path, quality=95)
|
| 124 |
+
plate_paths.append(plate_path)
|
| 125 |
+
except Exception as e:
|
| 126 |
+
print(f"Error saving license plate {i+1}: {e}")
|
| 127 |
+
continue
|
| 128 |
+
|
| 129 |
+
report_path = f"temp/detection_report_{timestamp}.csv"
|
| 130 |
+
if detections:
|
| 131 |
+
try:
|
| 132 |
+
df = pd.DataFrame(detections)
|
| 133 |
+
df.to_csv(report_path, index=False)
|
| 134 |
+
except Exception as e:
|
| 135 |
+
print(f"Error creating detection report: {e}")
|
| 136 |
+
report_path = None
|
| 137 |
+
|
| 138 |
+
zip_path = f"temp/detection_results_{timestamp}.zip"
|
| 139 |
+
try:
|
| 140 |
+
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 141 |
+
if os.path.exists(annotated_path):
|
| 142 |
+
zipf.write(annotated_path, f"annotated_image_{timestamp}.jpg")
|
| 143 |
+
for plate_path in plate_paths:
|
| 144 |
+
if os.path.exists(plate_path):
|
| 145 |
+
zipf.write(plate_path, os.path.basename(plate_path))
|
| 146 |
+
if report_path and os.path.exists(report_path):
|
| 147 |
+
zipf.write(report_path, f"detection_report_{timestamp}.csv")
|
| 148 |
+
except Exception as e:
|
| 149 |
+
print(f"Error creating ZIP file: {e}")
|
| 150 |
+
return None, annotated_path, plate_paths
|
| 151 |
+
|
| 152 |
+
return zip_path, annotated_path, plate_paths
|
| 153 |
+
|
| 154 |
+
except Exception as e:
|
| 155 |
+
print(f"Error in create_download_files: {e}")
|
| 156 |
+
return None, None, []
|
| 157 |
|
| 158 |
def yoloV8_func(
|
| 159 |
image=None,
|
|
|
|
| 209 |
download_files = None
|
| 210 |
|
| 211 |
if crop_plates and detections:
|
| 212 |
+
try:
|
| 213 |
+
cropped_plates = crop_license_plates(image, detections)
|
| 214 |
+
license_plate_gallery = [plate_data['image'] for plate_data in cropped_plates]
|
| 215 |
+
|
| 216 |
+
if cropped_plates or detections:
|
| 217 |
download_files, _, _ = create_download_files(annotated_image, cropped_plates, detections)
|
| 218 |
+
if download_files is None:
|
| 219 |
+
print("Warning: Could not create download files")
|
| 220 |
+
except Exception as e:
|
| 221 |
+
print(f"Error in license plate processing: {e}")
|
| 222 |
+
cropped_plates = []
|
| 223 |
+
license_plate_gallery = []
|
| 224 |
+
download_files = None
|
| 225 |
|
| 226 |
# Create stats text
|
| 227 |
stats_text = ""
|