Spaces:
Runtime error
Runtime error
Update alpr_module.py
Browse files- alpr_module.py +74 -72
alpr_module.py
CHANGED
|
@@ -1,72 +1,74 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import numpy as np
|
| 3 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import os
|
| 4 |
+
os.makedirs('/app/.easyocr', exist_ok=True)
|
| 5 |
+
|
| 6 |
+
from inference_sdk import InferenceHTTPClient
|
| 7 |
+
|
| 8 |
+
# تعريف العميل
|
| 9 |
+
CLIENT = InferenceHTTPClient(
|
| 10 |
+
api_url="https://serverless.roboflow.com",
|
| 11 |
+
api_key="4m9tZRxBfEK8C4no7zsZ"
|
| 12 |
+
)
|
| 13 |
+
reader = easyocr.Reader(['en'], model_storage_directory='/app/.easyocr')
|
| 14 |
+
def detect_license_plate(image_path, model_id="license-plate-recognition-rxg4e/11"):
|
| 15 |
+
"""إجراء التنبؤ باستخدام نموذج Roboflow"""
|
| 16 |
+
result = CLIENT.infer(image_path, model_id=model_id)
|
| 17 |
+
return result
|
| 18 |
+
|
| 19 |
+
def extract_plate_from_image(image_path, predictions):
|
| 20 |
+
"""استخلاص صورة اللوحة من الصورة الكاملة بناءً على التنبؤات"""
|
| 21 |
+
image = cv2.imread(image_path)
|
| 22 |
+
max_confidence = 0
|
| 23 |
+
best_coords = None
|
| 24 |
+
|
| 25 |
+
for prediction in predictions['predictions']:
|
| 26 |
+
confidence = prediction['confidence']
|
| 27 |
+
if confidence > max_confidence:
|
| 28 |
+
max_confidence = confidence
|
| 29 |
+
x = int(prediction['x'])
|
| 30 |
+
y = int(prediction['y'])
|
| 31 |
+
width = int(prediction['width'])
|
| 32 |
+
height = int(prediction['height'])
|
| 33 |
+
x1 = int(x - width / 2)
|
| 34 |
+
y1 = int(y - height / 2)
|
| 35 |
+
x2 = int(x + width / 2)
|
| 36 |
+
y2 = int(y + height / 2)
|
| 37 |
+
best_coords = (x1, y1, x2, y2)
|
| 38 |
+
|
| 39 |
+
if not best_coords:
|
| 40 |
+
return None # لم يتم الكشف عن أي لوحة
|
| 41 |
+
|
| 42 |
+
x1, y1, x2, y2 = best_coords
|
| 43 |
+
polygon_points = np.array([
|
| 44 |
+
[x1, y1],
|
| 45 |
+
[x2, y1],
|
| 46 |
+
[x2, y2],
|
| 47 |
+
[x1, y2]
|
| 48 |
+
], dtype=np.int32)
|
| 49 |
+
|
| 50 |
+
mask = np.zeros(image.shape[:2], dtype=np.uint8)
|
| 51 |
+
cv2.fillPoly(mask, [polygon_points], 255)
|
| 52 |
+
|
| 53 |
+
masked_image = cv2.bitwise_and(image, image, mask=mask)
|
| 54 |
+
x, y, w, h = cv2.boundingRect(polygon_points)
|
| 55 |
+
cropped_plate = masked_image[y:y+h, x:x+w]
|
| 56 |
+
|
| 57 |
+
return cropped_plate
|
| 58 |
+
|
| 59 |
+
def enhance_image_for_ocr(image):
|
| 60 |
+
"""تحسين الصورة لاستخلاص النصوص باستخدام EasyOCR"""
|
| 61 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 62 |
+
return gray
|
| 63 |
+
|
| 64 |
+
def extract_license_number(image):
|
| 65 |
+
"""استخراج الأرقام من صورة اللوحة"""
|
| 66 |
+
result = reader.readtext(image)
|
| 67 |
+
plate_numbers = []
|
| 68 |
+
|
| 69 |
+
for detection in result:
|
| 70 |
+
text = detection[1]
|
| 71 |
+
numbers_only = ''.join(c for c in text if c.isdigit())
|
| 72 |
+
if numbers_only:
|
| 73 |
+
plate_numbers.append(numbers_only)
|
| 74 |
+
return plate_numbers
|