Spaces:
Sleeping
Sleeping
Upload 8 files
Browse files- Readme.txt +109 -0
- app.py +140 -0
- door_detector_model/args.yaml +110 -0
- door_detector_model/weights/best.pt +3 -0
- door_detector_model/weights/last.pt +3 -0
- requirements.txt +0 -0
- utils/extractor.py +229 -0
- utils/preprocessor.py +48 -0
Readme.txt
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
Floorplan Analyzer
|
| 3 |
+
-------------------------------------------
|
| 4 |
+
|
| 5 |
+
AI-powered application for analyzing architectural floor plan images.
|
| 6 |
+
|
| 7 |
+
Features:
|
| 8 |
+
- Door detection and counting
|
| 9 |
+
- Architectural text search
|
| 10 |
+
|
| 11 |
+
Technologies:
|
| 12 |
+
- Python
|
| 13 |
+
- YOLOv8
|
| 14 |
+
- EasyOCR
|
| 15 |
+
- OpenCV
|
| 16 |
+
- Gradio
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
Project Structure
|
| 20 |
+
-----------------
|
| 21 |
+
project/
|
| 22 |
+
|
| 23 |
+
├── app.py
|
| 24 |
+
├── requirements.txt
|
| 25 |
+
|
| 26 |
+
├── models/
|
| 27 |
+
│ └── best.pt
|
| 28 |
+
|
| 29 |
+
├── utils/
|
| 30 |
+
│ ├── extractor.py
|
| 31 |
+
│ ├── preprocessor.py
|
| 32 |
+
│ └── data_creator.py
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
Setup Instructions
|
| 36 |
+
|
| 37 |
+
1. Create Virtual Environment
|
| 38 |
+
----------------------------
|
| 39 |
+
Windows:
|
| 40 |
+
|
| 41 |
+
python -m venv venv_name
|
| 42 |
+
Activate Environment:
|
| 43 |
+
venv_name\Scripts\activate
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
2. Install Requirements
|
| 47 |
+
--------------------------------
|
| 48 |
+
pip install -r requirements.txt
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
Important Note About Torch Installation
|
| 52 |
+
----------------------------------------
|
| 53 |
+
On some systems, torch installation may fail due to CUDA or platform-specific issues.
|
| 54 |
+
|
| 55 |
+
If installation stops because of torch:
|
| 56 |
+
- continue normally
|
| 57 |
+
- run the application
|
| 58 |
+
- install only missing packages manually
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
===========================================================
|
| 62 |
+
Run:
|
| 63 |
+
|
| 64 |
+
python app.py
|
| 65 |
+
===========================================================
|
| 66 |
+
If any package is missing, install it manually.
|
| 67 |
+
|
| 68 |
+
Example:
|
| 69 |
+
|
| 70 |
+
pip install opencv-python
|
| 71 |
+
|
| 72 |
+
The application supports CPU inference.
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
Run Application
|
| 76 |
+
|
| 77 |
+
python app.py
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
After launching, Gradio will provide a local browser URL similar to:
|
| 81 |
+
|
| 82 |
+
http://127.0.0.1:7860
|
| 83 |
+
|
| 84 |
+
Open it in your browser.
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
Using the Application
|
| 88 |
+
|
| 89 |
+
Door Detection
|
| 90 |
+
- Upload floor plan image
|
| 91 |
+
- Select "Count Doors"
|
| 92 |
+
- Submit
|
| 93 |
+
|
| 94 |
+
Text Search
|
| 95 |
+
- Upload floor plan image
|
| 96 |
+
- Select "Search Text"
|
| 97 |
+
- Enter query text
|
| 98 |
+
- Submit
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
Notes
|
| 102 |
+
- Door detection uses tiled inference
|
| 103 |
+
- OCR uses EasyOCR with fuzzy matching
|
| 104 |
+
- CPU inference supported
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
Author
|
| 108 |
+
- Aswin Das
|
| 109 |
+
|
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import tempfile
|
| 4 |
+
|
| 5 |
+
from utils.preprocessor import (
|
| 6 |
+
TextTargetPreprocessor,
|
| 7 |
+
DoorTargetPreprocessor
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
from utils.extractor import (
|
| 11 |
+
TextDetector,
|
| 12 |
+
DoorDetector
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# LOAD COMPONENTS
|
| 17 |
+
|
| 18 |
+
text_preprocessor = TextTargetPreprocessor()
|
| 19 |
+
door_preprocessor = DoorTargetPreprocessor()
|
| 20 |
+
text_detector = TextDetector(gpu=False)
|
| 21 |
+
door_detector = DoorDetector()
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# MAIN PIPELINE
|
| 25 |
+
|
| 26 |
+
def process_floorplan(image, operation, query):
|
| 27 |
+
|
| 28 |
+
# save uploaded image temporarily
|
| 29 |
+
temp_path = tempfile.NamedTemporaryFile(
|
| 30 |
+
suffix=".png",
|
| 31 |
+
delete=False
|
| 32 |
+
).name
|
| 33 |
+
|
| 34 |
+
cv2.imwrite(
|
| 35 |
+
temp_path,
|
| 36 |
+
cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# DOOR DETECTION
|
| 41 |
+
|
| 42 |
+
if operation == "Count Doors":
|
| 43 |
+
|
| 44 |
+
processed = door_preprocessor.run(temp_path)
|
| 45 |
+
|
| 46 |
+
processed_path = tempfile.NamedTemporaryFile(
|
| 47 |
+
suffix=".png",
|
| 48 |
+
delete=False
|
| 49 |
+
).name
|
| 50 |
+
|
| 51 |
+
cv2.imwrite(processed_path, processed)
|
| 52 |
+
|
| 53 |
+
result = door_detector.predict(
|
| 54 |
+
image_path=processed_path,
|
| 55 |
+
confidence_threshold=0.5
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
final_img = result["output_image"]
|
| 59 |
+
|
| 60 |
+
final_img = cv2.cvtColor(
|
| 61 |
+
final_img,
|
| 62 |
+
cv2.COLOR_BGR2RGB
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
return (
|
| 66 |
+
final_img,
|
| 67 |
+
f"Total Doors Detected: {result['door_count']}"
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# TEXT SEARCH
|
| 72 |
+
|
| 73 |
+
elif operation == "Search Text":
|
| 74 |
+
|
| 75 |
+
processed = text_preprocessor.run(temp_path)
|
| 76 |
+
|
| 77 |
+
result_img = text_detector.extract_and_search(
|
| 78 |
+
processed,
|
| 79 |
+
query
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
result_img = cv2.cvtColor(
|
| 83 |
+
result_img,
|
| 84 |
+
cv2.COLOR_BGR2RGB
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
return (
|
| 88 |
+
result_img,
|
| 89 |
+
f"Searched Text: {query}"
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# GRADIO UI
|
| 93 |
+
|
| 94 |
+
demo = gr.Interface(
|
| 95 |
+
|
| 96 |
+
fn=process_floorplan,
|
| 97 |
+
|
| 98 |
+
inputs=[
|
| 99 |
+
|
| 100 |
+
gr.Image(
|
| 101 |
+
type="numpy",
|
| 102 |
+
label="Upload Floorplan"
|
| 103 |
+
),
|
| 104 |
+
|
| 105 |
+
gr.Radio(
|
| 106 |
+
["Count Doors", "Search Text"],
|
| 107 |
+
label="Select Operation"
|
| 108 |
+
),
|
| 109 |
+
|
| 110 |
+
gr.Textbox(
|
| 111 |
+
label="Text to Search",
|
| 112 |
+
placeholder="Example: Lounge"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
],
|
| 116 |
+
|
| 117 |
+
outputs=[
|
| 118 |
+
|
| 119 |
+
gr.Image(
|
| 120 |
+
type="numpy",
|
| 121 |
+
label="Output Image"
|
| 122 |
+
),
|
| 123 |
+
|
| 124 |
+
gr.Textbox(
|
| 125 |
+
label="Result"
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
],
|
| 129 |
+
|
| 130 |
+
title="Floorplan Analyzer",
|
| 131 |
+
|
| 132 |
+
description="""
|
| 133 |
+
Upload a floorplan image and either:
|
| 134 |
+
- count door symbols
|
| 135 |
+
- search for architectural text labels
|
| 136 |
+
"""
|
| 137 |
+
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
demo.launch()
|
door_detector_model/args.yaml
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
task: detect
|
| 2 |
+
mode: train
|
| 3 |
+
model: yolov8n.pt
|
| 4 |
+
data: door.v1i.yolov8/data.yaml
|
| 5 |
+
epochs: 100
|
| 6 |
+
time: null
|
| 7 |
+
patience: 100
|
| 8 |
+
batch: 2
|
| 9 |
+
imgsz: 640
|
| 10 |
+
save: true
|
| 11 |
+
save_period: -1
|
| 12 |
+
cache: false
|
| 13 |
+
device: '0'
|
| 14 |
+
workers: 0
|
| 15 |
+
project: door_detection_project
|
| 16 |
+
name: door_detector-2
|
| 17 |
+
exist_ok: false
|
| 18 |
+
pretrained: true
|
| 19 |
+
optimizer: auto
|
| 20 |
+
verbose: true
|
| 21 |
+
seed: 0
|
| 22 |
+
deterministic: true
|
| 23 |
+
single_cls: false
|
| 24 |
+
rect: false
|
| 25 |
+
cos_lr: false
|
| 26 |
+
close_mosaic: 10
|
| 27 |
+
resume: false
|
| 28 |
+
amp: true
|
| 29 |
+
fraction: 1.0
|
| 30 |
+
profile: false
|
| 31 |
+
freeze: null
|
| 32 |
+
multi_scale: 0.0
|
| 33 |
+
compile: false
|
| 34 |
+
overlap_mask: true
|
| 35 |
+
mask_ratio: 4
|
| 36 |
+
dropout: 0.0
|
| 37 |
+
val: true
|
| 38 |
+
split: val
|
| 39 |
+
save_json: false
|
| 40 |
+
conf: null
|
| 41 |
+
iou: 0.7
|
| 42 |
+
max_det: 300
|
| 43 |
+
half: false
|
| 44 |
+
dnn: false
|
| 45 |
+
plots: true
|
| 46 |
+
end2end: null
|
| 47 |
+
source: null
|
| 48 |
+
vid_stride: 1
|
| 49 |
+
stream_buffer: false
|
| 50 |
+
visualize: false
|
| 51 |
+
augment: false
|
| 52 |
+
agnostic_nms: false
|
| 53 |
+
classes: null
|
| 54 |
+
retina_masks: false
|
| 55 |
+
embed: null
|
| 56 |
+
show: false
|
| 57 |
+
save_frames: false
|
| 58 |
+
save_txt: false
|
| 59 |
+
save_conf: false
|
| 60 |
+
save_crop: false
|
| 61 |
+
show_labels: true
|
| 62 |
+
show_conf: true
|
| 63 |
+
show_boxes: true
|
| 64 |
+
line_width: null
|
| 65 |
+
format: torchscript
|
| 66 |
+
keras: false
|
| 67 |
+
optimize: false
|
| 68 |
+
int8: false
|
| 69 |
+
dynamic: false
|
| 70 |
+
simplify: true
|
| 71 |
+
opset: null
|
| 72 |
+
workspace: null
|
| 73 |
+
nms: false
|
| 74 |
+
lr0: 0.01
|
| 75 |
+
lrf: 0.01
|
| 76 |
+
momentum: 0.937
|
| 77 |
+
weight_decay: 0.0005
|
| 78 |
+
warmup_epochs: 3.0
|
| 79 |
+
warmup_momentum: 0.8
|
| 80 |
+
warmup_bias_lr: 0.1
|
| 81 |
+
box: 7.5
|
| 82 |
+
cls: 0.5
|
| 83 |
+
cls_pw: 0.0
|
| 84 |
+
dfl: 1.5
|
| 85 |
+
pose: 12.0
|
| 86 |
+
kobj: 1.0
|
| 87 |
+
rle: 1.0
|
| 88 |
+
angle: 1.0
|
| 89 |
+
nbs: 64
|
| 90 |
+
hsv_h: 0.015
|
| 91 |
+
hsv_s: 0.7
|
| 92 |
+
hsv_v: 0.4
|
| 93 |
+
degrees: 0.0
|
| 94 |
+
translate: 0.1
|
| 95 |
+
scale: 0.5
|
| 96 |
+
shear: 0.0
|
| 97 |
+
perspective: 0.0
|
| 98 |
+
flipud: 0.0
|
| 99 |
+
fliplr: 0.5
|
| 100 |
+
bgr: 0.0
|
| 101 |
+
mosaic: 1.0
|
| 102 |
+
mixup: 0.0
|
| 103 |
+
cutmix: 0.0
|
| 104 |
+
copy_paste: 0.0
|
| 105 |
+
copy_paste_mode: flip
|
| 106 |
+
auto_augment: randaugment
|
| 107 |
+
erasing: 0.4
|
| 108 |
+
cfg: null
|
| 109 |
+
tracker: botsort.yaml
|
| 110 |
+
save_dir: C:\Users\Aswin Das\Desktop\machine_test_G3\runs\detect\door_detection_project\door_detector-2
|
door_detector_model/weights/best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:105e4b153064b4b4a369ff74386d9d38ef0b4a0803fa5e9cd0b7b963fa63f51e
|
| 3 |
+
size 6255331
|
door_detector_model/weights/last.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8a21e70bcd446ed7fc44681d4d1409f44e335dc5f702777da1da71355c2413ff
|
| 3 |
+
size 6255331
|
requirements.txt
ADDED
|
Binary file (3.07 kB). View file
|
|
|
utils/extractor.py
ADDED
|
@@ -0,0 +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 |
+
|
utils/preprocessor.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class TextTargetPreprocessor:
|
| 7 |
+
def __init__(self, upscale_factor=3):
|
| 8 |
+
self.upscale_factor = upscale_factor
|
| 9 |
+
|
| 10 |
+
def run(self, image_path):
|
| 11 |
+
img = cv2.imread(image_path)
|
| 12 |
+
if img is None: raise FileNotFoundError("Image not found.")
|
| 13 |
+
|
| 14 |
+
# 1. Upscale (Maintain Cubic interpolation for smoothness)
|
| 15 |
+
h, w = img.shape[:2]
|
| 16 |
+
img_scaled = cv2.resize(img, (w*self.upscale_factor, h*self.upscale_factor),
|
| 17 |
+
interpolation=cv2.INTER_CUBIC)
|
| 18 |
+
|
| 19 |
+
return img_scaled
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class DoorTargetPreprocessor:
|
| 23 |
+
def __init__(self, upscale_factor=3):
|
| 24 |
+
self.upscale_factor = upscale_factor
|
| 25 |
+
|
| 26 |
+
def run(self, image_path):
|
| 27 |
+
img = cv2.imread(image_path)
|
| 28 |
+
if img is None: raise FileNotFoundError("Image not found.")
|
| 29 |
+
|
| 30 |
+
# Upscale (Maintain Sub-pixel details)
|
| 31 |
+
h, w = img.shape[:2]
|
| 32 |
+
img_scaled = cv2.resize(img, (w*self.upscale_factor, h*self.upscale_factor),
|
| 33 |
+
interpolation=cv2.INTER_CUBIC)
|
| 34 |
+
|
| 35 |
+
denoised = cv2.bilateralFilter(img_scaled, 9, 75, 75)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
gamma = 0.8
|
| 39 |
+
lookUpTable = np.empty((1,256), np.uint8)
|
| 40 |
+
for i in range(256):
|
| 41 |
+
lookUpTable[0,i] = np.clip(pow(i / 255.0, gamma) * 255.0, 0, 255)
|
| 42 |
+
structural_img = cv2.LUT(denoised, lookUpTable)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
|
| 46 |
+
final_yolo_img = cv2.filter2D(structural_img, -1, kernel)
|
| 47 |
+
|
| 48 |
+
return final_yolo_img
|