Example #1
0
class Analysis:
    def __init__(self, source, setup):
        self.dbs = source.dbs
        self.main_window = source.main_window
        self.source = source
        self.setup = setup
        if self.source.media == 'file':
            self.files = source.files
        self.main_window.tabs.setCurrentIndex(1)
        self.pointer = 0
        self.location = Location(self.main_window, self.source)

        # signals
        # play button
        self.main_window.connect(self.main_window.analysis_play_btn, \
            SIGNAL("clicked()"), self.click_analysis_play_btn)
        # pause button
        self.main_window.connect(self.main_window.analysis_pause_btn, \
            SIGNAL("clicked()"), self.click_analysis_pause_btn)
        # stop button
        self.main_window.connect(self.main_window.analysis_stop_btn, \
            SIGNAL("clicked()"), self.click_analysis_stop_btn)
        # next button
        self.main_window.connect(self.main_window.analysis_next_btn, \
            SIGNAL("clicked()"), self.click_analysis_next_btn)
        # debug speed button
        self.main_window.connect(self.main_window.speed_btn, \
            SIGNAL("clicked()"), self.click_speed_btn)
        # debug road status button
        self.main_window.connect(self.main_window.road_status_btn, \
            SIGNAL("clicked()"), self.click_road_status_btn)
        # haar collect button
        self.main_window.connect(self.main_window.haar_capture_btn, \
            SIGNAL("clicked()"), self.click_haar_capture_btn)
        # haar classifier button
        self.main_window.connect(self.main_window.classifier_btn, \
            SIGNAL("clicked()"), self.click_classifier_btn)

        # choose media source
        if self.source.media == 'file':
            self.sequencer()
        if self.source.media == 'camera':
            self.open()

    def release_media(self):
        print("Releasing media")
        if self.source.setup_frames != None:
            self.source.setup_frames.release()
        self.source.analysis_frames.release()

    def sequencer(self):
        init_setup(self.main_window)
        if self.pointer != len(self.files):
            self.file = self.files[self.pointer]
            self.open()
            self.pointer += 1
        else:
            print("Nothing to analyse!")
            self.dialog = DialogWindow("Analysis", "Analysis is finished!")
            init_main(self.main_window)
            # self.main_window.tabs.hide()
            self.play.analysis_timer.stop()
            self.release_media()

    def open(self):
        init_analysis(self.main_window)
        self.main_window.analysis_next_btn.setEnabled(True)
        if self.source.media == 'file':
            self.source.base_name = get_base_file(self.file) 
            self.source.analysis_frames = cv2.VideoCapture(self.file)
        if self.source.media == 'camera':
            init_analysis_camera(self.main_window)
            print("Setting analysis frames for camera")
            # release camera resource for analysis
            self.source.setup_frames.release()
            self.source.analysis_frames = cv2.VideoCapture(config['camera'])
        # images folder
        directory = 'images/' + self.source.base_name
        create_dir(directory) 
        self.get_video()
        if self.source.media == 'file':
            self.setup.load_video(self.video.id)
        self.background = Background(self.source) 
        self.background.get_db(self.video.id)
        self.source.background = self.background
        self.polygon = Polygon(self.source, self.setup) 
        self.polygon.get_points(self.video.id)
        self.polygon.draw_setup(self.background.frame, self.polygon.points)
        self.source.polygon = self.polygon
        self.play = Play(self.source, self.setup)
        self.play.run_analysis()
        # set location
        self.location.set_location(self.video.address_id)
        # play
        self.click_analysis_play_btn()

    def click_analysis_play_btn(self):
        print("Clicked play button in analysis!")
        self.play.analysis_status = True
        self.play.analysis_timer.start()
        self.main_window.analysis_pause_btn.setEnabled(True)
        self.main_window.analysis_play_btn.setEnabled(False)
        self.play.timer.status = True
        self.play.timer.start()

    def click_analysis_pause_btn(self):
        print("Clicked pause button in analysis!")
        self.play.analysis_status = False 
        self.play.analysis_timer.stop()
        self.main_window.analysis_pause_btn.setEnabled(False)
        self.main_window.analysis_play_btn.setEnabled(True)

    def click_analysis_stop_btn(self):
        print("Clicked stop button in analysis!")
        self.play.analysis_status = False 
        self.play.analysis_timer.stop()
        stop_analysis(self.main_window)
        init_setup(self.main_window)
        # release media source
        self.release_media()
        self.main_window.tabs.setCurrentIndex(0)

    def get_video(self):
        if self.source.media == 'file':
            self.base_name = get_base_name(self.file)
        if self.source.media == 'camera':
            self.base_name = self.source.base_name
        s = self.dbs
        result = s.query(Video).filter(Video.name == self.base_name)
        if result.count() == 0:
            print("No video record found in database")
            self.video = None
        elif result.count() == 1:
            print("Found 1 record in database")
            self.video = result.first()
            self.source.video = self.video
            print(self.video)
            # get location of source
            self.source.location = s.query(Address).filter(Address.id \
                == self.video.address_id).first()
            print("Location of source is ", self.source.location)
        else:
            self.video = None

    def click_analysis_next_btn(self):
        print("Clicked next button in analysis!")
        self.sequencer()

    def click_speed_btn(self):
        if self.play.speed_debug == True:
            self.play.speed_debug = False
        else:
            self.play.speed_debug = True
        self.main_window.speed_debug_lbl.setText(\
            str(self.play.speed_debug))
        print("Speed debug status:", self.play.speed_debug)

    def click_road_status_btn(self):
        if self.play.road_status_debug == True:
            self.play.road_status_debug = False
        else:
            self.play.road_status_debug = True
        self.main_window.road_status_debug_lbl.setText(\
            str(self.play.road_status_debug))
        print("Speed debug status:", self.play.road_status_debug)

    def click_haar_capture_btn(self):
        print("Clicked haar capture button")
        if self.play.haar_collect == True:
            self.play.haar_collect = False
        else:
            self.play.haar_collect = True
        self.main_window.haar_capture_lbl.setText(\
            str(self.play.haar_collect))
        print("Haar collect status:", self.play.haar_collect)

    def click_classifier_btn(self):
        print("Clicked haar classifier button")
        if self.play.haar_activate == True:
            self.play.haar_activate = False
        else:
            self.play.haar_activate = True
        self.main_window.classifier_lbl.setText(\
            str(self.play.haar_activate))
        print("Haar activate status:", self.play.haar_activate)