Skip to content

Commit

Permalink
update new files
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhaoliang committed Jul 10, 2020
1 parent 866ca6b commit bff0498
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
56 changes: 56 additions & 0 deletions base_thread.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from queue import Queue
import cv2
from PyQt5.QtCore import (QThread, QTime, QMutex, pyqtSignal, QMutexLocker)

from structures import ThreadStatisticsData


class BaseThread(QThread):

"""
Base class for all types of threads (capture, processing, stitching, ...,
etc). Mainly for collecting statistics of this thread.
"""

FPS_STAT_QUEUE_LENGTH = 32

update_statistics_gui = pyqtSignal(ThreadStatisticsData)

def __init__(self, parent=None):
super(BaseThread, self).__init__(parent)
self.init_commons()

def init_commons(self):
self.stopped = False
self.stop_mutex = QMutex()
self.clock = QTime()
self.fps = Queue()
self.processing_time = 0
self.processing_mutex = QMutex()
self.sample_count = 0
self.fps_sum = 0
self.stat_data = ThreadStatisticsData()

def stop(self):
with QMutexLocker(self.stop_mutex):
self.stopped = True

def update_fps(self, dt):
# add instantaneous fps value to queue
if dt > 0:
self.fps.put(1000 / dt)
self.sample_count += 1

# discard redundant items in the fps queue
if self.fps.qsize() > self.FPS_STAT_QUEUE_LENGTH:
self.fps.get()

# update statistics
if self.fps.qsize() == self.sample_count == self.FPS_STAT_QUEUE_LENGTH:
while not self.fps.empty():
self.fps_sum += self.fps.get()

self.stat_data.average_fps = round(self.fps_sum / self.FPS_STAT_QUEUE_LENGTH, 2)
# reset sum and sample number
self.fps_sum = 0
self.sample_count = 0
12 changes: 12 additions & 0 deletions structures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class ImageFrame(object):

def __init__(self, timestamp, image):
self.timestamp = timestamp
self.image = image


class ThreadStatisticsData(object):

def __init__(self):
self.average_fps = 0
self.frames_processed_count = 0

0 comments on commit bff0498

Please sign in to comment.