SatyamSinghal commited on
Commit
f3b7d6b
·
verified ·
1 Parent(s): 49e169b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -80
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
- # Generate a summary of detected objects
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
- with interface:
78
- with gr.Row():
79
- gr.Markdown("<header><h1>🌟 InsightVision: Detect, Analyze, Summarize 🌟</h1></header>")
80
- with gr.Row():
81
- gr.Markdown(
82
- "<div class='description'>Upload an image to detect objects using YOLOv5 and receive a detailed summary of detected items!</div>"
83
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  with gr.Row():
85
- with gr.Column():
86
- uploaded_image = gr.Image(type="pil", label="Upload Your Image")
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
- submit_button = gr.Button("Analyze Image")
93
-
94
- def process_image(image):
95
- annotated_image, summary = run_inference(image)
96
- return annotated_image, summary
97
-
98
- submit_button.click(
99
- process_image,
100
- inputs=[uploaded_image],
101
- outputs=[detected_image, summary_output],
 
 
 
102
  )
103
 
104
- if __name__ == "__main__":
105
- interface.launch()
 
 
 
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()