aboutsummaryrefslogtreecommitdiff
path: root/util.py
diff options
context:
space:
mode:
authorLeonard Kugis <leonard@kug.is>2023-03-22 04:22:29 +0100
committerLeonard Kugis <leonard@kug.is>2023-03-22 04:22:29 +0100
commita747254ece9da9af1517dd7dfdb4d16949b0eabb (patch)
tree9999a22523c2dc9a4473efb200b823943e3ca155 /util.py
parent7c635fed24bba638f10a73fe39134519ecd675bf (diff)
Implemented detail scanner with multithreading
Diffstat (limited to 'util.py')
-rw-r--r--util.py61
1 files changed, 60 insertions, 1 deletions
diff --git a/util.py b/util.py
index 2bf4ff8..9fca80c 100644
--- a/util.py
+++ b/util.py
@@ -4,6 +4,9 @@ import cv2
import platform
import readline
import os
+import numpy as np
+from queue import Queue
+from threading import Thread, Lock
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
# initialize the dimensions of the image to be resized and
@@ -36,6 +39,11 @@ def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
# return the resized image
return resized
+def image_embed(img, dimensions):
+ ret = np.zeros((dimensions[0], dimensions[1], 3), np.uint8)
+ ret[0:img.shape[0], 0:img.shape[1]] = img
+ return ret
+
'''
Fetch input prompt with prefilled text.
@@ -76,4 +84,55 @@ def open_system(file):
elif platform.system() == 'Windows': # Windows
os.startfile(file)
else: # linux variants
- subprocess.call(('xdg-open', file)) \ No newline at end of file
+ subprocess.call(('xdg-open', file))
+
+class Worker(Thread):
+ def __init__(self, tasks):
+ Thread.__init__(self)
+ self.tasks = tasks
+ self.daemon = True
+ self.lock = Lock()
+ self.start()
+
+ def run(self):
+ while True:
+ func, args, kargs = self.tasks.get()
+ try:
+ if func.lower() == "terminate":
+ break
+ except:
+ try:
+ with self.lock:
+ func(*args, **kargs)
+ except Exception as exception:
+ print(exception)
+ self.tasks.task_done()
+
+class ThreadPool:
+ def __init__(self, num_threads, num_queue=None):
+ if num_queue is None or num_queue < num_threads:
+ num_queue = num_threads
+ self.tasks = Queue(num_queue)
+ self.threads = num_threads
+ for _ in range(num_threads): Worker(self.tasks)
+
+ # This function can be called to terminate all the worker threads of the queue
+ def terminate(self):
+ self.wait_completion()
+ for _ in range(self.threads): self.add_task("terminate")
+ return None
+
+ # This function can be called to add new work to the queue
+ def add_task(self, func, *args, **kargs):
+ self.tasks.put((func, args, kargs))
+
+ # This function can be called to wait till all the workers are done processing the pending works. If this function is called, the main will not process any new lines unless all the workers are done with the pending works.
+ def wait_completion(self):
+ self.tasks.join()
+
+ # This function can be called to check if there are any pending/running works in the queue. If there are any works pending, the call will return Boolean True or else it will return Boolean False
+ def is_alive(self):
+ if self.tasks.unfinished_tasks == 0:
+ return False
+ else:
+ return True \ No newline at end of file