Exemple #1
0
class Play:
    def __init__(self, source, setup):
        self.source = source
        self.setup = setup
        self.main_window = self.source.main_window
        self.notification = self.main_window.notification
        self.setup_timer = QtCore.QTimer(self.main_window)
        self.setup_timer.timeout.connect(self.run_setup)
        self.analysis_timer = QtCore.QTimer(self.main_window)
        self.analysis_timer.timeout.connect(self.run_analysis)
        self.setup_frame_no = 0
        self.analysis_frame_no = 0
        self.setup_status = False
        self.centers = []
        self.speed_debug = False
        self.road_status_debug = False
        self.top_speed = 0 
        self.top_density = 0
        self.haar_collect = False
        self.haar_activate = False
        # timer object
        self.timer = Timer()
        # knn
        self.tracks = []

    def run_setup(self):
        try:
            self.setup_frame_no = self.setup_frame_no + 1
            self.source.setup_get_next_frame()
            # process here
            frame = copy(self.source.setup_current_frame)
            frame = setup_frame(frame, self.setup)
            qt_image = convert_frame_qt(frame)
            self.main_window.setup_frame.setPixmap(qt_image)
            self.main_window.setup_frame.setScaledContents(True)
        except TypeError as e:
            self.setup_timer.stop()
            self.main_window.setup_play_btn.setEnabled(False)
            self.main_window.setup_pause_btn.setEnabled(False)
            print("Error:", e)

    def run_setup_pause(self):
        try:
            frame = copy(self.source.setup_current_frame)
            frame = setup_frame(frame, self.setup)
            qt_image = convert_frame_qt(frame)
            self.main_window.setup_frame.setPixmap(qt_image)
            self.main_window.setup_frame.setScaledContents(True)
        except TypeError as e:
            print("Error:", e)

    def run_setup_reset(self):
        try:
            frame = copy(self.source.setup_current_frame)
            qt_image = convert_frame_qt(frame)
            self.main_window.setup_frame.setPixmap(qt_image)
            self.main_window.setup_frame.setScaledContents(True)
        except TypeError as e:
            print("Error:", e)

    def run_analysis(self):
        def populate_analysis():
            self.main_window.density_lbl.setText(str(density))
            self.main_window.vehicle_area_lbl.setText(str(vehicle_area))
            self.main_window.polygon_area_lbl.setText(str(polygon_area))
            self.main_window.contour_no_lbl.setText(str(contour_count))
            self.main_window.speed_lbl.setText(str(speed))
            self.main_window.analysis_frame_no_lbl.setText(\
                str(self.analysis_frame_no))
            self.main_window.high_speed_lbl.setText(str(self.top_speed))
            self.main_window.high_density_lbl.setText(str(self.top_density))
            self.main_window.road_status_lbl.setText(traffic_status)
            self.main_window.congestion_lbl.setText(str(congestion_lvl))
        try:
            self.source.analysis_get_next_frame()
            if self.source.analysis_current_frame != None:
                self.analysis_frame_no = self.analysis_frame_no + 1
                # process here
                frame = copy(self.source.analysis_current_frame)
                # background subtraction
                frame, vehicle_area, contour_count, centers, points = \
                    background_subtraction(frame, \
                    self.source.background.frame, \
                    self.source.polygon.points, self.haar_collect, \
                    self.source.base_name)
                print(self.source.base_name)
                # draw polygon
                frame = draw_polygon_frame(frame, self.source.polygon.points)
                # get polygon area
                polygon_area = get_area_polygon(self.source.polygon.points)
                polygon_area = abs(polygon_area)
                print("Polygon area:", abs(polygon_area), end=' ')
                # compute density
                if vehicle_area > self.top_density:
                    self.top_density = vehicle_area
                density = vehicle_area / polygon_area
                density = abs(density)

                # compute speed
                speed = 0
                if contour_count != 0:
                    self.centers = self.centers + centers
                    frame, speed = draw_line_speed(frame, self.centers)
                    if speed != 0:
                        if speed > self.top_speed:
                            self.top_speed = speed
                        speed = speed / self.top_speed
                else:
                    self.centers = []

                # debug speed
                if self.speed_debug == True:
                    if speed != 0:
                        self.analysis_timer.stop()
                        self.main_window.analysis_play_btn.setEnabled(True)
                # fuzzy logic
                input_val = [density, speed]
                congestion_lvl = 0.0
                traffic_status = 'N/A'
                congestion_lvl, traffic_status, status_code = \
                    FuzzyLogic(input_val).getResult()
                print(congestion_lvl, traffic_status, status_code)
                # populate main window
                populate_analysis()
                # analysis params here
                analysis = {
                    'frame_no': self.analysis_frame_no,
                    'status': traffic_status,
                    'density': density,
                    'speed': speed,
                    'congestion': congestion_lvl,
                    'status_at': None
                }
                print(analysis)
                # check timer for sms notification
                self.source.analysis_dict = analysis
                if self.timer.status:
                    if self.timer.is_due_inc():
                        print("Timer is due")
                        # call send sms here
                        self.notification.send_sms_user("traffic", None)
                        # save frame
                        tid = str(time()) 
                        tid = tid.replace('.', '')
                        frame_file = 'frames/' + tid + "-" + \
                            self.source.base_name + "-" + \
                            traffic_status + '.jpg'
                        cv2.imwrite(frame_file, frame)
                print("Density:", density)
                # convert to qt image
                qt_image = convert_frame_qt(frame)
                self.main_window.analysis_frame.setPixmap(qt_image)
                self.main_window.analysis_frame.setScaledContents(True)
            else:
                self.source.analysis.sequencer()
        except TypeError as e:
            self.analysis_timer.stop()
            self.main_window.analysis_play_btn.setEnabled(False)
            self.main_window.analysis_pause_btn.setEnabled(False)
            print("Error:", e)