Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -18,88 +18,90 @@ def run_inference(image):
|
|
18 |
annotated_image = results.render()[0]
|
19 |
annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
|
20 |
|
21 |
-
|
22 |
-
detected_objects = results.pandas().xyxy[0]['name'].value_counts().to_dict()
|
23 |
-
summary = "Detected Objects:\n" + "\n".join(
|
24 |
-
[f"{obj}: {count}" for obj, count in detected_objects.items()]
|
25 |
-
)
|
26 |
-
return annotated_image, summary
|
27 |
-
|
28 |
-
# Create the Gradio interface
|
29 |
-
interface = gr.Blocks(css="""
|
30 |
-
body {
|
31 |
-
font-family: 'Poppins', sans-serif;
|
32 |
-
background: linear-gradient(135deg, #6a11cb, #2575fc);
|
33 |
-
color: #fff;
|
34 |
-
margin: 0;
|
35 |
-
padding: 0;
|
36 |
-
}
|
37 |
-
|
38 |
-
header {
|
39 |
-
text-align: center;
|
40 |
-
background: linear-gradient(90deg, #ff758c, #ff7eb3);
|
41 |
-
padding: 1rem;
|
42 |
-
border-radius: 15px;
|
43 |
-
box-shadow: 0px 4px 15px rgba(0, 0, 0, 0.2);
|
44 |
-
margin-bottom: 1rem;
|
45 |
-
}
|
46 |
-
|
47 |
-
header h1 {
|
48 |
-
margin: 0;
|
49 |
-
font-size: 2.5rem;
|
50 |
-
color: #fff;
|
51 |
-
}
|
52 |
-
|
53 |
-
.description {
|
54 |
-
text-align: center;
|
55 |
-
font-size: 1.2rem;
|
56 |
-
margin: 0.5rem 0 1.5rem 0;
|
57 |
-
color: #f0f0f0;
|
58 |
-
}
|
59 |
-
|
60 |
-
button {
|
61 |
-
background: linear-gradient(90deg, #6a11cb, #2575fc);
|
62 |
-
border: none;
|
63 |
-
border-radius: 10px;
|
64 |
-
padding: 0.8rem 1.5rem;
|
65 |
-
font-size: 1rem;
|
66 |
-
color: white;
|
67 |
-
cursor: pointer;
|
68 |
-
transition: transform 0.2s, background 0.2s;
|
69 |
-
}
|
70 |
-
|
71 |
-
button:hover {
|
72 |
-
transform: scale(1.05);
|
73 |
-
background: linear-gradient(90deg, #2575fc, #6a11cb);
|
74 |
-
}
|
75 |
-
""")
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
with gr.Row():
|
85 |
-
|
86 |
-
|
87 |
-
with gr.Column():
|
88 |
-
detected_image = gr.Image(type="pil", label="Detected Objects")
|
89 |
-
summary_output = gr.Textbox(lines=10, label="Summary of Detected Objects")
|
90 |
-
|
91 |
with gr.Row():
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
102 |
)
|
103 |
|
104 |
-
|
105 |
-
|
|
|
|
|
|
18 |
annotated_image = results.render()[0]
|
19 |
annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
|
20 |
|
21 |
+
return annotated_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
# Function to generate a summary for the detected objects
|
24 |
+
def generate_summary(image):
|
25 |
+
results = model(image)
|
26 |
+
detected_objects = results.pandas().xyxy[0]
|
27 |
+
summary = "Detected objects:\n\n"
|
28 |
+
for idx, obj in detected_objects.iterrows():
|
29 |
+
summary += f"- {obj['name']} with confidence {obj['confidence']:.2f}\n"
|
30 |
+
return summary
|
31 |
+
|
32 |
+
# Create the Gradio interface with improved UI
|
33 |
+
with gr.Blocks(css="""
|
34 |
+
body {
|
35 |
+
font-family: 'Poppins', sans-serif;
|
36 |
+
background-color: #2B3D41;
|
37 |
+
color: #F9B9D2;
|
38 |
+
}
|
39 |
+
header {
|
40 |
+
background-color: #83A0A0;
|
41 |
+
padding: 20px;
|
42 |
+
text-align: center;
|
43 |
+
border-radius: 10px;
|
44 |
+
color: white;
|
45 |
+
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
|
46 |
+
}
|
47 |
+
footer {
|
48 |
+
background-color: #4C5F6B;
|
49 |
+
padding: 10px;
|
50 |
+
text-align: center;
|
51 |
+
border-radius: 10px;
|
52 |
+
color: white;
|
53 |
+
margin-top: 20px;
|
54 |
+
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
|
55 |
+
}
|
56 |
+
.btn-primary {
|
57 |
+
background-color: #BCA0BC;
|
58 |
+
color: #2B3D41;
|
59 |
+
padding: 10px 20px;
|
60 |
+
border-radius: 5px;
|
61 |
+
font-weight: bold;
|
62 |
+
border: none;
|
63 |
+
cursor: pointer;
|
64 |
+
transition: all 0.3s;
|
65 |
+
}
|
66 |
+
.btn-primary:hover {
|
67 |
+
background-color: #F9B9D2;
|
68 |
+
color: #2B3D41;
|
69 |
+
}
|
70 |
+
.gr-box {
|
71 |
+
background-color: #4C5F6B;
|
72 |
+
border-radius: 10px;
|
73 |
+
padding: 20px;
|
74 |
+
color: #F9B9D2;
|
75 |
+
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
|
76 |
+
}
|
77 |
+
.gr-input {
|
78 |
+
background-color: #BCA0BC;
|
79 |
+
border-radius: 5px;
|
80 |
+
border: none;
|
81 |
+
padding: 10px;
|
82 |
+
color: #2B3D41;
|
83 |
+
}
|
84 |
+
""") as demo:
|
85 |
with gr.Row():
|
86 |
+
gr.Markdown("<h1 style='text-align:center; color:#F9B9D2;'>✨ InsightVision: Detect, Analyze, Summarize ✨</h1>")
|
87 |
+
|
|
|
|
|
|
|
|
|
88 |
with gr.Row():
|
89 |
+
with gr.Column(scale=2):
|
90 |
+
image_input = gr.Image(label="Upload Image", type="pil", elem_classes="gr-input")
|
91 |
+
with gr.Row():
|
92 |
+
detect_button = gr.Button("Run Detection", elem_classes="btn-primary")
|
93 |
+
with gr.Column(scale=3):
|
94 |
+
annotated_image_output = gr.Image(label="Detected Image", type="pil", elem_classes="gr-box")
|
95 |
+
summary_output = gr.Textbox(label="Detection Summary", lines=10, interactive=False, elem_classes="gr-box")
|
96 |
+
|
97 |
+
# Actions for buttons
|
98 |
+
detect_button.click(
|
99 |
+
fn=lambda image: (run_inference(image), generate_summary(np.array(image))),
|
100 |
+
inputs=[image_input],
|
101 |
+
outputs=[annotated_image_output, summary_output]
|
102 |
)
|
103 |
|
104 |
+
gr.Markdown("<footer>Made with ❤️ using Gradio and YOLOv5 | © 2024 InsightVision</footer>")
|
105 |
+
|
106 |
+
# Launch the interface
|
107 |
+
demo.launch()
|