-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recognition.py
60 lines (51 loc) · 2.34 KB
/
Recognition.py
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
import cv2
import pytesseract
# Set up pytesseract path (customize the path if necessary)
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
# Load the Haar Cascade for license plate detection
cascade_path = "haarcascade_russian_plate_number.xml"
license_plate_cascade = cv2.CascadeClassifier(cascade_path)
def preprocess_image(image):
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return gray
def detect_license_plate(image, cascade):
# Detect license plates
plates = cascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=4, minSize=(30, 30))
return plates
def recognize_characters(license_plate_image):
# Convert to grayscale (if not already)
gray_license_plate = cv2.cvtColor(license_plate_image, cv2.COLOR_BGR2GRAY)
# Apply adaptive thresholding
_, thresh_license_plate = cv2.threshold(gray_license_plate, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# OCR recognition with custom configuration
config = r'--oem 3 --psm 7 -l eng' # Adjust language if needed
text = pytesseract.image_to_string(thresh_license_plate, config=config)
return text
def main(image_path):
# Load image
image = cv2.imread(image_path)
if image is None:
print("Error: Unable to load image.")
return
# Preprocess the image
gray_image = preprocess_image(image)
# Detect license plates
plates = detect_license_plate(gray_image, license_plate_cascade)
if len(plates) > 0:
for (x, y, w, h) in plates:
# Extract license plate
license_plate_image = image[y:y + h, x:x + w]
# Recognize characters on the license plate
license_plate_text = recognize_characters(license_plate_image)
print(f"License Plate Text: {license_plate_text}")
# Draw rectangle around the detected license plate and display the result
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("License Plate Detection", image)
cv2.imshow("License Plate", license_plate_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print("No license plate detected.")
if __name__ == "__main__":
main(r"images\171148768666033ac6b2802_nu74-reg.jpg") # Use a raw string (prefix with 'r') to handle backslashes in the path