Here we demonstrate the inference capability of the bounding box detector when operating on images not in the training set, applying it to the additional 12,000 unlabeled video frames, to see what "physics" can be learned.

Installing IceVision and IceData

If on Colab run the following cell, else check the installation instructions

#try:
#    !wget https://raw.githubusercontent.com/airctic/icevision/master/install_colab.sh
#    !chmod +x install_colab.sh && ./install_colab.sh
#except:
#    print("Ignore the error messages and just keep going")
 
!pip install espiownage -Uqq
from espiownage.core import *
sysinfo()
TORCH_VERSION=torch1.8.0; CUDA_VERSION=cu101
CUDA available = True, Device count = 1, Current device = 0
Device name = TITAN X (Pascal)
hostname: lecun
# FOR COLAB: Install IceVision, then grab latest and force a hard restart.
#  Locally, you can & should still run (most of) the install_colab.sh script
! [ -e /content ] &&  wget https://raw.githubusercontent.com/airctic/icevision/master/install_colab.sh
! [ -e /content ] &&  chmod +x install_colab.sh && ./install_colab.sh 
! [ -e /content ] &&  pip install git+git://github.com/airctic/icevision.git --upgrade && kill -9 -1
from icevision.all import *
import pandas as pd
INFO     - The mmdet config folder already exists. No need to downloaded it. Path : /home/drscotthawley/.icevision/mmdetection_configs/mmdetection_configs-2.10.0/configs | icevision.models.mmdet.download_configs:download_mmdet_configs:17

The real test: if this next import doesn't pass then your IV install is old/messed up:

from icevision.models.checkpoint import *

Main definitions: Where are the images, and where's the model?

Images

imgdir = '/home/drscotthawley/datasets/zooniverse_steelpan'
img_files = get_image_files(imgdir)
len(img_files)
14000
img = PIL.Image.open(img_files[13500])
img

Load Model from Checkpoint

k = 0
checkpoint_path = f'espi-retinanet-checkpoint-real-k{k}.pth'

checkpoint_and_model = model_from_checkpoint(checkpoint_path, map_location='cuda',
                        model_name='mmdet.retinanet', 
                        backbone_name='resnet50_fpn_1x',
                        img_size=384)

model = checkpoint_and_model["model"]
# Check device
model.to('cuda')
device=next(model.parameters()).device
device
Use load_from_local loader
/home/drscotthawley/envs/iv2/lib/python3.8/site-packages/mmcv/cnn/utils/weight_init.py:118: UserWarning: init_cfg without layer key, if you do not define override key either, this init_cfg will do nothing
  warnings.warn(
device(type='cuda', index=0)
model_type = checkpoint_and_model["model_type"]
backbone = checkpoint_and_model["backbone"]
class_map = checkpoint_and_model["class_map"]
img_size = checkpoint_and_model["img_size"]
model_type, backbone, class_map, img_size
(<module 'icevision.models.mmdet.models.retinanet' from '/home/drscotthawley/envs/iv2/lib/python3.8/site-packages/icevision/models/mmdet/models/retinanet/__init__.py'>,
 <icevision.models.mmdet.models.retinanet.backbones.resnet_fpn.MMDetRetinanetBackboneConfig at 0x7f2b94ad21c0>,
 <ClassMap: {'background': 0, 'AN': 1}>,
 384)
img_size = checkpoint_and_model["img_size"]
valid_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(img_size), tfms.A.Normalize()])
img = PIL.Image.open(img_files[13500])
img = img.resize((384,384)).convert('RGB')

pred_dict  = model_type.end2end_detect(img, valid_tfms, model, class_map=class_map, detection_threshold=0.5)
pred_dict['img']

Batch Inference

imgs_array = [PIL.Image.open(Path(fname)).convert('RGB') for fname in img_files]
infer_ds = Dataset.from_images(imgs_array, valid_tfms, class_map=class_map)
infer_dl = model_type.infer_dl(infer_ds, batch_size=4, shuffle=False)
preds = model_type.predict_from_dl(model, infer_dl, keep_images=True)
len(infer_ds), len(preds)
(14000, 14000)
preds[0].detection.
[<icevision.core.record_components.BBoxesRecordComponent at 0x7f2978f5a0d0>,
 <icevision.core.record_components.ScoresRecordComponent at 0x7f2978f5a3d0>,
 <icevision.core.record_components.InstancesLabelsRecordComponent at 0x7f2978f5a850>]

Generate BBoxes and Image Crops

def convert_bb(bb, square=384, width=512, height=384):
    "IceVision borked our non-square images so all the bboxes are off; Convert back"
    bb = [int(x*width/square) for x in bb]  # unshrink everything to make it like a 512x512 image
    bb[1], bb[3] = bb[1]-(width-height)//2, bb[3]-(width-height)//2   # now subtract off the coords of the "bar" at the top
    return [bb[0],bb[1],bb[2],bb[3]]

def get_bblist(pred):
    "given IV pred, return list of bboxes"
    my_bblist = []
    bblist = pred.pred.detection.bboxes
    for i in range(len(bblist)):
        bb = [bblist[i].xmin, bblist[i].ymin, bblist[i].xmax, bblist[i].ymax]
        bb = convert_bb(bb)
        my_bblist.append([bb])
    return my_bblist

def gen_crops(meta_file, img, bblist, crops_dir='crops_iv', verbose=False):
    if verbose: print("gen_crops: bblist = ", bblist)
    for bb_ in bblist:
        if verbose: print("    gen_crops: bb_ = ", bb_)
        if bb_ is not None:
            bb = bb_[0]   # comes wrapped in an extra list
            if verbose: print("     gen_crops: bb = ", bb)
            #print("meta_file, bb=",meta_file, bb)
            img_cropped = crop_to_bbox(img, bb)
            if img_cropped is not None:
                out_file = crops_dir+'/'+str(Path(meta_file).stem)+f"_{bb[0]}_{bb[1]}_{bb[2]}_{bb[3]}.png"
                img_cropped.save(out_file)
from PIL import ImageDraw

def draw_bbs_on_img(img, bblist, color="cyan", verbose=False):
    "Draws a rectangle on image. IN PLACE"
    if [] == bblist: return 
    draw = ImageDraw.Draw(img)
    for bb_ in bblist:
        if verbose: print(" draw_bb_on_img: bb_ = ",bb_)
        if bb_ is not None:
            bb = bb_[0]
            if verbose: print(" draw_bb_on_img: bb = ",bb)
            draw.rectangle(((bb[0], bb[1]), (bb[2],bb[3])), outline=color)
        
im = img.copy()
draw_bb_on_img(im, (50,50,150,100))
im

Loop through all the images generating bbox lists and crops

results = []  # list to become dataframe
crops_dir = 'crops_iv'
mkdir_if_needed(crops_dir)

for i in range(0,len(preds)):
    meta_filename = str(Path(img_files[i]).stem)+'.csv'
    bblist = get_bblist(preds[i])
    if [] != bblist: 
        gen_crops(meta_filename, imgs_array[i], bblist, crops_dir, verbose=verbose)
        #draw_bbs_on_img(imgs_array[i], bblist, verbose=verbose)  # draws in place # do this after crops
    line_list = [meta_filename, bblist, i]
    results.append(line_list)
print(f"\n{len(results)} images processed.")
14000 images processed.
pd.set_option('display.max_rows', None)
res_df = pd.DataFrame(results, columns=['filename', 'bblist','i'])
res_df.iloc[13400:13600] # take a look.  Note that Pandas saves lists as strings!
filename bblist i
13400 06241902_proc_01400.csv [[[143, 99, 264, 208]], [[0, 30, 85, 221]]] 13400
13401 06241902_proc_01401.csv [[[139, 81, 275, 214]], [[0, 54, 84, 228]]] 13401
13402 06241902_proc_01402.csv [[[138, 83, 277, 218]], [[0, 41, 85, 241]], [[198, 246, 286, 333]]] 13402
13403 06241902_proc_01403.csv [[[136, 77, 282, 218]], [[183, 238, 283, 342]], [[1, 40, 83, 245]]] 13403
13404 06241902_proc_01404.csv [[[128, 82, 271, 216]], [[173, 238, 278, 341]], [[0, 47, 85, 226]]] 13404
13405 06241902_proc_01405.csv [[[126, 74, 280, 219]], [[171, 245, 274, 342]], [[1, 36, 87, 251]]] 13405
13406 06241902_proc_01406.csv [[[128, 70, 287, 216]], [[164, 247, 267, 349]], [[0, 47, 81, 232]]] 13406
13407 06241902_proc_01407.csv [[[130, 72, 283, 217]], [[167, 249, 264, 347]], [[0, 51, 82, 233]]] 13407
13408 06241902_proc_01408.csv [[[125, 73, 282, 220]], [[1, 42, 76, 228]], [[169, 256, 259, 347]]] 13408
13409 06241902_proc_01409.csv [[[130, 75, 282, 218]], [[0, 39, 80, 238]]] 13409
13410 06241902_proc_01410.csv [[[126, 79, 276, 222]], [[0, 37, 82, 236]]] 13410
13411 06241902_proc_01411.csv [[[131, 94, 268, 213]], [[0, 43, 85, 234]]] 13411
13412 06241902_proc_01412.csv [[[139, 103, 258, 210]], [[0, 52, 77, 214]], [[142, 250, 233, 339]]] 13412
13413 06241902_proc_01413.csv [[[150, 238, 241, 341]], [[0, 43, 86, 224]], [[162, 98, 256, 199]]] 13413
13414 06241902_proc_01414.csv [[[155, 76, 291, 208]], [[151, 243, 244, 338]], [[0, 36, 86, 225]]] 13414
13415 06241902_proc_01415.csv [[[147, 75, 287, 214]], [[153, 239, 252, 339]], [[0, 38, 87, 230]]] 13415
13416 06241902_proc_01416.csv [[[141, 71, 289, 220]], [[0, 65, 88, 233]], [[153, 240, 254, 339]]] 13416
13417 06241902_proc_01417.csv [[[146, 71, 293, 220]], [[0, 44, 93, 217]], [[161, 244, 254, 337]]] 13417
13418 06241902_proc_01418.csv [[[146, 71, 285, 218]], [[160, 243, 253, 338]], [[2, 36, 86, 203]]] 13418
13419 06241902_proc_01419.csv [[[139, 68, 290, 223]], [[1, 36, 88, 210]], [[156, 248, 249, 346]]] 13419
13420 06241902_proc_01420.csv [[[138, 67, 287, 224]], [[1, 35, 85, 226]]] 13420
13421 06241902_proc_01421.csv [[[136, 61, 292, 227]], [[0, 39, 74, 205]]] 13421
13422 06241902_proc_01422.csv [[[138, 66, 289, 218]], [[174, 242, 277, 341]], [[1, 52, 90, 225]]] 13422
13423 06241902_proc_01423.csv [[[140, 69, 290, 220]], [[173, 239, 277, 345]], [[0, 55, 93, 234]]] 13423
13424 06241902_proc_01424.csv [[[144, 73, 287, 217]], [[165, 240, 276, 343]], [[0, 27, 84, 237]]] 13424
13425 06241902_proc_01425.csv [[[154, 81, 287, 214]], [[163, 243, 267, 343]], [[0, 21, 84, 236]]] 13425
13426 06241902_proc_01426.csv [[[159, 244, 265, 350]], [[165, 98, 276, 201]], [[0, 36, 87, 219]]] 13426
13427 06241902_proc_01427.csv [[[165, 243, 264, 346]], [[167, 100, 249, 192]], [[0, 27, 79, 222]]] 13427
13428 06241902_proc_01428.csv [[[143, 86, 266, 210]], [[0, 27, 84, 233]], [[167, 256, 251, 344]]] 13428
13429 06241902_proc_01429.csv [[[129, 82, 276, 220]], [[0, 39, 82, 233]]] 13429
13430 06241902_proc_01430.csv [[[130, 80, 286, 222]], [[123, 244, 230, 343]], [[0, 34, 80, 234]]] 13430
13431 06241902_proc_01431.csv [[[129, 74, 285, 222]], [[136, 244, 243, 344]], [[0, 27, 80, 247]]] 13431
13432 06241902_proc_01432.csv [[[127, 72, 282, 223]], [[145, 245, 247, 345]], [[0, 27, 81, 238]]] 13432
13433 06241902_proc_01433.csv [[[124, 60, 283, 227]], [[147, 242, 253, 346]], [[0, 32, 86, 231]]] 13433
13434 06241902_proc_01434.csv [[[127, 65, 286, 226]], [[150, 244, 254, 349]], [[0, 38, 77, 242]]] 13434
13435 06241902_proc_01435.csv [[[129, 68, 281, 222]], [[153, 245, 254, 343]], [[1, 35, 81, 240]]] 13435
13436 06241902_proc_01436.csv [[[128, 69, 281, 217]], [[156, 245, 252, 339]], [[0, 35, 79, 242]]] 13436
13437 06241902_proc_01437.csv [[[124, 68, 279, 222]], [[1, 33, 88, 226]], [[154, 246, 249, 339]]] 13437
13438 06241902_proc_01438.csv [[[123, 69, 277, 221]], [[1, 35, 86, 228]]] 13438
13439 06241902_proc_01439.csv [[[130, 82, 275, 218]], [[0, 23, 85, 227]]] 13439
13440 06241902_proc_01440.csv [[[135, 100, 258, 210]], [[0, 25, 85, 227]]] 13440
13441 06241902_proc_01441.csv [[[180, 237, 274, 337]], [[1, 27, 85, 225]], [[170, 102, 252, 190]]] 13441
13442 06241902_proc_01442.csv [[[157, 78, 282, 199]], [[170, 230, 279, 341]], [[0, 28, 84, 234]]] 13442
13443 06241902_proc_01443.csv [[[145, 74, 284, 214]], [[166, 232, 278, 343]], [[0, 26, 77, 199]]] 13443
13444 06241902_proc_01444.csv [[[139, 72, 289, 220]], [[160, 239, 272, 344]], [[0, 27, 82, 220]]] 13444
13445 06241902_proc_01445.csv [[[138, 71, 291, 222]], [[160, 248, 261, 343]], [[0, 35, 84, 233]]] 13445
13446 06241902_proc_01446.csv [[[135, 70, 290, 219]], [[158, 248, 258, 346]], [[0, 30, 85, 234]]] 13446
13447 06241902_proc_01447.csv [[[134, 69, 287, 221]], [[165, 253, 260, 344]], [[0, 38, 89, 236]]] 13447
13448 06241902_proc_01448.csv [[[137, 67, 288, 226]], [[0, 34, 90, 232]]] 13448
13449 06241902_proc_01449.csv [[[137, 64, 284, 228]], [[0, 32, 90, 230]], [[145, 249, 228, 343]]] 13449
13450 06241902_proc_01450.csv [[[138, 66, 289, 221]], [[139, 242, 243, 351]], [[0, 22, 97, 216]]] 13450
13451 06241902_proc_01451.csv [[[140, 68, 289, 216]], [[143, 246, 249, 349]], [[1, 32, 83, 204]]] 13451
13452 06241902_proc_01452.csv [[[143, 68, 290, 212]], [[144, 242, 255, 350]], [[1, 32, 86, 199]]] 13452
13453 06241902_proc_01453.csv [[[148, 72, 285, 212]], [[152, 238, 260, 347]], [[0, 21, 103, 215]]] 13453
13454 06241902_proc_01454.csv [[[165, 84, 284, 199]], [[157, 238, 260, 342]], [[0, 30, 90, 225]]] 13454
13455 06241902_proc_01455.csv [[[152, 236, 253, 340]], [[156, 87, 270, 201]], [[0, 34, 89, 237]]] 13455
13456 06241902_proc_01456.csv [[[133, 86, 271, 210]], [[0, 45, 83, 242]]] 13456
13457 06241902_proc_01457.csv [[[137, 81, 281, 216]], [[0, 47, 85, 244]]] 13457
13458 06241902_proc_01458.csv [[[134, 74, 284, 220]], [[0, 55, 86, 239]]] 13458
13459 06241902_proc_01459.csv [[[132, 73, 286, 221]], [[0, 58, 79, 238]], [[193, 237, 285, 337]]] 13459
13460 06241902_proc_01460.csv [[[129, 73, 283, 224]], [[186, 234, 280, 339]], [[0, 30, 69, 228]]] 13460
13461 06241902_proc_01461.csv [[[131, 73, 288, 227]], [[177, 234, 281, 339]], [[0, 32, 80, 226]]] 13461
13462 06241902_proc_01462.csv [[[126, 72, 288, 229]], [[175, 237, 273, 337]], [[0, 30, 73, 227]]] 13462
13463 06241902_proc_01463.csv [[[129, 69, 284, 225]], [[170, 239, 269, 345]], [[0, 30, 74, 229]]] 13463
13464 06241902_proc_01464.csv [[[131, 67, 285, 221]], [[166, 247, 264, 345]], [[0, 46, 76, 226]]] 13464
13465 06241902_proc_01465.csv [[[127, 68, 283, 219]], [[165, 251, 260, 345]], [[0, 42, 86, 228]]] 13465
13466 06241902_proc_01466.csv [[[132, 75, 280, 218]], [[165, 256, 258, 353]], [[1, 37, 82, 227]]] 13466
13467 06241902_proc_01467.csv [[[137, 80, 272, 211]], [[0, 36, 79, 234]], [[166, 252, 250, 348]]] 13467
13468 06241902_proc_01468.csv [[[142, 102, 254, 214]], [[0, 38, 81, 228]]] 13468
13469 06241902_proc_01469.csv [[[145, 247, 242, 350]], [[168, 84, 271, 182]], [[0, 41, 82, 223]]] 13469
13470 06241902_proc_01470.csv [[[153, 68, 274, 193]], [[143, 244, 248, 351]], [[0, 24, 81, 202]]] 13470
13471 06241902_proc_01471.csv [[[137, 66, 282, 213]], [[146, 242, 254, 351]], [[1, 35, 89, 178]]] 13471
13472 06241902_proc_01472.csv [[[140, 68, 286, 217]], [[149, 238, 259, 348]], [[0, 31, 79, 231]]] 13472
13473 06241902_proc_01473.csv [[[135, 62, 287, 225]], [[155, 237, 263, 345]], [[0, 37, 68, 233]]] 13473
13474 06241902_proc_01474.csv [[[134, 60, 287, 227]], [[163, 240, 261, 338]], [[0, 39, 84, 216]]] 13474
13475 06241902_proc_01475.csv [[[135, 67, 290, 226]], [[172, 241, 261, 332]], [[1, 35, 86, 228]]] 13475
13476 06241902_proc_01476.csv [[[135, 72, 292, 228]], [[0, 51, 92, 236]], [[151, 246, 253, 349]]] 13476
13477 06241902_proc_01477.csv [[[138, 70, 287, 228]], [[161, 251, 248, 340]], [[0, 26, 90, 243]]] 13477
13478 06241902_proc_01478.csv [[[136, 71, 293, 228]], [[168, 241, 272, 340]], [[0, 21, 83, 240]]] 13478
13479 06241902_proc_01479.csv [[[138, 71, 286, 218]], [[173, 237, 276, 341]], [[0, 41, 83, 239]]] 13479
13480 06241902_proc_01480.csv [[[138, 63, 286, 217]], [[168, 231, 274, 344]], [[0, 29, 87, 235]]] 13480
13481 06241902_proc_01481.csv [[[141, 60, 289, 212]], [[165, 230, 275, 342]], [[0, 36, 79, 209]]] 13481
13482 06241902_proc_01482.csv [[[165, 235, 267, 342]], [[154, 68, 273, 182]], [[0, 34, 74, 230]]] 13482
13483 06241902_proc_01483.csv [[[159, 237, 263, 344]], [[0, 47, 74, 235]], [[146, 104, 256, 202]]] 13483
13484 06241902_proc_01484.csv [[[135, 78, 276, 211]], [[157, 245, 250, 345]], [[0, 39, 73, 226]]] 13484
13485 06241902_proc_01485.csv [[[131, 74, 282, 218]], [[161, 257, 241, 348]], [[0, 29, 84, 219]]] 13485
13486 06241902_proc_01486.csv [[[134, 72, 280, 219]], [[0, 21, 83, 228]]] 13486
13487 06241902_proc_01487.csv [[[134, 72, 285, 222]], [[0, 32, 82, 237]], [[150, 247, 245, 343]]] 13487
13488 06241902_proc_01488.csv [[[135, 69, 289, 225]], [[151, 247, 241, 349]], [[0, 25, 77, 247]]] 13488
13489 06241902_proc_01489.csv [[[127, 62, 290, 236]], [[147, 243, 249, 349]], [[0, 22, 73, 244]]] 13489
13490 06241902_proc_01490.csv [[[128, 65, 288, 234]], [[145, 243, 253, 349]], [[0, 33, 79, 239]]] 13490
13491 06241902_proc_01491.csv [[[132, 69, 289, 231]], [[147, 246, 252, 349]], [[0, 24, 84, 239]]] 13491
13492 06241902_proc_01492.csv [[[132, 71, 287, 225]], [[152, 245, 263, 350]], [[0, 23, 83, 236]]] 13492
13493 06241902_proc_01493.csv [[[134, 76, 289, 224]], [[155, 243, 269, 347]], [[0, 24, 86, 239]]] 13493
13494 06241902_proc_01494.csv [[[136, 74, 288, 222]], [[163, 244, 270, 346]], [[1, 29, 87, 243]]] 13494
13495 06241902_proc_01495.csv [[[135, 74, 280, 219]], [[175, 237, 281, 341]], [[1, 23, 88, 241]]] 13495
13496 06241902_proc_01496.csv [[[137, 81, 275, 217]], [[1, 24, 88, 237]]] 13496
13497 06241902_proc_01497.csv [[[140, 84, 279, 212]], [[166, 249, 258, 341]]] 13497
13498 06241902_proc_01498.csv [[[129, 74, 281, 214]], [[167, 235, 270, 341]], [[0, 42, 81, 229]]] 13498
13499 06241902_proc_01499.csv [[[132, 67, 286, 214]], [[162, 232, 275, 346]], [[0, 29, 82, 230]]] 13499
13500 06241902_proc_01500.csv [[[128, 63, 284, 217]], [[156, 229, 273, 344]], [[0, 21, 84, 223]], [[9, 295, 114, 384]]] 13500
13501 06241902_proc_01501.csv [[[128, 62, 283, 221]], [[158, 233, 267, 341]], [[0, 24, 86, 209]], [[7, 279, 114, 384]]] 13501
13502 06241902_proc_01502.csv [[[132, 64, 284, 222]], [[156, 236, 260, 342]], [[0, 30, 88, 202]], [[2, 275, 116, 383]]] 13502
13503 06241902_proc_01503.csv [[[127, 65, 283, 226]], [[153, 243, 249, 342]], [[0, 32, 85, 187]], [[3, 280, 121, 385]]] 13503
13504 06241902_proc_01504.csv [[[128, 62, 285, 227]], [[136, 245, 241, 345]], [[0, 34, 79, 227]]] 13504
13505 06241902_proc_01505.csv [[[129, 66, 285, 229]], [[0, 35, 80, 234]], [[135, 260, 213, 344]]] 13505
13506 06241902_proc_01506.csv [[[129, 64, 284, 230]], [[3, 38, 93, 228]], [[185, 245, 269, 339]]] 13506
13507 06241902_proc_01507.csv [[[132, 73, 285, 224]], [[163, 245, 259, 346]], [[0, 29, 88, 231]]] 13507
13508 06241902_proc_01508.csv [[[137, 75, 284, 221]], [[155, 246, 252, 350]], [[1, 35, 90, 235]], [[14, 297, 116, 385]]] 13508
13509 06241902_proc_01509.csv [[[146, 77, 280, 213]], [[152, 244, 256, 352]], [[0, 27, 86, 244]], [[6, 290, 105, 384]]] 13509
13510 06241902_proc_01510.csv [[[153, 246, 260, 350]], [[3, 286, 109, 383]], [[162, 79, 270, 184]], [[0, 36, 84, 243]]] 13510
13511 06241902_proc_01511.csv [[[151, 245, 263, 352]], [[164, 105, 266, 209]], [[3, 285, 111, 383]], [[0, 30, 84, 238]]] 13511
13512 06241902_proc_01512.csv [[[152, 99, 277, 216]], [[155, 244, 266, 350]], [[2, 287, 108, 383]], [[0, 24, 84, 238]]] 13512
13513 06241902_proc_01513.csv [[[143, 79, 288, 223]], [[166, 245, 270, 344]], [[3, 284, 109, 383]], [[0, 25, 87, 235]]] 13513
13514 06241902_proc_01514.csv [[[136, 73, 291, 222]], [[0, 28, 77, 220]], [[4, 280, 107, 381]]] 13514
13515 06241902_proc_01515.csv [[[133, 68, 289, 221]], [[0, 25, 76, 221]]] 13515
13516 06241902_proc_01516.csv [[[130, 63, 287, 221]], [[0, 24, 79, 232]], [[149, 241, 242, 345]]] 13516
13517 06241902_proc_01517.csv [[[133, 60, 290, 225]], [[162, 241, 259, 345]], [[0, 30, 76, 240]]] 13517
13518 06241902_proc_01518.csv [[[132, 67, 289, 226]], [[162, 236, 263, 345]], [[0, 49, 82, 240]]] 13518
13519 06241902_proc_01519.csv [[[139, 72, 285, 224]], [[159, 231, 268, 342]], [[0, 43, 88, 239]]] 13519
13520 06241902_proc_01520.csv [[[136, 76, 284, 223]], [[157, 231, 268, 341]], [[0, 30, 86, 240]]] 13520
13521 06241902_proc_01521.csv [[[137, 80, 287, 226]], [[159, 232, 264, 345]], [[0, 32, 86, 242]]] 13521
13522 06241902_proc_01522.csv [[[135, 82, 289, 229]], [[164, 246, 253, 344]], [[0, 37, 81, 244]]] 13522
13523 06241902_proc_01523.csv [[[136, 85, 287, 227]], [[0, 36, 74, 243]]] 13523
13524 06241902_proc_01524.csv [[[138, 93, 285, 225]], [[0, 30, 81, 229]]] 13524
13525 06241902_proc_01525.csv [[[161, 93, 265, 205]], [[178, 242, 283, 344]], [[0, 28, 84, 221]]] 13525
13526 06241902_proc_01526.csv [[[165, 241, 274, 346]], [[136, 89, 273, 217]], [[0, 45, 83, 232]]] 13526
13527 06241902_proc_01527.csv [[[135, 84, 277, 219]], [[161, 243, 266, 347]], [[1, 38, 85, 231]]] 13527
13528 06241902_proc_01528.csv [[[132, 74, 277, 218]], [[158, 242, 266, 353]], [[0, 31, 90, 231]]] 13528
13529 06241902_proc_01529.csv [[[129, 65, 285, 222]], [[161, 243, 263, 351]], [[1, 35, 86, 238]]] 13529
13530 06241902_proc_01530.csv [[[129, 69, 284, 220]], [[160, 246, 262, 351]], [[1, 34, 79, 246]]] 13530
13531 06241902_proc_01531.csv [[[127, 67, 284, 225]], [[167, 245, 266, 348]], [[1, 40, 79, 244]]] 13531
13532 06241902_proc_01532.csv [[[127, 64, 285, 221]], [[0, 42, 79, 240]], [[177, 255, 275, 351]]] 13532
13533 06241902_proc_01533.csv [[[130, 67, 283, 225]], [[1, 38, 86, 238]]] 13533
13534 06241902_proc_01534.csv [[[127, 63, 284, 225]], [[0, 33, 87, 237]]] 13534
13535 06241902_proc_01535.csv [[[130, 66, 282, 221]], [[0, 37, 92, 236]], [[132, 238, 235, 338]], [[3, 307, 101, 386]]] 13535
13536 06241902_proc_01536.csv [[[139, 76, 277, 218]], [[151, 242, 246, 338]], [[0, 36, 90, 220]]] 13536
13537 06241902_proc_01537.csv [[[139, 88, 273, 217]], [[154, 242, 251, 339]], [[0, 35, 83, 212]], [[5, 300, 123, 385]]] 13537
13538 06241902_proc_01538.csv [[[140, 101, 269, 211]], [[150, 241, 256, 340]], [[0, 36, 88, 185]], [[8, 301, 124, 385]]] 13538
13539 06241902_proc_01539.csv [[[178, 105, 266, 200]], [[145, 234, 259, 344]], [[0, 28, 83, 206]], [[9, 299, 122, 385]]] 13539
13540 06241902_proc_01540.csv [[[159, 98, 280, 213]], [[149, 238, 252, 341]], [[0, 27, 82, 203]], [[12, 298, 128, 384]]] 13540
13541 06241902_proc_01541.csv [[[146, 92, 286, 220]], [[153, 241, 245, 334]], [[0, 31, 83, 219]]] 13541
13542 06241902_proc_01542.csv [[[140, 82, 291, 228]], [[0, 29, 80, 221]], [[148, 245, 237, 332]]] 13542
13543 06241902_proc_01543.csv [[[145, 82, 284, 227]], [[1, 25, 85, 238]]] 13543
13544 06241902_proc_01544.csv [[[138, 73, 292, 222]], [[0, 25, 89, 244]], [[186, 259, 276, 352]]] 13544
13545 06241902_proc_01545.csv [[[141, 73, 289, 220]], [[170, 249, 267, 346]], [[0, 30, 92, 261]]] 13545
13546 06241902_proc_01546.csv [[[142, 68, 288, 221]], [[164, 247, 266, 347]], [[0, 31, 86, 240]]] 13546
13547 06241902_proc_01547.csv [[[139, 70, 288, 217]], [[162, 245, 266, 345]], [[0, 31, 84, 237]]] 13547
13548 06241902_proc_01548.csv [[[142, 68, 291, 217]], [[163, 245, 268, 347]], [[1, 31, 78, 252]]] 13548
13549 06241902_proc_01549.csv [[[145, 74, 287, 215]], [[167, 248, 265, 348]], [[0, 27, 87, 245]]] 13549
13550 06241902_proc_01550.csv [[[147, 85, 281, 215]], [[173, 254, 266, 345]], [[0, 31, 86, 239]]] 13550
13551 06241902_proc_01551.csv [[[146, 93, 278, 216]], [[2, 29, 84, 233]], [[183, 260, 274, 346]]] 13551
13552 06241902_proc_01552.csv [[[153, 103, 268, 212]], [[0, 22, 92, 226]]] 13552
13553 06241902_proc_01553.csv [[[161, 82, 269, 198]], [[2, 19, 94, 218]]] 13553
13554 06241902_proc_01554.csv [[[158, 92, 267, 202]], [[0, 30, 89, 230]], [[141, 233, 237, 323]]] 13554
13555 06241902_proc_01555.csv [[[142, 84, 270, 212]], [[0, 29, 89, 232]], [[144, 232, 250, 334]]] 13555
13556 06241902_proc_01556.csv [[[133, 78, 272, 219]], [[157, 241, 250, 335]], [[0, 36, 81, 239]]] 13556
13557 06241902_proc_01557.csv [[[130, 72, 274, 221]], [[148, 238, 249, 338]], [[0, 33, 87, 244]]] 13557
13558 06241902_proc_01558.csv [[[132, 75, 278, 219]], [[144, 240, 242, 338]], [[0, 36, 84, 229]]] 13558
13559 06241902_proc_01559.csv [[[126, 68, 280, 220]], [[145, 243, 239, 340]], [[0, 40, 84, 229]]] 13559
13560 06241902_proc_01560.csv [[[124, 64, 282, 220]], [[149, 245, 236, 337]], [[0, 45, 85, 229]]] 13560
13561 06241902_proc_01561.csv [[[125, 69, 280, 219]], [[0, 55, 80, 238]], [[139, 243, 232, 330]]] 13561
13562 06241902_proc_01562.csv [[[128, 71, 281, 218]], [[1, 57, 80, 238]]] 13562
13563 06241902_proc_01563.csv [[[130, 71, 283, 216]], [[0, 37, 79, 242]]] 13563
13564 06241902_proc_01564.csv [[[129, 75, 276, 213]], [[0, 32, 81, 238]], [[181, 264, 254, 348]]] 13564
13565 06241902_proc_01565.csv [[[131, 83, 272, 208]], [[171, 257, 258, 347]], [[0, 24, 87, 237]]] 13565
13566 06241902_proc_01566.csv [[[133, 83, 263, 204]], [[171, 253, 263, 342]], [[1, 33, 87, 234]]] 13566
13567 06241902_proc_01567.csv [[[171, 252, 265, 343]], [[160, 104, 257, 192]], [[1, 42, 80, 202]]] 13567
13568 06241902_proc_01568.csv [[[147, 82, 283, 208]], [[175, 250, 267, 344]], [[1, 38, 87, 218]]] 13568
13569 06241902_proc_01569.csv [[[148, 81, 286, 211]], [[181, 251, 269, 340]], [[0, 35, 85, 221]]] 13569
13570 06241902_proc_01570.csv [[[147, 83, 284, 219]], [[0, 33, 79, 233]], [[186, 252, 277, 341]]] 13570
13571 06241902_proc_01571.csv [[[147, 81, 288, 222]], [[0, 41, 77, 241]]] 13571
13572 06241902_proc_01572.csv [[[146, 79, 290, 221]], [[0, 58, 79, 246]]] 13572
13573 06241902_proc_01573.csv [[[148, 83, 290, 224]], [[0, 38, 83, 245]], [[143, 249, 234, 316]]] 13573
13574 06241902_proc_01574.csv [[[145, 76, 290, 225]], [[159, 238, 249, 321]], [[0, 40, 83, 243]]] 13574
13575 06241902_proc_01575.csv [[[144, 76, 292, 225]], [[161, 235, 253, 330]], [[0, 47, 79, 243]]] 13575
13576 06241902_proc_01576.csv [[[139, 75, 292, 219]], [[159, 241, 250, 335]], [[1, 49, 73, 242]]] 13576
13577 06241902_proc_01577.csv [[[143, 70, 290, 215]], [[153, 243, 243, 335]], [[0, 34, 72, 219]]] 13577
13578 06241902_proc_01578.csv [[[143, 74, 287, 213]], [[1, 33, 77, 209]], [[138, 242, 229, 336]]] 13578
13579 06241902_proc_01579.csv [[[149, 79, 281, 212]], [[0, 38, 79, 217]], [[149, 255, 222, 329]]] 13579
13580 06241902_proc_01580.csv [[[158, 99, 268, 201]], [[1, 32, 79, 233]]] 13580
13581 06241902_proc_01581.csv [[[0, 26, 81, 237]]] 13581
13582 06241902_proc_01582.csv [[[152, 82, 265, 197]], [[0, 34, 85, 239]]] 13582
13583 06241902_proc_01583.csv [[[131, 78, 270, 202]], [[0, 36, 83, 241]]] 13583
13584 06241902_proc_01584.csv [[[131, 77, 275, 216]], [[0, 39, 81, 239]], [[174, 262, 254, 344]]] 13584
13585 06241902_proc_01585.csv [[[133, 79, 279, 216]], [[169, 254, 259, 345]], [[0, 29, 76, 241]]] 13585
13586 06241902_proc_01586.csv [[[129, 78, 278, 220]], [[172, 255, 262, 347]], [[0, 28, 82, 235]]] 13586
13587 06241902_proc_01587.csv [[[128, 72, 279, 220]], [[182, 251, 269, 343]], [[1, 27, 88, 241]]] 13587
13588 06241902_proc_01588.csv [[[132, 72, 277, 216]], [[188, 253, 274, 340]], [[0, 34, 84, 231]]] 13588
13589 06241902_proc_01589.csv [[[133, 71, 279, 218]], [[1, 31, 83, 242]], [[202, 253, 279, 342]]] 13589
13590 06241902_proc_01590.csv [[[132, 71, 281, 218]], [[0, 49, 88, 241]]] 13590
13591 06241902_proc_01591.csv [[[128, 71, 281, 219]], [[0, 36, 86, 236]]] 13591
13592 06241902_proc_01592.csv [[[134, 75, 277, 210]], [[0, 35, 85, 244]]] 13592
13593 06241902_proc_01593.csv [[[144, 81, 275, 203]], [[155, 234, 247, 324]], [[0, 52, 80, 244]]] 13593
13594 06241902_proc_01594.csv [[[156, 232, 254, 330]], [[166, 93, 265, 195]], [[0, 39, 78, 245]]] 13594
13595 06241902_proc_01595.csv [[[164, 234, 255, 329]], [[0, 31, 82, 236]], [[170, 104, 251, 187]]] 13595
13596 06241902_proc_01596.csv [[[168, 95, 272, 187]], [[0, 33, 81, 231]], [[159, 240, 242, 329]]] 13596
13597 06241902_proc_01597.csv [[[156, 81, 278, 207]], [[153, 248, 230, 327]], [[0, 41, 84, 230]]] 13597
13598 06241902_proc_01598.csv [[[150, 85, 283, 212]], [[0, 55, 89, 240]], [[146, 254, 217, 321]]] 13598
13599 06241902_proc_01599.csv [[[145, 80, 286, 214]], [[2, 55, 96, 246]]] 13599
res_df.to_csv('bboxes_all_frames.csv', index=False)

Draw bounding boxes

results = []  # list to become dataframe
for i in range(0,len(preds)):
    meta_filename = str(Path(img_files[i]).stem)+'.csv'
    bblist = get_bblist(preds[i])
    if [] != bblist: 
        draw_bbs_on_img(imgs_array[i], bblist, verbose=verbose)  # draws in place # do this after crops
    line_list = [meta_filename, bblist, i]
print(f"\n{len(results)} images processed.")
imgs_array[13500]

Now save the bbox-drawn images to files

boxdir = 'box_drawings_iv'
mkdir_if_needed(boxdir)
for i in range(len(imgs_array)):
    fname = img_files[i]
    outname = boxdir+'/'+str(Path(fname).stem)+'_B.png'
    if i%500 == 0: print(f"{i}: saving {outname}")
    imgs_array[i].save (outname)
0: saving box_drawings_iv/06240907_proc_00000_B.png
500: saving box_drawings_iv/06240907_proc_00500_B.png
1000: saving box_drawings_iv/06240907_proc_01000_B.png
1500: saving box_drawings_iv/06240907_proc_01500_B.png
2000: saving box_drawings_iv/06240909_proc_00000_B.png
2500: saving box_drawings_iv/06240909_proc_00500_B.png
3000: saving box_drawings_iv/06240909_proc_01000_B.png
3500: saving box_drawings_iv/06240909_proc_01500_B.png
4000: saving box_drawings_iv/06240910_proc_00000_B.png
4500: saving box_drawings_iv/06240910_proc_00500_B.png
5000: saving box_drawings_iv/06240910_proc_01000_B.png
5500: saving box_drawings_iv/06240910_proc_01500_B.png
6000: saving box_drawings_iv/06240912_proc_00000_B.png
6500: saving box_drawings_iv/06240912_proc_00500_B.png
7000: saving box_drawings_iv/06240912_proc_01000_B.png
7500: saving box_drawings_iv/06240912_proc_01500_B.png
8000: saving box_drawings_iv/06240913_proc_00000_B.png
8500: saving box_drawings_iv/06240913_proc_00500_B.png
9000: saving box_drawings_iv/06240913_proc_01000_B.png
9500: saving box_drawings_iv/06240913_proc_01500_B.png
10000: saving box_drawings_iv/06240914_proc_00000_B.png
10500: saving box_drawings_iv/06240914_proc_00500_B.png
11000: saving box_drawings_iv/06240914_proc_01000_B.png
11500: saving box_drawings_iv/06240914_proc_01500_B.png
12000: saving box_drawings_iv/06241902_proc_00000_B.png
12500: saving box_drawings_iv/06241902_proc_00500_B.png
13000: saving box_drawings_iv/06241902_proc_01000_B.png
13500: saving box_drawings_iv/06241902_proc_01500_B.png

Make a movie of the bounding boxes

# cd box_drawings_iv; ffmpeg -loglevel quiet -r 30 -pattern_type glob -i '*.png' -c:v libx264 bboxes.mp4
from IPython.display import YouTubeVideo
YouTubeVideo('-5cDhgbomH0')