refactored threading

This commit is contained in:
Sai Subhakar T
2020-09-30 14:58:58 +02:00
parent 099beb3fba
commit 07dc0bfe2b
5 changed files with 61 additions and 370 deletions

View File

@@ -4,9 +4,13 @@
# Enter mail below to receive real-time email alerts
# e.g., 'email@gmail.com'
MAIL = ''
# Enter the ip camera url (e.g., url = 'http://191.138.0.100:8040/video')
url = ''
# ON/OFF for mail feature. Enter True to turn on the email alert feature.
ALERT = False
# Threading ON/OFF
Thread = False
# Simple log to log the counting data
Log = False

28
mylib/thread.py Normal file
View File

@@ -0,0 +1,28 @@
import cv2, threading, queue
class ThreadingClass:
# initiate threading class
def __init__(self, name):
self.cap = cv2.VideoCapture(name)
# define an empty queue and thread
self.q = queue.Queue()
t = threading.Thread(target=self._reader)
t.daemon = True
t.start()
# read the frames as soon as they are available
# this approach removes OpenCV's internal buffer and reduces the frame lag
def _reader(self):
while True:
ret, frame = self.cap.read() # read the frames and ---
if not ret:
break
if not self.q.empty():
try:
self.q.get_nowait()
except queue.Empty:
pass
self.q.put(frame) # --- store them in a queue (instead of the buffer)
def read(self):
return self.q.get() # fetch frames from the queue one by one