mirror of
https://github.com/rendies/People-Counting-in-Real-Time.git
synced 2025-10-29 19:43:03 +07:00
refactored threading
This commit is contained in:
@@ -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
28
mylib/thread.py
Normal 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
|
||||
Reference in New Issue
Block a user