| Server IP : 202.61.199.114 / Your IP : 216.73.217.139 Web Server : nginx/1.22.1 System : Linux de.arni-solutions.de 6.1.0-49-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.174-1 (2026-05-26) x86_64 User : web20 ( 1018) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/shit/ |
Upload File : |
import cv2
import pytesseract
import re
import sys
import os
pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
def detect_plate_number(image_path):
image = cv2.imread(image_path)
if image is None:
print(f"Fehler: Datei '{image_path}' konnte nicht geladen werden.")
return None
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# Kanten finden
edges = cv2.Canny(blurred, 100, 200)
contours, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)
plate_contour = None
for contour in contours:
epsilon = 0.02 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)
if len(approx) == 4: # Rechteck gefunden
plate_contour = approx
break
if plate_contour is not None:
x, y, w, h = cv2.boundingRect(plate_contour)
plate_image = gray[y:y + h, x:x + w]
# --- Linken Rand (EU-Bereich) abschneiden ---
cut_x = int(w * 0.15)
plate_image = plate_image[:, cut_x:]
# --- Vergrößern ---
plate_image = cv2.resize(plate_image, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
# --- Threshold-Varianten ---
_, thresh_otsu = cv2.threshold(plate_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
thresh_adapt = cv2.adaptiveThreshold(
plate_image, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,
11, 2
)
# --- Zwischenschritte speichern ---
base_name = os.path.splitext(os.path.basename(image_path))[0]
cv2.imwrite(f"{base_name}_plate_otsu.png", thresh_otsu)
cv2.imwrite(f"{base_name}_plate_adapt.png", thresh_adapt)
print(f"Zwischenschritte gespeichert: {base_name}_plate_otsu.png, {base_name}_plate_adapt.png")
# --- OCR Settings ---
configs = {
"otsu_psm7": ("--psm 7 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ", thresh_otsu),
"otsu_psm8": ("--psm 8 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ", thresh_otsu),
"adapt_psm7": ("--psm 7 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ", thresh_adapt),
"adapt_psm8": ("--psm 8 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ", thresh_adapt),
}
results = {}
for label, (config, img) in configs.items():
txt = pytesseract.image_to_string(img, config=config).strip()
results[label] = txt
print(f"OCR Ergebnis [{label}]: {txt}")
# bestes Ergebnis wählen (längster String)
plate_number = max(results.values(), key=len) if any(results.values()) else None
if plate_number:
plate_number_clean = plate_number.replace(" ", "").replace("-", "")
match_de = re.match(r"^([A-Z]{1,3})([A-Z]{1,2})([0-9]{1,4})$", plate_number_clean)
match_nl = re.match(r"^([A-Z]{1,2})([0-9]{2,3})([A-Z]{1,2})$", plate_number_clean)
if match_de:
plate_number = f"{match_de.group(1)} {match_de.group(2)} {match_de.group(3)}"
elif match_nl:
plate_number = f"{match_nl.group(1)} {match_nl.group(2)} {match_nl.group(3)}"
return plate_number.strip()
return None
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Aufruf: python3 license.py <bilddatei>")
sys.exit(1)
image_path = sys.argv[1]
plate_number = detect_plate_number(image_path)
print("Detected Plate Number:", plate_number)