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