-
Notifications
You must be signed in to change notification settings - Fork 5
/
object_detector.py
31 lines (22 loc) · 930 Bytes
/
object_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
import cv2
class detectorObj():
def __init__(self):
pass
def detect_objects(self, frame):
# Convert Image to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Create a Mask with adaptive threshold
mask = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 19, 5)
# Find contours
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#cv2.imshow("mask", mask)
objects_contours = []
for cnt in contours:
area = cv2.contourArea(cnt)
if area > 2000:
#cnt = cv2.approxPolyDP(cnt, 0.03*cv2.arcLength(cnt, True), True)
objects_contours.append(cnt)
return objects_contours
# def get_objects_rect(self):
# box = cv2.boxPoints(rect) # cv2.boxPoints(rect) for OpenCV 3.x
# box = np.int0(box)