Beispiel #1
0
def main():
    import sys
    
    if path.exists('data/coordinates_1.yml'):
        os.remove('data/coordinates_1.yml')
        
    points = None
    waterLevelSlots = None
    
    try:
        fn = sys.argv[1]
    except:
        fn = 0
    cap = video.create_capture(fn)

    def fetchShortIntervalVideos(ctrl, motion_detector, coordinates_data, times, statuses, lock):
        with lock:
             videoFilePath, hasIncomingVideoCaptureChanged = ctrl.getVideoFilePath()
             return videoFilePath, hasIncomingVideoCaptureChanged, motion_detector, coordinates_data, times, statuses
    
    threadn = cv.getNumberOfCPUs()
    pending = deque()
    lock = Lock()
    pool = ThreadPool(processes = threadn, initializer = init_child, initargs=(lock,))
    
    threaded_mode = True
    ctrl = None
    motionDetector = None
    
    lastsave = 0
    coordinates_data = None
    times = None 
    statuses = None
    
    pointsCaptured = False
    while True:
        with lock:
            while len(pending) > 1 and pending[0].ready() and pending[1].ready():
                payload = pending.popleft().get()
                if len(payload) == 6:
                    videoFilePath, hasIncomingVideoCaptureChanged, motion_detector, coordinates_data, times, statuses = payload
                    if videoFilePath == None and hasIncomingVideoCaptureChanged == None:
                        break
                    else:
                    
                        if hasIncomingVideoCaptureChanged == True:
                            capture = cv.VideoCapture(videoFilePath)
                            while capture.isOpened():
                                result, frame = capture.read()
                                if not result:
                                    capture.release()
                                    continue
                                else:
                                    res, evaluated_waterLevelSlots  = motion_detector.process_algo_per_frame(frame, capture, coordinates_data, times, statuses)
                                    
                                    draw_str(res, (5, 20), WaterLevelSlots.LEVEL_REACHED_MSG
                                             + str(evaluated_waterLevelSlots.get_current_water_level()))
                        
                                    if time.time() - lastsave > 1:
                                        lastsave = time.time()
                                        persistWaterLevelData(evaluated_waterLevelSlots.get_current_water_level())
                        
                        cv.namedWindow('flood-detection', cv.WINDOW_NORMAL)
                        cv.setWindowProperty('flood-detection', 0, 1)
                        
                        cv.imshow('flood-detection', res)
                         
        if len(pending) < threadn:
            
            if not pointsCaptured:
                _ret, frame = cap.read()
                points = getPoints(frame, points)
                waterLevelSlots = WaterLevelSlots(len(points))
                ctrl = Controller(points, None, None)
                motionDetector = MotionDetector(points, 1, waterLevelSlots)
                coordinates_data, times, statuses = motionDetector.detect_motion_activity()
                pointsCaptured = True
           
            if threaded_mode:
                task_put_videos = pool.apply_async(captureShortIntervalVideos, (cap, lock))
                task_get_videos = pool.apply_async(fetchShortIntervalVideos,
                                                   (ctrl, motionDetector, coordinates_data, times, statuses, lock))
                
            
            pending.append(task_put_videos)
            pending.append(task_get_videos)
        
        ch = cv.waitKey(1)
        if ch == ord(' '):
            threaded_mode = not threaded_mode
        if ch == 27:
            break

    print('Done')
    cap.release()
Beispiel #2
0
def main():
    import sys

    if path.exists('data/coordinates_1.yml'):
        os.remove('data/coordinates_1.yml')

    points = None
    carPark = None

    try:
        fn = sys.argv[1]
    except:
        fn = 1
    cap = video.create_capture(fn)

    def fetchShortIntervalVideos(ctrl, motion_detector, coordinates_data,
                                 times, statuses, lock):
        with lock:
            videoFilePath, hasIncomingVideoCaptureChanged = ctrl.getVideoFilePath(
            )
            return videoFilePath, hasIncomingVideoCaptureChanged, motion_detector, coordinates_data, times, statuses

    threadn = cv.getNumberOfCPUs()
    pending = deque()
    lock = Lock()
    pool = ThreadPool(processes=threadn,
                      initializer=init_child,
                      initargs=(lock, ))

    threaded_mode = True
    ctrl = None
    motionDetector = None

    lastsave = 0
    coordinates_data = None
    times = None
    statuses = None

    pointsCaptured = False
    while True:
        with lock:
            while len(pending) > 1 and pending[0].ready() and pending[1].ready(
            ):
                payload = pending.popleft().get()
                if len(payload) == 6:
                    videoFilePath, hasIncomingVideoCaptureChanged, motion_detector, coordinates_data, times, statuses = payload
                    if videoFilePath == None and hasIncomingVideoCaptureChanged == None:
                        break
                    else:

                        if hasIncomingVideoCaptureChanged == True:
                            capture = cv.VideoCapture(videoFilePath)
                            while capture.isOpened():
                                result, frame = capture.read()
                                if not result:
                                    capture.release()
                                    continue
                                else:
                                    res, evaluated_carPark = motion_detector.process_algo_per_frame(
                                        frame, capture, coordinates_data,
                                        times, statuses)
                                    draw_str(
                                        res, (5, 20),
                                        CarParkData.TOTAL_NUMBER_OF_SLOTS +
                                        str(evaluated_carPark.
                                            get_total_car_park_slots()))
                                    draw_str(
                                        res, (5, 40), CarParkData.
                                        NUMBER_OF_CARPARK_SLOTS_AVAILABLE +
                                        str(evaluated_carPark.
                                            get_available_carpark_slots()))

                                    if carPark.is_carpark_full():
                                        draw_str_red(
                                            res, (5, 60),
                                            CarParkData.CARPARK_FULL_MESSAGE)
                                    else:
                                        draw_str_green(
                                            res, (5, 60), CarParkData.
                                            CARPARK_AVAILABLE_MESSAGE)

                                    #latest_modified_date_time = datetime.now()
                                    #draw_str(res, (440,20), latest_modified_date_time.strftime('%d-%m-%Y %H:%M:%S '))

                                    json = '['
                                    for carpark_slot in evaluated_carPark.get_carpark_slots(
                                    ):
                                        json = json + carpark_slot.toJSON(
                                        ) + ','

                                    json = json[:-1]
                                    json = json + ']'
                                    json = json.replace('\n', '')
                                    json = json.replace('\t', '')
                                    json = json.replace('\r', '')
                                    json = json.replace("\'", '')
                                    json = json.replace('    ', '')
                                    #retval, buffer = cv.imencode('.jpg', res)
                                    #jpg_as_text = base64.b64encode(buffer)

                                    if time.time() - lastsave > 1:
                                        lastsave = time.time()
                                        persistSmartParkRealTime(
                                            None, None,
                                            evaluated_carPark.
                                            get_total_car_park_slots(),
                                            evaluated_carPark.
                                            get_available_carpark_slots(),
                                            json, None, None)

                                    cv.namedWindow('smart-parking',
                                                   cv.WINDOW_NORMAL)
                                    cv.setWindowProperty('smart-parking', 0, 1)
                                    cv.imshow('smart-parking', res)

        if len(pending) < threadn:

            if not pointsCaptured:
                _ret, frame = cap.read()
                points = getPoints(frame, points)
                carPark = CarParkData('SmartCarPark', len(points))
                ctrl = Controller(points, None, None)
                motionDetector = MotionDetector(points, 1, carPark)
                coordinates_data, times, statuses = motionDetector.detect_motion_activity(
                )
                pointsCaptured = True

            if threaded_mode:
                task_put_videos = pool.apply_async(captureShortIntervalVideos,
                                                   (cap, lock))
                task_get_videos = pool.apply_async(
                    fetchShortIntervalVideos,
                    (ctrl, motionDetector, coordinates_data, times, statuses,
                     lock))

            pending.append(task_put_videos)
            pending.append(task_get_videos)

        ch = cv.waitKey(1)
        if ch == ord(' '):
            threaded_mode = not threaded_mode
        if ch == 27:
            break

    print('Done')
    cap.release()