Repository navigation
Expand file tree
/
Copy pathdash3.py
More file actions
216 lines (171 loc) · 8.73 KB
/
Copy pathdash3.py
File metadata and controls
216 lines (171 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import cv2
import numpy as np
import os
import time
from datetime import datetime
# Optional: Import pytesseract if available
try:
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
TESSERACT_AVAILABLE = True
except ImportError:
TESSERACT_AVAILABLE = False
print("Warning: pytesseract not found. License plate text recognition will be disabled.")
def create_output_folders():
"""Create folders to store the output in a fixed folder."""
base_dir = "dashcam_analysis" # Fixed folder name
plates_dir = os.path.join(base_dir, "license_plates")
os.makedirs(base_dir, exist_ok=True)
os.makedirs(plates_dir, exist_ok=True)
return base_dir, plates_dir
def analyze_dashcam_video(video_path, sample_rate=5):
"""
Analyze dashcam footage to detect license plates.
Args:
video_path: Path to the video file
sample_rate: Process every nth frame to improve performance
"""
# Try to load a license plate cascade classifier
try:
plate_cascade = cv2.CascadeClassifier('haarcascade_russian_plate_number.xml')
if plate_cascade.empty():
plate_cascade = None
print("License plate cascade not found or empty")
except Exception:
plate_cascade = None
print("Using edge detection for potential license plates")
# Create output folders (fixed folder)
base_dir, plates_dir = create_output_folders()
# Open video file
video = cv2.VideoCapture(video_path)
if not video.isOpened():
print(f"Error: Could not open video file {video_path}")
return None
fps = video.get(cv2.CAP_PROP_FPS)
frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
duration = frame_count / fps
print(f"Video stats: {frame_count} frames, {fps} fps, {duration:.2f} seconds")
print(f"Processing every {sample_rate} frame")
# Create output log file
log_file = os.path.join(base_dir, "analysis_log.txt")
plate_count = 0
frame_number = 0
with open(log_file, 'w') as log:
log.write(f"Dashcam Analysis Log - {datetime.now()}\n")
log.write(f"Video: {video_path}\n")
log.write("=" * 50 + "\n\n")
start_time = time.time()
while video.isOpened():
ret, frame = video.read()
if not ret:
break
frame_number += 1
if frame_number % sample_rate != 0:
continue
timestamp = frame_number / fps
elapsed = time.time() - start_time
if frame_number % 10 == 0:
print(f"\rProcessing frame {frame_number}/{frame_count} - {timestamp:.2f}s - Elapsed: {elapsed:.2f}s", end="")
# Create a copy for visualization
display_frame = frame.copy()
# Resize frame for faster processing (keep display frame original size)
frame_small = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)
height, width, _ = frame_small.shape
# Convert to grayscale for detection
gray = cv2.cvtColor(frame_small, cv2.COLOR_BGR2GRAY)
# License plate detection
if plate_cascade is not None:
# Use Haar Cascade for license plate detection
plates = plate_cascade.detectMultiScale(gray, 1.1, 5)
for (x, y, w, h) in plates:
# Adjust coordinates for the original frame
x_orig, y_orig, w_orig, h_orig = x*2, y*2, w*2, h*2
# Extract license plate image from original frame
plate_img = frame[y_orig:y_orig+h_orig, x_orig:x_orig+w_orig]
if process_license_plate(plate_img, display_frame, x_orig, y_orig, w_orig, h_orig,
frame_number, timestamp, plates_dir, plate_count, log):
plate_count += 1
else:
# Fallback method using edge detection to find potential license plates
edges = cv2.Canny(gray, 100, 200)
contours, _ = cv2.findContours(edges.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
area = cv2.contourArea(contour)
if area < 1000 or area > 10000:
continue
x, y, w, h = cv2.boundingRect(contour)
aspect_ratio = float(w) / h
if 1.5 <= aspect_ratio <= 5.0:
x_orig, y_orig, w_orig, h_orig = x*2, y*2, w*2, h*2
plate_img = frame[y_orig:y_orig+h_orig, x_orig:x_orig+w_orig]
if process_license_plate(plate_img, display_frame, x_orig, y_orig, w_orig, h_orig,
frame_number, timestamp, plates_dir, plate_count, log,
is_potential=True):
plate_count += 1
# Save the annotated frame every 10th processed frame
if frame_number % 10 == 0:
output_frame = os.path.join(base_dir, f"frame_{frame_number}.jpg")
cv2.imwrite(output_frame, display_frame)
log.write("\n" + "=" * 50 + "\n")
log.write(f"Analysis completed: {plate_count} license plates detected\n")
video.release()
print(f"\nAnalysis completed: {plate_count} license plates detected")
print(f"Results saved to {base_dir}")
return base_dir
def process_license_plate(plate_img, display_frame, x, y, w, h, frame_number, timestamp,
plates_dir, plate_count, log, is_potential=False):
"""Process and save a detected license plate."""
if plate_img.size == 0:
return False
valid_plate = False
plate_text = "Unknown"
if TESSERACT_AVAILABLE:
try:
gray_plate = cv2.cvtColor(plate_img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray_plate, 150, 255, cv2.THRESH_BINARY)
plate_text = pytesseract.image_to_string(thresh, config='--psm 8 --oem 3 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
plate_text = plate_text.strip()
has_letter = any(c.isalpha() for c in plate_text)
has_number = any(c.isdigit() for c in plate_text)
valid_plate = (len(plate_text) >= 2) and has_letter and has_number
if not valid_plate:
if not is_potential:
log.write(f"License plate candidate filtered - insufficient alphanumeric characters: '{plate_text}' at frame {frame_number}\n")
return False
except Exception as e:
plate_text = f"Error: {str(e)}"
log.write(f"OCR error at frame {frame_number}: {str(e)}\n")
valid_plate = True
else:
gray_plate = cv2.cvtColor(plate_img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray_plate, 100, 200)
edge_pixels = cv2.countNonZero(edges)
plate_area = plate_img.shape[0] * plate_img.shape[1]
edge_density = edge_pixels / plate_area if plate_area > 0 else 0
valid_plate = edge_density > 0.05
if not valid_plate:
if not is_potential:
log.write(f"License plate candidate filtered - insufficient edge features at frame {frame_number}\n")
return False
plate_file = os.path.join(plates_dir, f"plate_{plate_count}_{frame_number}.jpg")
cv2.imwrite(plate_file, plate_img)
color = (0, 0, 255) if is_potential else (255, 0, 0)
cv2.rectangle(display_frame, (x, y), (x+w, y+h), color, 2)
if plate_text != "Unknown":
cv2.putText(display_frame, plate_text, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
prefix = "Potential " if is_potential else ""
log.write(f"{prefix}License plate detected at frame {frame_number} ({timestamp:.2f}s)\n")
if plate_text != "Unknown":
log.write(f" Text: {plate_text}\n")
log.write(f" Saved to: {plate_file}\n")
return True
if __name__ == "__main__":
video_path = "carplates.mp4" # Change this to your video file path
sample_rate = 2 # Change this value if needed
print(f"Starting analysis of video: {video_path}")
print(f"Processing every {sample_rate} frame")
result_dir = analyze_dashcam_video(video_path, sample_rate)
if result_dir:
print(f"Analysis complete! Results saved to: {result_dir}")
else:
print("Analysis failed. Please check the error messages above.")