MalekCode03 commited on
Commit
76f34f5
·
verified ·
1 Parent(s): a0a2522

Update alpr_module.py

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