Spaces:
Runtime error
Runtime error
Commit
•
e94fae2
1
Parent(s):
ea9f0e0
added output image
Browse files- app.py +3 -3
- detector.py +32 -13
app.py
CHANGED
@@ -2,13 +2,13 @@ import gradio as gr
|
|
2 |
from detector import bat_detector
|
3 |
|
4 |
def find_bats(input_img):
|
5 |
-
|
6 |
-
return f"{
|
7 |
|
8 |
iface = gr.Interface(
|
9 |
fn=find_bats,
|
10 |
inputs=gr.Image(shape=(640, 512)),
|
11 |
-
outputs='text'
|
12 |
)
|
13 |
|
14 |
iface.launch()
|
|
|
2 |
from detector import bat_detector
|
3 |
|
4 |
def find_bats(input_img):
|
5 |
+
after_image, total_bats = bat_detector(input_img)
|
6 |
+
return f"{total_bats}" + " bats found in " + f"{time:.2f}s", after_image
|
7 |
|
8 |
iface = gr.Interface(
|
9 |
fn=find_bats,
|
10 |
inputs=gr.Image(shape=(640, 512)),
|
11 |
+
outputs=['text', gr.Image(shape=(960, 768))]
|
12 |
)
|
13 |
|
14 |
iface.launch()
|
detector.py
CHANGED
@@ -63,20 +63,39 @@ def find_bats(allImgs):
|
|
63 |
|
64 |
return cropped_bats
|
65 |
|
66 |
-
def
|
67 |
-
|
68 |
-
|
69 |
-
cropped_bats = find_bats(allImgs)
|
70 |
-
|
71 |
learn = load_learner("model_densenet_zeros.pkl")
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
-
return
|
|
|
|
63 |
|
64 |
return cropped_bats
|
65 |
|
66 |
+
def find_bats(allImgs, learn):
|
67 |
+
total_bats, batDepthMin,batDepthMax = 0, 50, 400
|
68 |
+
allImgAfter = []
|
|
|
|
|
69 |
learn = load_learner("model_densenet_zeros.pkl")
|
70 |
|
71 |
+
for img in allImgs:
|
72 |
+
blobs = cv.findContours(img[1], cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)[-2]
|
73 |
+
after = img[0].copy()
|
74 |
+
# Process the blobs
|
75 |
+
for blob in blobs:
|
76 |
+
if batDepthMin < cv.contourArea(blob) < batDepthMax: # Only process blobs with a min / max size
|
77 |
+
rect = cv.minAreaRect(blob)
|
78 |
+
box = cv.boxPoints(rect)
|
79 |
+
box = np.int0(box)
|
80 |
+
cropped_bat = crop_bat(img[0], box)
|
81 |
+
with learn.no_bar(), learn.no_logging():
|
82 |
+
label, _, probs = learn.predict(cropped_bat)
|
83 |
+
p_not_bat=f"{probs[0]:.4f}"
|
84 |
+
p_bat=f"{probs[1]:.4f}"
|
85 |
+
if p_not_bat < '0.9' and p_bat > '0.5':
|
86 |
+
cv.drawContours(after, [box], 0, (0,0,255),1)
|
87 |
+
total_bats += 1
|
88 |
+
allImgAfter.append(after)
|
89 |
+
|
90 |
+
return allImgAfter, total_bats
|
91 |
|
92 |
+
def bat_detector(img):
|
93 |
+
allImgs = chop_img(img, 3)
|
94 |
+
allImgsAfter, total_bats = find_bats(allImgs)
|
95 |
+
img_row_after_1 = cv.hconcat([allImgsAfter[0],allImgsAfter[1],allImgsAfter[2]])
|
96 |
+
img_row_after_2 = cv.hconcat([allImgsAfter[3],allImgsAfter[4],allImgsAfter[5]])
|
97 |
+
img_row_after_3 = cv.hconcat([allImgsAfter[6],allImgsAfter[7],allImgsAfter[8]])
|
98 |
+
img_concat_after = cv.resize(cv.vconcat([img_row_after_1, img_row_after_2, img_row_after_3]), (960, 768))
|
99 |
|
100 |
+
return img_concat_after, total_bats
|
101 |
+
|