sabaridsnfuji
commited on
Commit
•
f144fcd
1
Parent(s):
e00ea77
Upload app.py
Browse files
app.py
CHANGED
@@ -1,129 +1,109 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
import
|
9 |
-
|
10 |
-
import
|
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 |
-
"./sample/10_12.jpg"
|
111 |
-
]
|
112 |
-
|
113 |
-
# Set up Gradio interface
|
114 |
-
def gradio_interface(image):
|
115 |
-
output_image, predictions_str = predict_image(image)
|
116 |
-
return output_image, predictions_str
|
117 |
-
|
118 |
-
# Create the Gradio Interface
|
119 |
-
gr_interface = gr.Interface(
|
120 |
-
fn=gradio_interface,
|
121 |
-
inputs=gr.Image(label="Upload or Select an Image", type="pil", examples=sample_images),
|
122 |
-
outputs=[gr.Image(label="Result Image"), gr.Textbox(label="Predictions JSON")],
|
123 |
-
title="House CAD Design Object Detection",
|
124 |
-
description="Upload a CAD design image of a house to detect objects with bounding boxes and probabilities."
|
125 |
-
)
|
126 |
-
|
127 |
-
# Launch the Gradio interface if run as main
|
128 |
-
if __name__ == "__main__":
|
129 |
-
gr_interface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import os
|
5 |
+
import json
|
6 |
+
from openvino.runtime import Core # Assuming you're using OpenVINO
|
7 |
+
from tqdm import tqdm
|
8 |
+
from PIL import Image
|
9 |
+
|
10 |
+
from tf_post_processing import non_max_suppression #,optimized_object_detection
|
11 |
+
|
12 |
+
# Load the OpenVINO model
|
13 |
+
classification_model_xml = r"D:\Research\Auto_CAD_detection\runs\detect\yolo11_Auto_CAD_detection_params6\weights\best_openvino_model\best.xml"
|
14 |
+
core = Core()
|
15 |
+
config = {
|
16 |
+
"INFERENCE_NUM_THREADS": 2,
|
17 |
+
"ENABLE_CPU_PINNING": True
|
18 |
+
}
|
19 |
+
model = core.read_model(model=classification_model_xml)
|
20 |
+
compiled_model = core.compile_model(model=model, device_name="CPU", config=config)
|
21 |
+
|
22 |
+
label_to_class_text = {0: 'range',
|
23 |
+
1: ' entry door',
|
24 |
+
2: 'kitchen sink',
|
25 |
+
3: ' bathroom sink',
|
26 |
+
4: 'toilet',
|
27 |
+
5: 'double folding door',
|
28 |
+
6: 'window',
|
29 |
+
7: 'shower',
|
30 |
+
8: 'bathtub',
|
31 |
+
9: 'single folding door',
|
32 |
+
10: 'dishwasher',
|
33 |
+
11: 'refrigerator'}
|
34 |
+
|
35 |
+
# Function to perform inference
|
36 |
+
def predict_image(image):
|
37 |
+
# Convert PIL Image to numpy array (OpenCV uses numpy arrays)
|
38 |
+
image = np.array(image)
|
39 |
+
|
40 |
+
# Resize, preprocess, and reshape the input image
|
41 |
+
img_size = 960
|
42 |
+
resized_image = cv2.resize(image, (img_size, img_size)) / 255.0
|
43 |
+
resized_image = resized_image.transpose(2, 0, 1)
|
44 |
+
reshaped_image = np.expand_dims(resized_image, axis=0).astype(np.float32)
|
45 |
+
|
46 |
+
im_height, im_width, _ = image.shape
|
47 |
+
output_numpy = compiled_model(reshaped_image)[0]
|
48 |
+
results = non_max_suppression(output_numpy, conf_thres=0.2, iou_thres=0.6, max_wh=15000)[0]
|
49 |
+
|
50 |
+
# Prepare output paths
|
51 |
+
predictions = []
|
52 |
+
|
53 |
+
# Draw boxes and collect prediction data
|
54 |
+
for result in results:
|
55 |
+
boxes = result[:4]
|
56 |
+
prob = result[4]
|
57 |
+
classes = int(result[5])
|
58 |
+
|
59 |
+
x1, y1, x2, y2 = np.uint16([
|
60 |
+
boxes[0] * im_width,
|
61 |
+
boxes[1] * im_height,
|
62 |
+
boxes[2] * im_width,
|
63 |
+
boxes[3] * im_height
|
64 |
+
])
|
65 |
+
|
66 |
+
if prob > 0.2:
|
67 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (255, 255, 0), 2)
|
68 |
+
label_text = f"{classes} {round(prob, 2)}"
|
69 |
+
cv2.putText(image, label_text, (x1, y1), 0, 0.5, (0, 255, 0), 2)
|
70 |
+
|
71 |
+
# Store prediction info in a JSON-compatible format
|
72 |
+
predictions.append({
|
73 |
+
"class": label_to_class_text[classes],
|
74 |
+
"probability": round(float(prob), 2),
|
75 |
+
"coordinates": {
|
76 |
+
"xmin": int(x1),
|
77 |
+
"ymin": int(y1),
|
78 |
+
"xmax": int(x2),
|
79 |
+
"ymax": int(y2)
|
80 |
+
}
|
81 |
+
})
|
82 |
+
|
83 |
+
# Convert the processed image back to PIL Image for Gradio
|
84 |
+
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
85 |
+
|
86 |
+
return pil_image, json.dumps(predictions, indent=4)
|
87 |
+
|
88 |
+
# Sample images for Gradio examples
|
89 |
+
# Define sample images for user convenience
|
90 |
+
sample_images = [
|
91 |
+
"./sample/10_2.jpg",
|
92 |
+
"./sample/10_10.jpg",
|
93 |
+
"./sample/10_12.jpg"
|
94 |
+
]
|
95 |
+
|
96 |
+
|
97 |
+
# Gradio UI setup with examples
|
98 |
+
gr_interface = gr.Interface(
|
99 |
+
fn=predict_image,
|
100 |
+
inputs=gr.Image(type="pil"), # Updated to gr.Image for image input
|
101 |
+
outputs=[gr.Image(type="pil"), gr.Textbox()], # Updated to gr.Image and gr.Textbox
|
102 |
+
title="House CAD Design Object Detection",
|
103 |
+
description="Upload a CAD design image of a house to detect objects with bounding boxes and probabilities.",
|
104 |
+
examples=sample_images # Add the examples here
|
105 |
+
)
|
106 |
+
|
107 |
+
# Launch the Gradio interface if run as main
|
108 |
+
if __name__ == "__main__":
|
109 |
+
gr_interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|