-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdetector_bgadaptive.py
79 lines (66 loc) · 1.79 KB
/
detector_bgadaptive.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
68
69
70
71
72
73
74
75
76
77
78
import cv, cv2
import rectangle
from hand import Hand
import numpy as np
import geom
from bench import benchmark
class DetectorAdaptive:
"""
Hand extraction using background subtraction
"""
def __init__(self, config, img, roi):
self.config = config
self.roi = roi
history = 500
nGauss = 2
bgThresh = 0.01
noise = 1
self.mog = cv2.BackgroundSubtractorMOG(history,nGauss,bgThresh,noise)
# Minimum and maximum angles between two fingers
self.min_finger_angle = 10
self.max_finger_angle = 90
# Minimum and maximum length of finger
self.min_finger_len = 10
self.max_finger_len = 100
# The tip of a finger must be above the palm
# (or slightly below, when it comes to the thumb)
self.finger_orientation_thresh = -20
def crop(self, img, roi):
"""
Crop an image
"""
x1, y1, x2, y2 = roi
# Remember the location of the
# cropped region in the image
self.offset = (x1, y1)
return img[y1:y2,x1:x2]
def preprocess(self, img, roi):
"""
The preprocessing step prepares the image to improve detection probability.
"""
#img = self.crop(img, roi)
# TODO: Try adaptive threshold
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return img
#@benchmark
def detect(self, img, roi):
"""
Find hand object (fingers, contours) in region of interest
"""
# Only consider region of interest
img = self.preprocess(img, roi)
background = self.mog.apply(img)
cv2.imshow("BG", background)
cv2.waitKey(300)
return background
def train(self, img):
"""
Train the detector with a special test image. This improves the following
detection results.
"""
pass
def set_config(config):
"""
Load new settings at runtime
"""
self.config = config