|
8 | 8 | import time |
9 | 9 | import warnings |
10 | 10 | from multiprocessing import Process |
| 11 | +from threading import Thread |
| 12 | +from queue import Queue |
11 | 13 |
|
12 | 14 | sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), ".")) |
13 | 15 |
|
|
33 | 35 |
|
34 | 36 | opt = parser.parse_args() |
35 | 37 |
|
| 38 | +MODE = SharedOptions.MODE |
| 39 | +SHARED_APP_DIR = SharedOptions.SHARED_APP_DIR |
| 40 | +CUDA_MODE = SharedOptions.CUDA_MODE |
| 41 | +db = SharedOptions.db |
| 42 | +TEMP_PATH = SharedOptions.TEMP_PATH |
36 | 43 |
|
37 | | -def objectdetection(thread_name: str, delay: float): |
| 44 | +if opt.name == None: |
| 45 | + IMAGE_QUEUE = "detection_queue" |
| 46 | +else: |
| 47 | + IMAGE_QUEUE = opt.name + "_queue" |
38 | 48 |
|
39 | | - MODE = SharedOptions.MODE |
40 | | - SHARED_APP_DIR = SharedOptions.SHARED_APP_DIR |
41 | | - CUDA_MODE = SharedOptions.CUDA_MODE |
42 | | - db = SharedOptions.db |
43 | | - TEMP_PATH = SharedOptions.TEMP_PATH |
| 49 | +if opt.model == None: |
| 50 | + model_path = os.path.join( |
| 51 | + SHARED_APP_DIR, SharedOptions.SETTINGS.DETECTION_MODEL |
| 52 | + ) |
| 53 | +else: |
| 54 | + model_path = opt.model |
44 | 55 |
|
45 | | - if opt.name == None: |
46 | | - IMAGE_QUEUE = "detection_queue" |
47 | | - else: |
48 | | - IMAGE_QUEUE = opt.name + "_queue" |
| 56 | +if MODE == "High": |
49 | 57 |
|
50 | | - if opt.model == None: |
51 | | - model_path = os.path.join( |
52 | | - SHARED_APP_DIR, SharedOptions.SETTINGS.DETECTION_MODEL |
53 | | - ) |
54 | | - else: |
55 | | - model_path = opt.model |
| 58 | + reso = SharedOptions.SETTINGS.DETECTION_HIGH |
56 | 59 |
|
57 | | - if MODE == "High": |
| 60 | +elif MODE == "Medium": |
58 | 61 |
|
59 | | - reso = SharedOptions.SETTINGS.DETECTION_HIGH |
| 62 | + reso = SharedOptions.SETTINGS.DETECTION_MEDIUM |
60 | 63 |
|
61 | | - elif MODE == "Medium": |
| 64 | +elif MODE == "Low": |
62 | 65 |
|
63 | | - reso = SharedOptions.SETTINGS.DETECTION_MEDIUM |
| 66 | + reso = SharedOptions.SETTINGS.DETECTION_LOW |
64 | 67 |
|
65 | | - elif MODE == "Low": |
| 68 | +detector = YOLODetector(model_path, reso, cuda=CUDA_MODE) |
66 | 69 |
|
67 | | - reso = SharedOptions.SETTINGS.DETECTION_LOW |
68 | | - |
69 | | - detector = YOLODetector(model_path, reso, cuda=CUDA_MODE) |
| 70 | +def run_task(q): |
70 | 71 | while True: |
71 | | - queue = db.lrange(IMAGE_QUEUE, 0, 0) |
| 72 | + req_data = q.get() |
72 | 73 |
|
73 | | - db.ltrim(IMAGE_QUEUE, len(queue), -1) |
| 74 | + img_id = req_data["imgid"] |
| 75 | + req_id = req_data["reqid"] |
| 76 | + req_type = req_data["reqtype"] |
| 77 | + threshold = float(req_data["minconfidence"]) |
| 78 | + img_path = os.path.join(TEMP_PATH, img_id) |
74 | 79 |
|
75 | | - if len(queue) > 0: |
| 80 | + try: |
| 81 | + det = detector.predict(img_path, threshold) |
76 | 82 |
|
77 | | - for req_data in queue: |
| 83 | + outputs = [] |
78 | 84 |
|
79 | | - req_data = json.JSONDecoder().decode(req_data) |
| 85 | + for *xyxy, conf, cls in reversed(det): |
| 86 | + x_min = xyxy[0] |
| 87 | + y_min = xyxy[1] |
| 88 | + x_max = xyxy[2] |
| 89 | + y_max = xyxy[3] |
| 90 | + score = conf.item() |
| 91 | + |
| 92 | + label = detector.names[int(cls.item())] |
| 93 | + |
| 94 | + detection = { |
| 95 | + "confidence": score, |
| 96 | + "label": label, |
| 97 | + "x_min": int(x_min), |
| 98 | + "y_min": int(y_min), |
| 99 | + "x_max": int(x_max), |
| 100 | + "y_max": int(y_max), |
| 101 | + } |
| 102 | + |
| 103 | + outputs.append(detection) |
80 | 104 |
|
81 | | - img_id = req_data["imgid"] |
82 | | - req_id = req_data["reqid"] |
83 | | - req_type = req_data["reqtype"] |
84 | | - threshold = float(req_data["minconfidence"]) |
85 | | - img_path = os.path.join(TEMP_PATH, img_id) |
| 105 | + output = {"success": True, "predictions": outputs} |
86 | 106 |
|
87 | | - try: |
88 | | - det = detector.predict(img_path, threshold) |
| 107 | + except UnidentifiedImageError: |
| 108 | + err_trace = traceback.format_exc() |
| 109 | + print(err_trace, file=sys.stderr, flush=True) |
89 | 110 |
|
90 | | - outputs = [] |
| 111 | + output = { |
| 112 | + "success": False, |
| 113 | + "error": "invalid image file", |
| 114 | + "code": 400, |
| 115 | + } |
91 | 116 |
|
92 | | - for *xyxy, conf, cls in reversed(det): |
93 | | - x_min = xyxy[0] |
94 | | - y_min = xyxy[1] |
95 | | - x_max = xyxy[2] |
96 | | - y_max = xyxy[3] |
97 | | - score = conf.item() |
| 117 | + except Exception: |
98 | 118 |
|
99 | | - label = detector.names[int(cls.item())] |
| 119 | + err_trace = traceback.format_exc() |
| 120 | + print(err_trace, file=sys.stderr, flush=True) |
100 | 121 |
|
101 | | - detection = { |
102 | | - "confidence": score, |
103 | | - "label": label, |
104 | | - "x_min": int(x_min), |
105 | | - "y_min": int(y_min), |
106 | | - "x_max": int(x_max), |
107 | | - "y_max": int(y_max), |
108 | | - } |
| 122 | + output = { |
| 123 | + "success": False, |
| 124 | + "error": "error occured on the server", |
| 125 | + "code": 500, |
| 126 | + } |
109 | 127 |
|
110 | | - outputs.append(detection) |
| 128 | + finally: |
| 129 | + db.set(req_id, json.dumps(output)) |
| 130 | + if os.path.exists(img_path): |
| 131 | + os.remove(img_path) |
111 | 132 |
|
112 | | - output = {"success": True, "predictions": outputs} |
113 | 133 |
|
114 | | - except UnidentifiedImageError: |
115 | | - err_trace = traceback.format_exc() |
116 | | - print(err_trace, file=sys.stderr, flush=True) |
| 134 | +def objectdetection(delay: float): |
117 | 135 |
|
118 | | - output = { |
119 | | - "success": False, |
120 | | - "error": "invalid image file", |
121 | | - "code": 400, |
122 | | - } |
| 136 | + q = Queue(maxsize=0) |
123 | 137 |
|
124 | | - except Exception: |
| 138 | + for _ in range(SharedOptions.THREADCOUNT): |
| 139 | + worker = Thread(target=run_task, args=(q,)) |
| 140 | + worker.setDaemon(True) |
| 141 | + worker.start() |
125 | 142 |
|
126 | | - err_trace = traceback.format_exc() |
127 | | - print(err_trace, file=sys.stderr, flush=True) |
| 143 | + while True: |
| 144 | + queue = db.lrange(IMAGE_QUEUE, 0, 0) |
128 | 145 |
|
129 | | - output = { |
130 | | - "success": False, |
131 | | - "error": "error occured on the server", |
132 | | - "code": 500, |
133 | | - } |
| 146 | + db.ltrim(IMAGE_QUEUE, len(queue), -1) |
| 147 | + |
| 148 | + if len(queue) > 0: |
| 149 | + |
| 150 | + for req_data in queue: |
| 151 | + |
| 152 | + req_data = json.JSONDecoder().decode(req_data) |
134 | 153 |
|
135 | | - finally: |
136 | | - db.set(req_id, json.dumps(output)) |
137 | | - if os.path.exists(img_path): |
138 | | - os.remove(img_path) |
| 154 | + q.put(req_data) |
139 | 155 |
|
140 | 156 | time.sleep(delay) |
141 | | -if __name__ == "__main__": |
142 | | - p = Process(target=objectdetection, args=("", SharedOptions.SLEEP_TIME)) |
143 | | - p.start() |
| 157 | +if __name__ == "__main__": |
| 158 | + objectdetection(SharedOptions.SLEEP_TIME) |
| 159 | + |
144 | 160 |
|
0 commit comments