MrDasOfficial commited on
Commit
470db6f
·
verified ·
1 Parent(s): 483ad1c

Update utils/extractor.py

Browse files
Files changed (1) hide show
  1. utils/extractor.py +229 -229
utils/extractor.py CHANGED
@@ -1,229 +1,229 @@
1
-
2
- import easyocr
3
- from rapidfuzz import fuzz
4
- import cv2
5
- from ultralytics import YOLO
6
- import numpy as np
7
-
8
-
9
- class TextDetector:
10
-
11
- def __init__(self, gpu=True):
12
- self.reader = easyocr.Reader(['en'], gpu=gpu)
13
-
14
- def extract_and_search(self, processed_text_img, query):
15
-
16
- results = self.reader.readtext(
17
- processed_text_img,
18
- rotation_info=[90, 180, 270],
19
- mag_ratio=2.0,
20
- text_threshold=0.3,
21
- low_text=0.2,
22
- paragraph=False
23
- )
24
-
25
- search_query = query.upper().strip()
26
- output_img = processed_text_img.copy()
27
-
28
- match_count = 0
29
-
30
- for (bbox, text, prob) in results:
31
- detected_text = text.upper().strip()
32
-
33
- if fuzz.partial_ratio(search_query, detected_text) >= 70:
34
- match_count += 1
35
- pts = np.array(bbox, np.int32)
36
-
37
- cv2.polylines(
38
- output_img,
39
- [pts],
40
- True,
41
- (255, 0, 0),
42
- 4
43
- )
44
-
45
- # TOP CENTER TEXT
46
-
47
- result_text = f"Matches Found: {match_count}"
48
-
49
- font = cv2.FONT_HERSHEY_SIMPLEX
50
- font_scale = 2
51
- thickness = 2
52
-
53
- text_size = cv2.getTextSize(result_text,font,font_scale,thickness)[0]
54
-
55
- text_x = (output_img.shape[1] - text_size[0]) // 2
56
- text_y = 50
57
-
58
- # background rectangle
59
- cv2.rectangle(output_img,(text_x - 10, text_y - 35),
60
- (text_x + text_size[0] + 10, text_y + 10),(0, 0, 0),-1)
61
-
62
- # text
63
- cv2.putText(output_img,result_text,(text_x, text_y),
64
- font,font_scale,(255, 255, 255),thickness)
65
-
66
- return output_img
67
-
68
-
69
-
70
-
71
-
72
-
73
- class DoorDetector:
74
-
75
- def __init__(
76
- self,
77
- model_path = r"door_detector_model\weights\best.pt",
78
- tile_size=256,
79
- stride=128):
80
-
81
- self.model = YOLO(model_path)
82
-
83
- self.tile_size = tile_size
84
- self.stride = stride
85
-
86
- def predict(
87
- self,
88
- image_path,
89
- confidence_threshold=0.7,
90
- output_path="door_detection_output.png"):
91
-
92
- # load image
93
- img = cv2.imread(image_path)
94
-
95
- if img is None:
96
- raise FileNotFoundError(
97
- f"Unable to load image: {image_path}"
98
- )
99
-
100
- original_img = img.copy()
101
-
102
- H, W = img.shape[:2]
103
-
104
- all_boxes = []
105
-
106
- # tiled inference
107
- for y in range(0, H, self.stride):
108
-
109
- for x in range(0, W, self.stride):
110
-
111
- x2 = min(x + self.tile_size, W)
112
- y2 = min(y + self.tile_size, H)
113
-
114
- tile = img[y:y2, x:x2]
115
-
116
- # skip tiny edge tiles
117
- if tile.shape[0] < 100 or tile.shape[1] < 100:
118
- continue
119
-
120
- results = self.model.predict(source=tile,imgsz=self.tile_size,
121
- conf=confidence_threshold,verbose=False)
122
-
123
- result = results[0]
124
- boxes = result.boxes
125
-
126
- for box in boxes:
127
- bx1, by1, bx2, by2 = map(
128
- int,
129
- box.xyxy[0]
130
- )
131
-
132
- confidence = float(box.conf[0])
133
-
134
- # convert to global coords
135
- gx1 = bx1 + x
136
- gy1 = by1 + y
137
- gx2 = bx2 + x
138
- gy2 = by2 + y
139
-
140
- all_boxes.append([
141
- gx1,
142
- gy1,
143
- gx2,
144
- gy2,
145
- confidence
146
- ])
147
-
148
- # non-max suppression
149
- final_boxes = []
150
-
151
- if len(all_boxes) > 0:
152
- boxes_xywh = []
153
- confidences = []
154
-
155
- for b in all_boxes:
156
-
157
- x1, y1, x2, y2, conf = b
158
-
159
- boxes_xywh.append([
160
- int(x1),
161
- int(y1),
162
- int(x2 - x1),
163
- int(y2 - y1)
164
- ])
165
-
166
- confidences.append(float(conf))
167
-
168
- indices = cv2.dnn.NMSBoxes(
169
-
170
- boxes_xywh,
171
-
172
- confidences,
173
-
174
- score_threshold=confidence_threshold,
175
-
176
- nms_threshold=0.30
177
-
178
- )
179
-
180
- if len(indices) > 0:
181
-
182
- for i in indices.flatten():
183
-
184
- final_boxes.append(all_boxes[i])
185
-
186
- # draw detections
187
- for box in final_boxes:
188
-
189
- x1, y1, x2, y2, conf = box
190
-
191
- cv2.rectangle(
192
-
193
- original_img,
194
-
195
- (int(x1), int(y1)),
196
- (int(x2), int(y2)),
197
-
198
- (0, 255, 0),
199
-
200
- 2
201
-
202
- )
203
-
204
- # total count
205
- door_count = len(final_boxes)
206
- count_text = f"Total Doors: {door_count}"
207
-
208
- text_x = original_img.shape[1] - 350
209
- text_y = 50
210
-
211
- cv2.rectangle(original_img,(text_x - 10, text_y - 35),
212
- (text_x + 320, text_y + 10),(0, 0, 0),-1)
213
-
214
- cv2.putText(original_img,count_text,(text_x, text_y),
215
- cv2.FONT_HERSHEY_SIMPLEX,1,(255, 255, 255),2)
216
-
217
- # save output
218
- cv2.imwrite(output_path, original_img)
219
-
220
- return {
221
- "door_count": door_count,
222
- "detections": final_boxes,
223
- "output_path": output_path,
224
- "output_image": original_img
225
- }
226
-
227
-
228
-
229
-
 
1
+
2
+ import easyocr
3
+ from rapidfuzz import fuzz
4
+ import cv2
5
+ from ultralytics import YOLO
6
+ import numpy as np
7
+
8
+
9
+ class TextDetector:
10
+
11
+ def __init__(self, gpu=True):
12
+ self.reader = easyocr.Reader(['en'], gpu=gpu)
13
+
14
+ def extract_and_search(self, processed_text_img, query):
15
+
16
+ results = self.reader.readtext(
17
+ processed_text_img,
18
+ rotation_info=[90, 180, 270],
19
+ mag_ratio=2.0,
20
+ text_threshold=0.3,
21
+ low_text=0.2,
22
+ paragraph=False
23
+ )
24
+
25
+ search_query = query.upper().strip()
26
+ output_img = processed_text_img.copy()
27
+
28
+ match_count = 0
29
+
30
+ for (bbox, text, prob) in results:
31
+ detected_text = text.upper().strip()
32
+
33
+ if fuzz.partial_ratio(search_query, detected_text) >= 70:
34
+ match_count += 1
35
+ pts = np.array(bbox, np.int32)
36
+
37
+ cv2.polylines(
38
+ output_img,
39
+ [pts],
40
+ True,
41
+ (255, 0, 0),
42
+ 4
43
+ )
44
+
45
+ # TOP CENTER TEXT
46
+
47
+ result_text = f"Matches Found: {match_count}"
48
+
49
+ font = cv2.FONT_HERSHEY_SIMPLEX
50
+ font_scale = 2
51
+ thickness = 2
52
+
53
+ text_size = cv2.getTextSize(result_text,font,font_scale,thickness)[0]
54
+
55
+ text_x = (output_img.shape[1] - text_size[0]) // 2
56
+ text_y = 50
57
+
58
+ # background rectangle
59
+ cv2.rectangle(output_img,(text_x - 10, text_y - 35),
60
+ (text_x + text_size[0] + 10, text_y + 10),(0, 0, 0),-1)
61
+
62
+ # text
63
+ cv2.putText(output_img,result_text,(text_x, text_y),
64
+ font,font_scale,(255, 255, 255),thickness)
65
+
66
+ return output_img
67
+
68
+
69
+
70
+
71
+
72
+
73
+ class DoorDetector:
74
+
75
+ def __init__(
76
+ self,
77
+ model_path = "door_detector_model/weights/best.pt",
78
+ tile_size=256,
79
+ stride=128):
80
+
81
+ self.model = YOLO(model_path)
82
+
83
+ self.tile_size = tile_size
84
+ self.stride = stride
85
+
86
+ def predict(
87
+ self,
88
+ image_path,
89
+ confidence_threshold=0.7,
90
+ output_path="door_detection_output.png"):
91
+
92
+ # load image
93
+ img = cv2.imread(image_path)
94
+
95
+ if img is None:
96
+ raise FileNotFoundError(
97
+ f"Unable to load image: {image_path}"
98
+ )
99
+
100
+ original_img = img.copy()
101
+
102
+ H, W = img.shape[:2]
103
+
104
+ all_boxes = []
105
+
106
+ # tiled inference
107
+ for y in range(0, H, self.stride):
108
+
109
+ for x in range(0, W, self.stride):
110
+
111
+ x2 = min(x + self.tile_size, W)
112
+ y2 = min(y + self.tile_size, H)
113
+
114
+ tile = img[y:y2, x:x2]
115
+
116
+ # skip tiny edge tiles
117
+ if tile.shape[0] < 100 or tile.shape[1] < 100:
118
+ continue
119
+
120
+ results = self.model.predict(source=tile,imgsz=self.tile_size,
121
+ conf=confidence_threshold,verbose=False)
122
+
123
+ result = results[0]
124
+ boxes = result.boxes
125
+
126
+ for box in boxes:
127
+ bx1, by1, bx2, by2 = map(
128
+ int,
129
+ box.xyxy[0]
130
+ )
131
+
132
+ confidence = float(box.conf[0])
133
+
134
+ # convert to global coords
135
+ gx1 = bx1 + x
136
+ gy1 = by1 + y
137
+ gx2 = bx2 + x
138
+ gy2 = by2 + y
139
+
140
+ all_boxes.append([
141
+ gx1,
142
+ gy1,
143
+ gx2,
144
+ gy2,
145
+ confidence
146
+ ])
147
+
148
+ # non-max suppression
149
+ final_boxes = []
150
+
151
+ if len(all_boxes) > 0:
152
+ boxes_xywh = []
153
+ confidences = []
154
+
155
+ for b in all_boxes:
156
+
157
+ x1, y1, x2, y2, conf = b
158
+
159
+ boxes_xywh.append([
160
+ int(x1),
161
+ int(y1),
162
+ int(x2 - x1),
163
+ int(y2 - y1)
164
+ ])
165
+
166
+ confidences.append(float(conf))
167
+
168
+ indices = cv2.dnn.NMSBoxes(
169
+
170
+ boxes_xywh,
171
+
172
+ confidences,
173
+
174
+ score_threshold=confidence_threshold,
175
+
176
+ nms_threshold=0.30
177
+
178
+ )
179
+
180
+ if len(indices) > 0:
181
+
182
+ for i in indices.flatten():
183
+
184
+ final_boxes.append(all_boxes[i])
185
+
186
+ # draw detections
187
+ for box in final_boxes:
188
+
189
+ x1, y1, x2, y2, conf = box
190
+
191
+ cv2.rectangle(
192
+
193
+ original_img,
194
+
195
+ (int(x1), int(y1)),
196
+ (int(x2), int(y2)),
197
+
198
+ (0, 255, 0),
199
+
200
+ 2
201
+
202
+ )
203
+
204
+ # total count
205
+ door_count = len(final_boxes)
206
+ count_text = f"Total Doors: {door_count}"
207
+
208
+ text_x = original_img.shape[1] - 350
209
+ text_y = 50
210
+
211
+ cv2.rectangle(original_img,(text_x - 10, text_y - 35),
212
+ (text_x + 320, text_y + 10),(0, 0, 0),-1)
213
+
214
+ cv2.putText(original_img,count_text,(text_x, text_y),
215
+ cv2.FONT_HERSHEY_SIMPLEX,1,(255, 255, 255),2)
216
+
217
+ # save output
218
+ cv2.imwrite(output_path, original_img)
219
+
220
+ return {
221
+ "door_count": door_count,
222
+ "detections": final_boxes,
223
+ "output_path": output_path,
224
+ "output_image": original_img
225
+ }
226
+
227
+
228
+
229
+