muhammadhamza-stack commited on
Commit
714b8fd
·
1 Parent(s): c89f1d9

refine the gradio app

Browse files
Files changed (9) hide show
  1. .gitattributes +1 -0
  2. .gitignore +1 -2
  3. app.py +108 -18
  4. examples/01.jpg +0 -0
  5. examples/03.jpg +0 -0
  6. examples/04.jpg +0 -0
  7. examples/05.jpg +0 -0
  8. examples/06.jpg +0 -0
  9. requirements.txt +2 -1
.gitattributes CHANGED
@@ -1,2 +1,3 @@
1
  examples/02.jpg filter=lfs diff=lfs merge=lfs -text
2
  best.pt filter=lfs diff=lfs merge=lfs -text
 
 
1
  examples/02.jpg filter=lfs diff=lfs merge=lfs -text
2
  best.pt filter=lfs diff=lfs merge=lfs -text
3
+ *.jpg filter=lfs diff=lfs merge=lfs -text
.gitignore CHANGED
@@ -1,2 +1 @@
1
- best.pt
2
- examples/
 
1
+ venv
 
app.py CHANGED
@@ -2,40 +2,130 @@ import gradio as gr
2
  from ultralytics import YOLO
3
  from PIL import Image
4
 
5
- model = YOLO("./best.pt")
6
 
 
 
 
7
 
8
- def process_img(img: gr.Image):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  result = model.predict(img)
 
 
10
  for r in result:
11
  im_bgr = r.plot()
12
- return gr.Image(
13
- label="Output Image with labels", value=Image.fromarray(im_bgr[..., ::-1])
14
- )
15
 
 
 
 
16
 
17
- with gr.Blocks() as demo:
18
- gr.Markdown(value="Port Classification App")
 
 
 
 
 
 
 
 
 
 
19
 
 
20
  with gr.Row():
21
  with gr.Column():
22
- upload_img = gr.Image(label="Upload Image", type="pil")
23
- classify_img_button = gr.Button(value="Process Image")
 
 
24
 
25
  with gr.Column():
26
- output_img = gr.Image(label="Output Image with labels")
 
 
 
 
27
  with gr.Row():
28
  gr.Examples(
29
  examples=[
30
- "./examples/01.jpg",
31
- "./examples/02.jpg",
32
- "./examples/03.jpg",
33
- "./examples/04.jpg",
34
- "./examples/05.jpg",
35
- "./examples/06.jpg",
36
  ],
37
- inputs=upload_img
 
38
  )
 
 
39
  classify_img_button.click(fn=process_img, inputs=upload_img, outputs=output_img)
40
 
41
- demo.launch()
 
 
2
  from ultralytics import YOLO
3
  from PIL import Image
4
 
5
+ # --- Documentation Strings ---
6
 
7
+ USAGE_GUIDELINES = """
8
+ ## 1. Quick Start Guide: Processing an Image
9
+ This application uses a specialized YOLO model for object detection to automatically locate and classify specific "Port" features within an image.
10
 
11
+ 1. **Upload Image**: Click the 'Upload Image' box and select your image file (JPG or PNG).
12
+ 2. **Run**: Click the **"Process Image"** button.
13
+ 3. **Review**: The 'Output Image with labels' will display the detected ports, marked by bounding boxes, class labels, and confidence scores.
14
+ """
15
+
16
+ INPUT_EXPLANATION = """
17
+ ## 2. Expected Inputs and Best Practices
18
+
19
+ | Input Field | Purpose | Requirement |
20
+ | :--- | :--- | :--- |
21
+ | **Upload Image** | The image containing the ports or features you wish to classify. | Must be a standard image file (JPG, PNG). |
22
+
23
+ ### Optimal Image Conditions
24
+ * **Clarity and Focus:** Ensure the ports are clearly visible and in focus. Blurry images significantly reduce detection accuracy.
25
+ * **Lighting:** Use well-lit images. Shadows or low-light conditions can cause the model to miss detections or misclassify objects.
26
+ * **Framing:** The ports should occupy a significant portion of the image. Highly zoomed-out images may treat ports as background noise.
27
+ """
28
+
29
+ OUTPUT_EXPLANATION = """
30
+ ## 3. Expected Output
31
+
32
+ The output image features graphical annotations provided by the AI model. Each annotation consists of three key components:
33
+
34
+ | Annotation Component | Description | Importance |
35
+ | :--- | :--- | :--- |
36
+ | **Bounding Box** | A colored rectangle drawn tightly around the detected object (the port). | Visually indicates the exact location of the object. |
37
+ | **Class Label** | The name of the predicted port type (e.g., USB-A, HDMI, Ethernet). | Represents the model's classification of the object within the box. |
38
+ | **Confidence Score** | A percentage (e.g., 0.92) representing the model's certainty in its prediction. | Scores below 0.50 (50%) should typically be treated as unreliable detections. |
39
+
40
+ """
41
+
42
+ TECHNICAL_TIPS = """
43
+ ## 4. Troubleshooting and Tips
44
+
45
+ * **No Detections:** If the output image is identical to the input with no boxes, the model either failed to find any objects or the confidence for all potential detections was too low to be displayed. Try improving the lighting or clarity of the input photo.
46
+ * **Testing with Examples:** Use the provided example images to quickly verify the application's functionality and see what types of objects the model is trained to recognize.
47
+ * **Processing Time:** Processing time depends on the image resolution and the complexity of the scene. Large images require more computational resources for detection.
48
+ """
49
+
50
+ # --------------------
51
+ # Core Pipeline Functions (Kept AS IS)
52
+ # --------------------
53
+
54
+ # Load the YOLO model weights
55
+ # Ensure 'best.pt' is available in the run directory
56
+ try:
57
+ model = YOLO("./best.pt")
58
+ except Exception as e:
59
+ print(f"Error loading model weights: {e}. Using a placeholder for demonstration.")
60
+ # Define a placeholder function if model loading fails
61
+ def placeholder_predict(img):
62
+ return Image.new('RGB', img.size, color='red')
63
+ model = placeholder_predict
64
+
65
+
66
+ def process_img(img: Image.Image):
67
+ if img is None:
68
+ gr.Warning("Please upload an image before processing.")
69
+ return None
70
+
71
+
72
+ # Perform prediction
73
  result = model.predict(img)
74
+
75
+ # r.plot() returns a BGR numpy array
76
  for r in result:
77
  im_bgr = r.plot()
78
+ # Convert BGR (OpenCV standard) to RGB (PIL/Gradio standard)
79
+ return Image.fromarray(im_bgr[..., ::-1])
80
+
81
 
82
+ # --------------------
83
+ # Gradio UI
84
+ # --------------------
85
 
86
+ with gr.Blocks(title="Port Classification App") as demo:
87
+ gr.Markdown("<h1 style='text-align: center;'> Port Classification and Object Detection </h1>")
88
+
89
+ # 1. Guidelines Accordion
90
+ with gr.Accordion("Tips & User Guidelines", open=False):
91
+ gr.Markdown(USAGE_GUIDELINES)
92
+ gr.Markdown("---")
93
+ gr.Markdown(INPUT_EXPLANATION)
94
+ gr.Markdown("---")
95
+ gr.Markdown(OUTPUT_EXPLANATION)
96
+ gr.Markdown("---")
97
+ gr.Markdown(TECHNICAL_TIPS)
98
 
99
+ # 2. Interface Definition
100
  with gr.Row():
101
  with gr.Column():
102
+ gr.Markdown("## Step 1: Upload Port Image ")
103
+ upload_img = gr.Image(label=" Upload Image", type="pil")
104
+ gr.Markdown("## Step 2: Click Process Image ")
105
+ classify_img_button = gr.Button(value=" Process Image", variant="primary")
106
 
107
  with gr.Column():
108
+ gr.Markdown("## Result ")
109
+ output_img = gr.Image(label=" Output Image with Detected Ports")
110
+
111
+ # 3. Examples Section
112
+ gr.Markdown("## Examples ")
113
  with gr.Row():
114
  gr.Examples(
115
  examples=[
116
+ ["./examples/01.jpg"],
117
+ ["./examples/02.jpg"],
118
+ ["./examples/03.jpg"],
119
+ ["./examples/04.jpg"],
120
+ ["./examples/05.jpg"],
121
+ ["./examples/06.jpg"],
122
  ],
123
+ inputs=upload_img,
124
+ label=" Click to load and test an example image"
125
  )
126
+
127
+ # Event Handler
128
  classify_img_button.click(fn=process_img, inputs=upload_img, outputs=output_img)
129
 
130
+ if __name__ == "__main__":
131
+ demo.launch()
examples/01.jpg CHANGED

Git LFS Details

  • SHA256: 0d4b53b04bcd69c0504af218c184e12165300fb276ded74dc198060f48cf9bb0
  • Pointer size: 130 Bytes
  • Size of remote file: 23.7 kB
examples/03.jpg CHANGED

Git LFS Details

  • SHA256: 260c58ad9b007266870b8aa1edf249d82fd13c65adb3907e9e53beb9cd3822f6
  • Pointer size: 131 Bytes
  • Size of remote file: 709 kB
examples/04.jpg CHANGED

Git LFS Details

  • SHA256: 53a795a0ca6188066b88f25a2fb963d63c45830006d1c32f0c9a90e15678c611
  • Pointer size: 131 Bytes
  • Size of remote file: 689 kB
examples/05.jpg CHANGED

Git LFS Details

  • SHA256: 657989fd2485e07dda5af7fd9297becf8fd1052630aa43dd09d98489866678f0
  • Pointer size: 131 Bytes
  • Size of remote file: 717 kB
examples/06.jpg CHANGED

Git LFS Details

  • SHA256: 3713a4682bc38b0cf3aafd8bc2bf1e175ad6839d1142f9b0f9572237468bea57
  • Pointer size: 131 Bytes
  • Size of remote file: 552 kB
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  ultralytics
2
  pillow
3
- gradio
 
 
1
  ultralytics
2
  pillow
3
+ gradio==3.50.2
4
+ gradio-client==0.6.1