mail2kandan commited on
Commit
66e8a54
Β·
1 Parent(s): 15d79d7

Add css for better visulization

Browse files
Files changed (2) hide show
  1. app.py +100 -22
  2. testImages/heroimage.png +0 -0
app.py CHANGED
@@ -21,35 +21,113 @@ MODEL_PATH = initialize_model_path()
21
 
22
  def predict(pilimg, conf_threshold, iou_threshold):
23
  try:
24
- # Initialize model for each prediction using the cached path
 
25
  model = YOLO(MODEL_PATH, task='detect')
26
-
27
- # Run inference
28
  result = model.predict(pilimg, conf=conf_threshold, iou=iou_threshold)
29
  img_bgr = result[0].plot()
30
- return Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
31
  except Exception as e:
32
  print(f"Error during prediction: {str(e)}")
33
  return None
34
 
35
- # Create Gradio interface
36
- demo = gr.Interface(
37
- fn=predict,
38
- inputs=[
39
- gr.Image(type="pil", label="Input Image"),
40
- gr.Slider(minimum=0.1, maximum=1.0, step=0.05, value=0.5, label="Confidence Threshold"),
41
- gr.Slider(minimum=0.1, maximum=1.0, step=0.05, value=0.6, label="IOU Threshold")
42
- ],
43
- outputs=gr.Image(type="pil", label="Detection Result"),
44
- title="Guitar and Wristwatch Detection",
45
- description="Upload an image or use examples to detect guitars and wristwatches.",
46
- examples=[
47
- ["testImages/acoustic_guitar_005.jpg"],
48
- ["testImages/acoustic_guitar_006.jpg"],
49
- ["testImages/analog_watch_024.jpg"],
50
- ["testImages/analog_watch_025.jpg"]
51
- ] if os.path.exists("testImages/acoustic_guitar_005.jpg") else None
52
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  if __name__ == "__main__":
55
  demo.launch(share=True)
 
21
 
22
  def predict(pilimg, conf_threshold, iou_threshold):
23
  try:
24
+ if pilimg is None:
25
+ return None
26
  model = YOLO(MODEL_PATH, task='detect')
 
 
27
  result = model.predict(pilimg, conf=conf_threshold, iou=iou_threshold)
28
  img_bgr = result[0].plot()
29
+ return Image.fromarray(img_bgr[..., ::-1])
30
  except Exception as e:
31
  print(f"Error during prediction: {str(e)}")
32
  return None
33
 
34
+ with gr.Blocks() as demo:
35
+ # Header
36
+ gr.HTML("""
37
+ <div style="text-align: center; margin-bottom: 20px; padding: 20px; background-color: #f7f7f7;">
38
+ <h1 style="font-size: 2.5em; color: #2196F3; margin-bottom: 10px;">Guitar & Watch Detection</h1>
39
+ <p style="font-size: 1.2em; color: #666;">Powered by YOLOv8 </p>
40
+ </div>
41
+ """)
42
+
43
+ # Hero section with image and description side by side
44
+ with gr.Row():
45
+ if os.path.exists("testImages/heroimage.png"):
46
+ with gr.Column(scale=1):
47
+ gr.Image("testImages/heroimage.png",
48
+ label="Watch Detection",
49
+ show_label=False,
50
+ elem_id="hero-image")
51
+
52
+ with gr.Column(scale=1):
53
+ gr.Markdown("""
54
+ ### 🎯 About This App
55
+ This application uses Yolo object detection to identify guitars and wristwatches in images.
56
+ The model is optimized using small model '8s' for faster inference.
57
+
58
+ ### πŸ“ How to Use
59
+ 1. Upload an image or use one of the examples below
60
+ 2. Adjust the confidence and IOU thresholds if needed
61
+ 3. View the detection results in real-time
62
+ """)
63
+
64
+ with gr.Row():
65
+ with gr.Column():
66
+ input_image = gr.Image(
67
+ type="pil",
68
+ label="πŸ“Έ Upload Image"
69
+ )
70
+
71
+ gr.Markdown("### βš™οΈ Detection Parameters")
72
+ with gr.Column(visible=True):
73
+ conf_threshold = gr.Slider(
74
+ minimum=0.1,
75
+ maximum=1.0,
76
+ step=0.05,
77
+ value=0.5,
78
+ label="🎯 Confidence Threshold",
79
+ info="Higher values mean more confident detections"
80
+ )
81
+ iou_threshold = gr.Slider(
82
+ minimum=0.1,
83
+ maximum=1.0,
84
+ step=0.05,
85
+ value=0.6,
86
+ label="πŸ”„ IOU Threshold",
87
+ info="Higher values mean less box overlap"
88
+ )
89
+
90
+ with gr.Column():
91
+ output_image = gr.Image(
92
+ type="pil",
93
+ label="πŸŽ‰ Detection Result"
94
+ )
95
+
96
+ # Examples section
97
+ gr.Markdown("### πŸ“š Example Images")
98
+ if os.path.exists("testImages/acoustic_guitar_005.jpg"):
99
+ examples = gr.Examples(
100
+ examples=[
101
+ ["testImages/acoustic_guitar_005.jpg"],
102
+ ["testImages/acoustic_guitar_006.jpg"],
103
+ ["testImages/analog_watch_024.jpg"],
104
+ ["testImages/analog_watch_025.jpg"]
105
+ ],
106
+ inputs=input_image,
107
+ label="Try these examples"
108
+ )
109
+
110
+ # Footer
111
+ gr.HTML("""
112
+ <div style="text-align: center; margin-top: 30px; padding: 20px; background-color: #f7f7f7;">
113
+ <p style="color: #666; font-size: 0.9em;">Β© 2025 Guitar & Watch Detection - 9027941P - Manikandan Sadhasivam</p>
114
+ </div>
115
+ """)
116
+
117
+ # Set up the prediction functionality
118
+ input_image.change(
119
+ fn=predict,
120
+ inputs=[input_image, conf_threshold, iou_threshold],
121
+ outputs=output_image
122
+ )
123
+
124
+ # Add custom CSS to resize the hero image
125
+ demo.css = """
126
+ #hero-image img {
127
+ width: 560px; /* Adjust the width to make the image smaller */
128
+ height: auto;
129
+ }
130
+ """
131
 
132
  if __name__ == "__main__":
133
  demo.launch(share=True)
testImages/heroimage.png ADDED