parser.add_argument('--filename',
                        default='filename',
                        help='filename to read data from')
    parser.add_argument('--month', type=int)

    args = parser.parse_args()
    args.cfg = check_file(args.cfg)  # check file
    args.names = check_file(args.names)  # check file
    file_to_read = args.filename
    print("Yolo for vehicle detection configuration:")
    print(args)
    directory_exists = os.path.isdir(args.save_path)
    if not directory_exists:
        os.mkdir(args.save_path)

    i = database_iterator()
    print(f"total network cameras: {i.numcams}")

    vehicle_detector = Vehicle_Detector(weights=args.weights,
                                        cfg=args.cfg,
                                        names=args.names,
                                        iou_thres=args.iou_thres,
                                        conf_thres=args.conf_thres,
                                        imgsz=args.img_size,
                                        half=args.half,
                                        device_id=args.device)

    detections = dict()
    day_night = dict()
    count = 0
    text_file = open(file_to_read, 'r')
def main(person_model, vehicle_detector, subset_all_images, process_num):
    i = database_iterator()

    person_detections = dict()
    day_night = dict()
    vehicle_detections = dict()
    vehicle_filename = os.path.join(
        args.save_path, "vehicle_detections_" + str(process_num) + ".json")
    person_filename = os.path.join(
        args.save_path, "person_detections_" + str(process_num) + ".json")
    day_night_filename = os.path.join(
        args.save_path, "day_night_" + str(process_num) + ".json")

    for foldername, image_link, time in i.get_subset_images(
            cam_list=subset_all_images):
        print(foldername)
        person_detections[foldername] = dict()
        day_night[foldername] = dict()

        check = all_same(i, image_link)

        if len(image_link) > 0 and not check:
            for j in range(len(image_link)):
                print(j)
                pil_image = (i.get_image(image_link[j]))
                img = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)

                # day night calculation
                if determine_day_night(img) == 0:
                    day_night[foldername][image_link[j]] = 'night'
                else:
                    day_night[foldername][image_link[j]] = 'day'

                # person detection
                results = inference_detector(person_model, img)
                if isinstance(results, tuple):
                    bbox_result, segm_result = results
                else:
                    bbox_result, segm_result = results, None
                bboxes = np.vstack(bbox_result)
                bboxes = bboxes.tolist()
                bbox_dict = dict()
                for each in bboxes:
                    bbox_dict[each[4]] = each[0:4]

                person_detections[foldername][image_link[j]] = bbox_dict
                """
                # vehicle detection
                img = np.array(pil_image.convert('RGB'))
                results = vehicle_detector.detect(img, view_img=False)
                vehicle_detections[foldername][image_link[j]] = results
   #             if j % 20 == 19:
    #                print(f"{j + 1} done out of {len(image_link)} images")
            """

            # write to the file at the end of every camera instead of when the entire process is complete
            # Helps if it gets disconnected in between
            """
            f = open(vehicle_filename, "w+")
            f.write(json.dumps(vehicle_detections))
            f.close()
            """

            f = open(person_filename, "w+")
            f.write(json.dumps(person_detections))
            f.close()

            f = open(day_night_filename, "w+")
            f.write(json.dumps(day_night))
            f.close()