def main():
    """Main function for the program.  Everything starts here.

    :return: None
    """
    global resize_output, resize_output_width, resize_output_height, \
           obj_detector_proc, resize_output, resize_output_width, resize_output_height, video_proc, \
           last_num_persons, asyncImWriter

    if (not handle_args()):
        print_usage()
        return 1

    # Set logging level to only log errors
    mvnc.global_set_option(mvnc.GlobalOption.RW_LOG_LEVEL, 3)

    devices = mvnc.enumerate_devices()
    if len(devices) < 1:
        print('No NCS device detected.')
        print('Insert device and try again!')
        return 1

    # Pick the first stick to run the network
    # use the first NCS device that opens for the object detection.
    dev_count = 0
    for one_device in devices:
        try:
            obj_detect_dev = mvnc.Device(one_device)
            obj_detect_dev.open()
            print("opened device " + str(dev_count))
            break
        except:
            print("Could not open device " + str(dev_count) +
                  ", trying next device")
            pass
        dev_count += 1

    cv2.namedWindow(cv_window_name)
    cv2.moveWindow(cv_window_name, 10, 10)
    cv2.waitKey(1)

    obj_detector_proc = SsdMobileNetProcessor(
        NETWORK_GRAPH_FILENAME,
        obj_detect_dev,
        inital_box_prob_thresh=min_score_percent / 100.0,
        classification_mask=object_classifications_mask)

    exit_app = False
    while (True):
        # video processor that will put video frames images on the object detector's input FIFO queue
        video_proc = CameraProcessor(CAMERA_INDEX,
                                     1920,
                                     1080,
                                     network_processor=obj_detector_proc)
        video_proc.start_processing()

        frame_count = 0
        start_time = time.time()
        end_time = start_time

        while (True):
            try:
                (filtered_objs, display_image
                 ) = obj_detector_proc.get_async_inference_result()
            except:
                print("exception caught in main")
                raise

            # check if the window is visible, this means the user hasn't closed
            # the window via the X button
            prop_val = cv2.getWindowProperty(cv_window_name,
                                             cv2.WND_PROP_ASPECT_RATIO)
            if (prop_val < 0.0):
                end_time = time.time()
                video_proc.stop_processing()
                exit_app = True
                break

            agg_results = overlay_on_image(display_image, filtered_objs)
            num_persons = len(agg_results)

            if (show_output):
                if (resize_output):
                    display_image = cv2.resize(
                        display_image,
                        (resize_output_width, resize_output_height),
                        cv2.INTER_LINEAR)
                cv2.imshow(cv_window_name, display_image)

                raw_key = cv2.waitKey(1)
                if (raw_key != -1):
                    if (handle_keys(raw_key, obj_detector_proc) == False):
                        end_time = time.time()
                        exit_app = True
                        video_proc.stop_processing()
                        break

            frame_count += 1

            # if (obj_detector_proc.is_input_queue_empty()):
            #     end_time = time.time()
            #     print('Neural Network Processor has nothing to process, assuming video is finished.')
            #     break

        frames_per_second = frame_count / (end_time - start_time)
        print('Frames per Second: ' + str(frames_per_second))

        throttling = obj_detect_dev.get_option(
            mvnc.DeviceOption.RO_THERMAL_THROTTLING_LEVEL)
        if (throttling > 0):
            print("\nDevice is throttling, level is: " + str(throttling))
            print("Sleeping for a few seconds....")
            cv2.waitKey(2000)

        #video_proc.stop_processing()
        cv2.waitKey(1)
        if (exit_app):
            video_proc.cleanup()
            break

    # Clean up the graph and the device
    obj_detector_proc.cleanup()
    obj_detect_dev.close()
    obj_detect_dev.destroy()

    cv2.destroyAllWindows()
Example #2
0
def main():
    """Main function for the program.  Everything starts here.

    :return: None
    """
    global resize_output, resize_output_width, resize_output_height, \
           obj_detector_proc, resize_output, resize_output_width, resize_output_height, video_proc

    if (not handle_args()):
        print_usage()
        return 1

    # get list of all the .mp4 files in the image directory
#    input_video_filename_list = os.listdir(input_video_path)
#    input_video_filename_list = [i for i in input_video_filename_list if i.endswith('.mp4')]
#    if (len(input_video_filename_list) < 1):
#        # no images to show
#        print('No video (.mp4) files found')
#        return 1

# Set logging level to only log errors
    mvnc.global_set_option(mvnc.GlobalOption.RW_LOG_LEVEL, 3)

    devices = mvnc.enumerate_devices()
    if len(devices) < 1:
        print('No NCS device detected.')
        print('Insert device and try again!')
        return 1

    # Pick the first stick to run the network
    # use the first NCS device that opens for the object detection.
    # open as many devices as we find
    dev_count = 0
    ncs_devices = []
    obj_detectors = []
    # open as many devices as detected
    for one_device in devices:
        print('one device ', one_device, 'dev_count ', dev_count, 'devices ',
              devices)
        obj_detect_dev = mvnc.Device(one_device)
        status = obj_detect_dev.open()
        ncs_devices.append(obj_detect_dev)
        obj_detector_proc = SsdMobileNetProcessor(
            NETWORK_GRAPH_FILENAME,
            ncs_devices,  # obj_detect_dev,
            inital_box_prob_thresh=min_score_percent / 100.0,
            classification_mask=object_classifications_mask)
        obj_detectors.append(obj_detector_proc)
        print("opened device " + str(dev_count), 'status ', status)
        dev_count += 1

    print('ncs_devices', ncs_devices)

    cv2.namedWindow(cv_window_name)
    cv2.moveWindow(cv_window_name, 10, 10)
    cv2.waitKey(1)

    #obj_detector_proc = SsdMobileNetProcessor(NETWORK_GRAPH_FILENAME, ncs_devices[0], # obj_detect_dev,
    #                                         inital_box_prob_thresh=min_score_percent/100.0,
    #                                         classification_mask=object_classifications_mask)

    exit_app = False

    # output file
    # fourcc = cv2.VideoWriter_fourcc(*"MJPG")
    fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    filenum = 1
    # keep the number of video files to a reasonable number in the current directory
    while (filenum < 20):
        doesexist = os.path.isfile("output" + str(filenum) + ".mp4")
        if (doesexist == False):
            out_filename = "output" + str(filenum) + ".mp4"
            break
        filenum += 1

    print("Using output file name " + out_filename)

    outfile = cv2.VideoWriter(out_filename, fourcc, 11.0, (640, 480))

    while (True):
        #        for input_video_file in input_video_filename_list :

        # video processor that will put video frames images on the object detector's input FIFO queue
        #            video_proc = VideoProcessor(input_video_path + '/' + input_video_file,
        #                                         network_processor = obj_detector_proc)
        # use the video cam (0)  ***swb***
        video_proc = VideoProcessor(
            0, network_processor=obj_detectors[0])  # obj_detector_proc)
        video_proc.start_processing()

        frame_count = 0
        start_time = time.time()
        end_time = start_time

        while (True):
            try:
                (filtered_objs, display_image
                 ) = obj_detector_proc.get_async_inference_result()
            except:
                print("exception caught in main")
                raise

            # check if the window is visible, this means the user hasn't closed
            # the window via the X button
            prop_val = cv2.getWindowProperty(cv_window_name,
                                             cv2.WND_PROP_ASPECT_RATIO)
            if (prop_val < 0.0):
                end_time = time.time()
                video_proc.stop_processing()
                exit_app = True
                break

            overlay_on_image(display_image, filtered_objs)

            if (resize_output):
                display_image = cv2.resize(
                    display_image, (resize_output_width, resize_output_height),
                    cv2.INTER_LINEAR)
            cv2.imshow(cv_window_name, display_image)

            outfile.write(display_image)

            raw_key = cv2.waitKey(1)
            if (raw_key != -1):
                if (handle_keys(raw_key, obj_detector_proc) == False):
                    end_time = time.time()
                    exit_app = True
                    video_proc.stop_processing()
                    continue

            frame_count += 1

            if (obj_detector_proc.is_input_queue_empty()):
                end_time = time.time()
                print(
                    'Neural Network Processor has nothing to process, assuming video is finished.'
                )
                break

        frames_per_second = frame_count / (end_time - start_time)
        print('Frames per Second: ' + str(frames_per_second))

        throttling = obj_detect_dev.get_option(
            mvnc.DeviceOption.RO_THERMAL_THROTTLING_LEVEL)
        if (throttling > 0):
            print("\nDevice is throttling, level is: " + str(throttling))
            print("Sleeping for a few seconds....")
            cv2.waitKey(2000)

        #video_proc.stop_processing()
        cv2.waitKey(1)

        video_proc.cleanup()

        if (exit_app):
            break
    #if (exit_app):
    #    break

    # Clean up the graph and the device
    obj_detector_proc.cleanup()
    obj_detect_dev.close()
    obj_detect_dev.destroy()

    cv2.destroyAllWindows()
    outfile.release()