-
Notifications
You must be signed in to change notification settings - Fork 0
/
bg_subtractor.py
63 lines (51 loc) · 2.3 KB
/
bg_subtractor.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
import cv2
import _thread
from queue import Queue
from threading import Lock
import time
###############SETTINGS##############################
NUM_WORKERS = 5 #Number of worker threads
LEARN_HISTORY = 600
THRESHOLD = 2000
SHADOW_THRESHOLD = 0.1
#####################################################
frames = Queue(10) #init queue of frams and bg masks
cap_locks = [Lock() for i in range(0, NUM_WORKERS)] #init mutexes for capture
push_locks = [Lock() for i in range(0, NUM_WORKERS)] #init mutexes for push to queue
def init_capture():
#inicializating capture
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
cap.set(cv2.CAP_PROP_SATURATION, 100) #set SATURATION 0 - 100
return cap
def init_bg_subtractor():
#inicializating bg_subtractor
bg_subtractor = cv2.createBackgroundSubtractorKNN()
bg_subtractor.setShadowThreshold(SHADOW_THRESHOLD)
bg_subtractor.setDetectShadows(True)
bg_subtractor.setDist2Threshold(THRESHOLD)
bg_subtractor.setHistory(LEARN_HISTORY)
bg_subtractor.setShadowValue(0)
return bg_subtractor
def worker(index, bg_subtractor, cap):
#workers to make bg substractr
while True:
cap_locks[index].acquire(True) #mutex to read frame
_ , frame = cap.read() #read frame
cap_locks[(index + 1) % NUM_WORKERS].release() #post mutex to read for next worker
result = bg_subtractor.apply(frame) #calculate bg substrat
push_locks[index].acquire(True) #mutex to push readed freme and fg mask
frames.put((frame, result, time.time()), block=True) #result push to queue
push_locks[(index + 1) % NUM_WORKERS].release() #post mutex to push for next worker
def start_threads():
#function to start all thread of program
cap = init_capture()
bg_subtractor = init_bg_subtractor()
#start all workers
for i in range(0, NUM_WORKERS):
cap_locks[i].acquire(True)
push_locks[i].acquire(True)
_thread.start_new_thread(worker, (i, bg_subtractor, cap))
cap_locks[0].release()
push_locks[0].release()