DawnC commited on
Commit
48169b5
1 Parent(s): bede3ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -24
app.py CHANGED
@@ -195,7 +195,23 @@ async def predict_single_dog(image):
195
 
196
  # return dogs
197
 
198
- async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.6):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
200
  dogs = []
201
  boxes = []
@@ -217,32 +233,11 @@ async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.6):
217
 
218
  if is_valid_dog(cropped_image):
219
  dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
 
 
220
 
221
  return dogs
222
 
223
- def is_valid_dog(image):
224
- img_array = np.array(image)
225
-
226
- # 1. 簡單的紋理檢測
227
- gray = np.mean(img_array, axis=2)
228
- texture = np.std(gray)
229
-
230
- # 2. 顏色分布檢測
231
- img_rgb = img_array.reshape(-1, 3)
232
- kmeans = KMeans(n_clusters=3, n_init=10)
233
- kmeans.fit(img_rgb)
234
- colors = kmeans.cluster_centers_
235
- color_variety = np.std(colors)
236
-
237
- # 3. 形狀檢測(檢查長寬比和面積)
238
- aspect_ratio = image.width / image.height
239
- area_ratio = (image.width * image.height) / (image.width * image.height)
240
-
241
- # 放寬判斷條件
242
- if (texture > 5 and color_variety > 20 and
243
- 0.3 < aspect_ratio < 3 and area_ratio > 0.05):
244
- return True
245
- return False
246
 
247
  def merge_overlapping_boxes(boxes, overlap_threshold):
248
  merged = []
 
195
 
196
  # return dogs
197
 
198
+
199
+ def filter_detections(dogs, image_size):
200
+ filtered_dogs = []
201
+ image_area = image_size[0] * image_size[1]
202
+
203
+ for dog in dogs:
204
+ _, _, box = dog
205
+ dog_area = (box[2] - box[0]) * (box[3] - box[1])
206
+ area_ratio = dog_area / image_area
207
+
208
+ if 0.01 < area_ratio < 0.9: # 過濾掉太小或太大的檢測框
209
+ filtered_dogs.append(dog)
210
+
211
+ return filtered_dogs
212
+
213
+
214
+ async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.4):
215
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
216
  dogs = []
217
  boxes = []
 
233
 
234
  if is_valid_dog(cropped_image):
235
  dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
236
+
237
+ dogs = filter_detections(dogs, (image.width, image.height))
238
 
239
  return dogs
240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
 
242
  def merge_overlapping_boxes(boxes, overlap_threshold):
243
  merged = []