forked from iceberg-project/Seals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob_detector.py
89 lines (48 loc) · 1.74 KB
/
blob_detector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Import packages
import cv2
import os
import pandas as pd
import numpy as np
from collections import defaultdict
# Setup the detector
params = cv2.SimpleBlobDetector_Params()
params.filterByArea = True
params.minArea = 8
params.maxArea = 35
params.thresholdStep = 18
params.filterByCircularity = True
params.minCircularity = 0.3
params.filterByConvexity = True
params.minConvexity = 0.6
params.filterByInertia = True
params.maxInertiaRatio = 0.1
params.maxInertiaRatio = 0.6
detector = cv2.SimpleBlobDetector_create(params)
# Classes with positive examples
pos_classes = ['crabeater', 'weddell']
# Save output in a dictionary
images = []
keyp = []
# Navigate to folders with seals
for folder in pos_classes:
for path, _, files in os.walk('./classified_images/{}/'.format(folder)):
for idx, filename in enumerate(files):
f = os.path.join(path, filename)
try:
image = cv2.imread(f, cv2.IMREAD_GRAYSCALE)
# Use detector to find keypoints in image
keypoints = detector.detect(image=image)
im_with_keypoints = cv2.drawKeypoints(image, keypoints, np.array([]), (0, 0, 255),
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
images.append(f)
keyp.append(keypoints)
cv2.imshow("Keypoints", im_with_keypoints)
cv2.waitKey(0)
except:
print('blob_detector: cv2.imread failed - Removed: ' + f)
os.system("rm \"" + f + "\"")
continue
out_df = pd.DataFrame({'image': images,
'key_points': keyp})
# write detections to csv file
out_df.to_csv('detections.csv')