tingul4 commited on
Commit
72c908c
·
1 Parent(s): 08d6fec

1. update README

Browse files

2. adjust sample image location
3. adjust some display texts in app.py

Files changed (3) hide show
  1. README.md +105 -1
  2. app.py +4 -5
  3. car.jpg → sample_images/car.jpg +0 -0
README.md CHANGED
@@ -9,4 +9,108 @@ app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  pinned: false
10
  ---
11
 
12
+ # 🎯 Text-Guided Image Segmentation Demo
13
+
14
+ 基於 **Grounding DINO** 和 **SAM (Segment Anything Model)** 的文字引導圖片分割應用,使用 Gradio 構建互動式介面。
15
+
16
+ ## 🚀 快速開始
17
+
18
+ ### 安裝依賴
19
+
20
+ ```bash
21
+ pip install -r requirements.txt
22
+ ```
23
+
24
+ ### 運行應用
25
+
26
+ ```bash
27
+ gradio app.py
28
+ ```
29
+
30
+
31
+
32
+ ```bash
33
+ python app.py
34
+ ```
35
+
36
+ 應用將在 `http://localhost:7860` 啟動。
37
+
38
+ ## 📦 依賴項
39
+
40
+ - `gradio` - 互動式 Web 介面
41
+ - `transformers` - Hugging Face 模型庫
42
+ - `torch` - PyTorch 深度學習框架
43
+ - `Pillow` - 圖像處理
44
+ - `numpy` - 數值計算
45
+
46
+ ## 🎮 使用方法
47
+
48
+ 1. **上傳圖片**:點擊或拖拽圖片到輸入區域
49
+ 2. **輸入文字提示**:
50
+ - 單個物件:`car`、`person`、`sky`
51
+ - 多個物件:`car. sky. road.`(用句號分隔)
52
+ 3. **點擊 Segment 按鈕**:開始分割
53
+ 4. **查看結果**:
54
+ - 左側顯示分割遮罩(不同物件用不同顏色)
55
+ - 右側顯示除錯資訊(檢測數量、標籤、信心度)
56
+
57
+ ## 🎨 顏色標示
58
+
59
+ 檢測到的物件會按順序使用以下顏色作為mask:
60
+
61
+ 1. 🔴 紅色
62
+ 2. 🟢 綠色
63
+ 3. 🔵 藍色
64
+ 4. 🟡 黃色
65
+
66
+ ## 🔧 技術架構
67
+
68
+ ### 模型
69
+
70
+ - **Grounding DINO** (`IDEA-Research/grounding-dino-base`)
71
+ - 用於零樣本物件檢測
72
+ - 根據文字描述定位物件
73
+
74
+ - **SAM** (`facebook/sam-vit-base`)
75
+ - 用於精確分割
76
+ - 基於檢測框生成高質量遮罩
77
+
78
+ ### 工作流程
79
+
80
+ ```
81
+ 輸入圖片 + 文字提示
82
+
83
+ Grounding DINO 檢測物件
84
+
85
+ SAM 生成分割遮罩
86
+
87
+ 多物件遮罩疊加(不同顏色)
88
+
89
+ 輸出結果
90
+ ```
91
+
92
+ ## 💡 使用技巧
93
+
94
+ ### 提高檢測準確度
95
+
96
+ 1. **使用具體描述**:`blue car` 比 `car` 更精確
97
+ 2. **調整閾值**:當前閾值為 0.15,可在 source code 中調整
98
+ 3. **多次嘗試**:嘗試不同的文字表達方式或是使用英文描述
99
+
100
+ ## 📝 程式碼結構
101
+
102
+ ```
103
+ text-image-seg/
104
+ ├── app.py
105
+ ├── requirements.txt
106
+ ├── README.md
107
+ ├── sample_images/
108
+ ├── .gitattributes
109
+ └── .gitignore
110
+ ```
111
+
112
+ ## 🔗 相關資源
113
+
114
+ - [Grounding DINO](https://github.com/IDEA-Research/GroundingDINO)
115
+ - [Segment Anything Model (SAM)](https://github.com/facebookresearch/segment-anything)
116
+ - [Gradio 文檔](https://www.gradio.app/docs)
app.py CHANGED
@@ -142,8 +142,8 @@ def segment_and_display(image, text):
142
 
143
  result_image, debug_info = segment_image_with_text(image, text)
144
 
145
- output_text = f"檢測到 {debug_info.get('num_detections', 0)} 個物件\n"
146
- output_text += f"原始文字提示: '{debug_info.get('original_prompt', text)}'\n"
147
 
148
  if 'labels' in debug_info and len(debug_info['labels']) > 0:
149
  output_text += "檢測結果:\n"
@@ -151,7 +151,6 @@ def segment_and_display(image, text):
151
  color_map = ['紅色', '綠色', '藍色', '黃色']
152
  color_name = color_map[i % len(color_map)]
153
  output_text += f" {i+1}. {label} (信心度: {score:.2f}, 顏色: {color_name})\n"
154
-
155
  if 'error' in debug_info:
156
  output_text += f"\n錯誤: {debug_info['error']}"
157
 
@@ -159,7 +158,7 @@ def segment_and_display(image, text):
159
 
160
 
161
  with gr.Blocks() as demo:
162
- gr.Markdown("# Text-Guided Image Segmentation")
163
  gr.Markdown("""
164
  ### 使用說明
165
  1. 上傳一張圖片 (有提供預設圖片方便 demo)
@@ -170,7 +169,7 @@ with gr.Blocks() as demo:
170
  with gr.Row():
171
  with gr.Column(scale=1):
172
  # 預設圖片 car.jpg
173
- image_input = gr.Image(label="Input Image", value="car.jpg")
174
  text_input = gr.Textbox(
175
  label="Text Prompt",
176
  placeholder="e.g. 'car. sky. road.'",
 
142
 
143
  result_image, debug_info = segment_image_with_text(image, text)
144
 
145
+ output_text = f"檢測到 {debug_info.get('num_detections', 0)} 個物件\n---\n"
146
+ output_text += f"輸入文字: '{debug_info.get('original_prompt', text)}'\n---\n"
147
 
148
  if 'labels' in debug_info and len(debug_info['labels']) > 0:
149
  output_text += "檢測結果:\n"
 
151
  color_map = ['紅色', '綠色', '藍色', '黃色']
152
  color_name = color_map[i % len(color_map)]
153
  output_text += f" {i+1}. {label} (信心度: {score:.2f}, 顏色: {color_name})\n"
 
154
  if 'error' in debug_info:
155
  output_text += f"\n錯誤: {debug_info['error']}"
156
 
 
158
 
159
 
160
  with gr.Blocks() as demo:
161
+ gr.Markdown("# Text-Guided Image Segmentation Demo")
162
  gr.Markdown("""
163
  ### 使用說明
164
  1. 上傳一張圖片 (有提供預設圖片方便 demo)
 
169
  with gr.Row():
170
  with gr.Column(scale=1):
171
  # 預設圖片 car.jpg
172
+ image_input = gr.Image(label="Input Image", value="sample_images/car.jpg")
173
  text_input = gr.Textbox(
174
  label="Text Prompt",
175
  placeholder="e.g. 'car. sky. road.'",
car.jpg → sample_images/car.jpg RENAMED
File without changes