Ejemplo n.º 1
0
def proc_video(ind, i_lock, frames, times, bbox_q, cameras, gpu):
    classes = utils.read_class_names("./config/coco.names")
   
    worker = Worker(gpu)
    while(True):
        if worker.avail:
            worker.mark_unavail()
            # save current index so it doesn't change while processing
            #lock ensures that multiple processes aren't working on same frame
            try:
                with i_lock:
                    i = ind.value
                    ind.value = ind.value + 1
                    ind.value = ind.value % len(frames)
            except:
                worker.mark_avail()
                continue
            
            #loop through frames, find people, record occupants and infractions 
            camera = cameras[i]
                
            try:
                worker.set_frame(np.asarray(frames[i]))
            except ValueError:
                worker.mark_avail()
                torch.cuda.empty_cache()
                continue
                
            ped_bboxes,veh_bboxes,blur = worker.get_bboxes()

            # denormalize
            im = F.normalize(worker.gpu_frame[0],mean = [-0.485/0.229, -0.456/0.224, -0.406/0.225],
                                       std = [1/0.229, 1/0.224, 1/0.225])
            im = F.to_pil_image(im.cpu())
            open_cv_image = np.array(im)
            im = open_cv_image.copy()/255.0
            #im = im[:,:,::-1]
            
            # blur all peds regardless of confidence
            for ped in blur:
                im = utils.find_blur_face(ped.int(),im)
            
            
            # parse metrics and write output frame
            filename = camera["output"]
            cam_name = camera["name"]
            pix_real = camera["im-gps"]
            frame_save = camera["save_frames"]
            dt = times[i]
            
            #find ft pts and convert to real_world
            ped_pts = utils.get_ftpts(ped_bboxes)
            realpts = tform.transform_pt_array(ped_pts, pix_real)
            
            # also convert veh points to realworld
            real_veh_pts = tform.transform_pt_array(utils.get_ftpts(veh_bboxes),pix_real)
            
            # verifies there is more than one point in the list (each point has size 2)]
            if realpts.size > 2:
                mytree = scipy.spatial.cKDTree(realpts)
                errors = utils.compliance_count(mytree, realpts)
                
                #FIXME can probably do these both in 1 function
                avg_dist = utils.find_dist(mytree, realpts)
                avg_min_dist = utils.find_min_dist(mytree, realpts)
            else:
                errors = 0
                avg_min_dist = None
                avg_dist = None
            occupants = len(ped_bboxes)
            
            #save frames with occupants
            if frame_save and (occupants > 12 or errors > 5):
                result = prep_frame(ped_pts, im, camera, errors, occupants, ped_bboxes,veh_bboxes,classes)
                dt_fixed = str(dt).replace(":","-").split(".")[0]
                frame_name = "{}/{}.jpg".format(camera["frame_dir"],dt_fixed)
                cv2.imwrite(frame_name,result*255)
            
            #combine so bounding boxes remain associated with camera
            output = [realpts,real_veh_pts,dt,errors,occupants,avg_dist,avg_min_dist,cam_name,i,worker.id]
            bbox_q.put(output)            
            
            
            worker.count += 1
            worker.mark_avail()
                

    return
def post_processor(bbox_q, cameras, out_q, frames, times, image_q=None):
    classes = utils.read_class_names("./config/coco.names")

    try:
        while True:
            if not bbox_q.empty():
                box_ind = bbox_q.get()
                ped_bboxes = box_ind[0]
                veh_bboxes = box_ind[1]
                i = box_ind[2]
                frame = box_ind[3]

                # first, try and show frame
                #frame = frame.transpose(1, 2, 0)
                # cv2.imshow("test",frame)
                # cv2.waitKey(0)

                camera = cameras[i]
                filename = camera["address"]
                pix_real = camera["im-gps"]
                frame_show = camera["save_frames"]
                dt = times[i]

                #find ft pts and convert to real_world
                ped_pts = utils.get_ftpts(ped_bboxes)
                realpts = tform.transform_pt_array(ped_pts, pix_real)

                # verifies there is more than one point in the list (each point has size 2)]
                if realpts.size > 2:
                    mytree = scipy.spatial.cKDTree(realpts)
                    errors = utils.compliance_count(mytree, realpts)

                    #FIXME can probably do these both in 1 function
                    avg_dist = utils.find_dist(mytree, realpts)
                    avg_min_dist = utils.find_min_dist(mytree, realpts)
                else:
                    errors = 0
                    avg_min_dist = None
                    avg_dist = None
                occupants = len(ped_bboxes)
                #output info to csv file
                with open(filename, 'a', newline='') as base_f:
                    writer = csv.writer(base_f)
                    utils.video_write_info(writer, realpts, str(dt), errors,
                                           occupants, avg_dist, avg_min_dist)

                stats = [i, errors, occupants, avg_min_dist]

                #put outpt data into queue so it is accessible by the analyzer
                if out_q.full():
                    out_q.get()
                out_q.put(stats)

                result = prep_frame(ped_pts, frame, camera, errors, occupants,
                                    ped_bboxes, veh_bboxes, classes)

                cv2.imshow("frame", result)
                cv2.waitKey(0)

                # if frame_save or frame_show:
                #     result = prep_frame(ftpts, frame, vid, errors, occupants, bboxes)

                ### UNCOMMENT AND USE
                # frame_save = True
                # #save frames
                # if frame_save:
                #     outpt_frame(result, vid)

                # if frame_show:
                #     if image_q.full():
                #         image_q.get()
                #     image_q.put(result)

                # # FIXME - just for debugging, show frame on screen
                show_frame(result, i)
                if cv2.waitKey(1) & 0xFF == ord('q'): break
    except KeyboardInterrupt:
        print("Unexpected postprocessing error:", sys.exc_info()[0])
        cv2.destroyAllWindows()
    return
def main(errs, ocpts, dists, updated, frames, times, avgs, avg_lock, i_lock,
         ind, out_q, bbox_q, image_q, config, ctx):

    # file containing camera information
    # transform_f = 'C:/Users/Nikki/Documents/work/inputs-outputs/transforms.csv'
    # transform_f = 'C:/Users/Nikki/Documents/work/inputs-outputs/test.csv'
    # transform_f = 'C:/Users/Nikki/Documents/work/inputs-outputs/test_all.csv'
    # transform_f = './config/LAMBDA_TEST.config'

    #create VidObjs to store information about each camera
    cameras = initialize_cams(config)

    num_cams = len(cameras)
    #length of queues, kinda arbitrary - this is the number that will be used for moving avg analytics
    buf_num = 3

    #need to fix these references
    # errs = var_list[0]
    # ocpts = var_list[1]
    # dists = var_list[2]
    # updated = var_list[3]
    # frames = var_list[4]
    # times = var_list[5]
    # avgs = var_list[6]
    # avg_lock = var_list[7]
    # i_lock = var_list[8]
    # ind = var_list[9]
    # bbox_q = var_list[10]
    # ind = var_list[11]

    #uncomment if running from this file

    # manager = mp.Manager()
    # print('MP manager created')
    # #  create manager to handle shared variables across processes
    # updated = manager.Value(c_bool, False)
    # frames = manager.list([None]* num_cams)
    # times = manager.list([None]* num_cams)
    # avgs = manager.list([None] * 5)
    # avg_lock = manager.Lock()
    # i_lock = manager.Lock()
    # out_q = manager.Queue(num_cams*2)
    # bbox_q = manager.Queue()
    # ind = manager.Value(int, 0)
    # image_q = manager.Queue(num_cams*2)

    # # sample = manager.list([None] * 5)

    # errs = manager.list()
    # ocpts = manager.list()
    # dists = manager.list()
    # s_lock = manager.Lock()
    # # for _ in range(num_cams):
    # #     ocpts.append( [None]* buf_num)

    # for i in range(num_cams):
    #     errs.append(manager.list([None]))
    #     ocpts.append(manager.list([None]))
    #     dists.append(manager.list([None]))

    classes = utils.read_class_names("./config/coco.names")

    #stores frame data that has been transfered to GPU
    GPU_LIST = [i for i in range(torch.cuda.device_count())]
    # workers = setup_gpus(vids, gpu_list = GPU_LIST)
    #start model
    # model = detector.start_model()
    streamers = []

    worker = Worker(2)
    frame = cv2.imread("/home/worklab/Desktop/test1.png")
    worker.set_frame(frame)
    ped, veh = worker.get_bboxes()
    cameras[0]["frame_size"] = (worker.gpu_frame[0].shape[:2])

    im = F.normalize(worker.gpu_frame[0],
                     mean=[-0.485 / 0.229, -0.456 / 0.224, -0.406 / 0.225],
                     std=[1 / 0.229, 1 / 0.224, 1 / 0.225])
    im = F.to_pil_image(im.cpu())
    open_cv_image = np.array(im)
    im = open_cv_image.copy() / 255.0
    im = im[:, :, ::-1]
    #combine so bounding boxes remain associated with camera
    i = 0
    box_ind = (ped, veh, i, im)

    ped_bboxes = box_ind[0]
    veh_bboxes = box_ind[1]
    i = box_ind[2]
    frame = box_ind[3]

    # first, try and show frame
    #frame = frame.transpose(1, 2, 0)
    # cv2.imshow("test",frame)
    # cv2.waitKey(0)

    camera = cameras[i]
    filename = camera["address"]
    pix_real = camera["im-gps"]
    frame_show = camera["save_frames"]
    dt = times[i]

    #find ft pts and convert to real_world
    ped_pts = utils.get_ftpts(ped_bboxes)
    realpts = tform.transform_pt_array(ped_pts, pix_real)

    # verifies there is more than one point in the list (each point has size 2)]
    if realpts.size > 2:
        mytree = scipy.spatial.cKDTree(realpts)
        errors = utils.compliance_count(mytree, realpts)

        #FIXME can probably do these both in 1 function
        avg_dist = utils.find_dist(mytree, realpts)
        avg_min_dist = utils.find_min_dist(mytree, realpts)
    else:
        errors = 0
        avg_min_dist = None
        avg_dist = None
    occupants = len(ped_bboxes)
    #output info to csv file
    with open(filename, 'a', newline='') as base_f:
        writer = csv.writer(base_f)
        utils.video_write_info(writer, realpts, str(dt), errors, occupants,
                               avg_dist, avg_min_dist)

    stats = [i, errors, occupants, avg_min_dist]

    #put outpt data into queue so it is accessible by the analyzer
    if out_q.full():
        out_q.get()
    out_q.put(stats)

    result = prep_frame(ped_pts, frame, camera, errors, occupants, ped_bboxes,
                        veh_bboxes, classes)

    cv2.imshow("frame", result)
    cv2.waitKey(0)
def post_processor(bbox_q, cameras, out_q, frames, times, image_q = None):
    classes = utils.read_class_names("./config/coco.names")
    
    start_time = time.time()
    start_time = time.strftime('%Y-%m-%d %H-%M-%S', time.localtime(start_time))
    
    f_directory = cameras[0]["output"].split("/")[:-1]
    f_directory.append("{}_output_frames".format(start_time))
    f_directory = "/".join(f_directory)                                                
    os.mkdir(f_directory)     

    frames_processed = np.zeros(len(cameras))
    
    try:
        while True:
            
            if not bbox_q.empty():
                box_ind = bbox_q.get()
                ped_bboxes = box_ind[0]
                veh_bboxes = box_ind[1]
                i = int(box_ind[2])
                frame = box_ind[3]
                
                # first, try and show frame
                #frame = frame.transpose(1, 2, 0)
                # cv2.imshow("test",frame)
                # cv2.waitKey(0)
                
                camera = cameras[i]
                filename = camera["output"].format(start_time)
                cam_name = camera["name"]
                pix_real = camera["im-gps"]
                frame_save = camera["save_frames"]
                dt = times[i]
                
                #find ft pts and convert to real_world
                ped_pts = utils.get_ftpts(ped_bboxes)
                realpts = tform.transform_pt_array(ped_pts, pix_real)
                
                # verifies there is more than one point in the list (each point has size 2)]
                if realpts.size > 2:
                    mytree = scipy.spatial.cKDTree(realpts)
                    errors = utils.compliance_count(mytree, realpts)
                    
                    #FIXME can probably do these both in 1 function
                    avg_dist = utils.find_dist(mytree, realpts)
                    avg_min_dist = utils.find_min_dist(mytree, realpts)
                else:
                    errors = 0
                    avg_min_dist = None
                    avg_dist = None
                occupants = len(ped_bboxes)
                #output info to csv file  
                with open(filename, 'a', newline='') as base_f:
                    writer = csv.writer(base_f)
                    utils.video_write_info(writer, realpts, str(dt), errors, occupants, avg_dist, avg_min_dist,cam_name)
                        
                stats = [i, errors, occupants, avg_min_dist]
                
                #put outpt data into queue so it is accessible by the analyzer
                if out_q.full():
                    temp = out_q.get()
                out_q.put(stats)
                
                if frame_save:
                    result = prep_frame(ped_pts, frame, camera, errors, occupants, ped_bboxes,veh_bboxes,classes)
                
                
      
                #save frames
                if frame_save:
                    frame_name = "{}/{}_{}_processed.jpg".format(f_directory,cam_name,int(frames_processed[i]))
                    cv2.imwrite(frame_name,result*255)
                        
                # if frame_show:
                #     if image_q.full():
                #         image_q.get()
                #     image_q.put(result)
                    
                # # FIXME - just for debugging, show frame on screen
                # show_frame(result, i)  
                # if cv2.waitKey(1) & 0xFF == ord('q'): break
                  
                frames_processed[i] += 1
                    
    except:
        print("Unexpected postprocessing error:", sys.exc_info()[0])
        cv2.destroyAllWindows()
    return
Ejemplo n.º 5
0
nums = set()
for loc, v in deepcopy(grid).items():
    if v not in '.#':
        nums.add(loc)
    if v == '0':
        start = loc
    grid[loc] = v != '#'

dists = {}
for l1 in sorted(nums):
    for l2 in sorted(nums):
        if l1 == l2:
            continue
        k = tuple(sorted((l1, l2)))
        if k in dists.keys():
            continue
        dists[k] = utils.find_dist(grid, 0, {l1}, l2)

nums.remove(start)
for path in itertools.permutations(nums):
    dist = 0
    go_from = start
    for go_to in path:
        dist += dists[tuple(sorted((go_from, go_to)))]
        go_from = go_to
    p1 = min(p1, dist)
    p2 = min(p2, dist + dists[tuple(sorted((go_from, start)))])

print(f'Part 1: {p1}')
print(f'Part 2: {p2}')
Ejemplo n.º 6
0
            p1 += 1

print(f'Part 1: {p1}')


def display(nodes):
    max_y = max({y for _, y in nodes.keys()})
    for y in range(max_y + 1):
        for (mx, my), data in nodes.items():
            if my != y:
                continue
            print(f'{data[1]!s:>3}/{data[0]!s:>3}', end=' ')
        print()


if test:
    display(nodes)

# 1. move blocks to empty slot left of goal data (28 steps for my input)
# 2. move goal into empty slot
# 3. cycle blocks around goal data and move goal toward 0,0 - 5 steps each

max_x = max({x if y == 0 else 0 for x, y in nodes.keys()}) - 1

# determine legal distance from empty to target node
grid = {(x, y): data[0] < 100 for (x, y), data in nodes.items()}
first_dist = utils.find_dist(grid, 0, {empty_node}, (max_x, 0))

p2 = first_dist + 1 + 5 * max_x
print(f'Part 2: {p2}')