-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocman.py
More file actions
58 lines (47 loc) · 1.96 KB
/
procman.py
File metadata and controls
58 lines (47 loc) · 1.96 KB
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
# To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.
import multiprocessing as mp
import Queue
import cv2
import cv2.aruco as aruco
class ProcessManager:
def __init__(self):
self.resultsque = mp.Queue() # Queue of scanning results
self.maxprocesses = mp.cpu_count()
self.procpool = [] # Pool of processes
self.dataque = mp.Queue() # Data waiting to be processed.
self.aruco_dict = aruco.Dictionary_get(aruco.DICT_4X4_50)
self.parameters = aruco.DetectorParameters_create()
def scanSubprocess(self, timestamp, timeout, image, offsetx, offsety, resultsque):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
self.corners, self.ids, self.rejectedImgPoints = aruco.detectMarkers(gray, self.aruco_dict, parameters=self.parameters)
resultsque.put([timestamp, self.corners, self.ids, self.rejectedImgPoints])
resultsque.close()
def removeFinished(self):
# Remove finished processes from the pool.
for p in self.procpool:
if not p.is_alive():
self.procpool.remove(p)
def addProcess(self, timestamp, timeout, image, xoffset = 0, yoffset = 0):
# Add process to the pool
if len(self.procpool) < self.maxprocesses:
p = mp.Process(target=self.scanSubprocess, args=(timestamp, timeout, image, xoffset, yoffset, self.resultsque))
p.start()
self.procpool.append(p)
print "Process launched."
return True
else:
# Pool is full.
print "Pool is full."
pass
return False
def resultsAvailable(self):
return not self.resultsque.empty()
def getResult(self):
try:
r = self.resultsque.get(False)
except Queue.Empty:
print "no results in queue."
r = []
return r