Nikhil0987 commited on
Commit
b88cd09
·
verified ·
1 Parent(s): 4821690

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ for dirname, _, filenames in os.walk('/kaggle/input'):
3
+ for filename in filenames:
4
+ print(os.path.join(dirname, filename))
5
+
6
+
7
+
8
+
9
+ #Importing Libraries
10
+ import cv2
11
+ import matplotlib.pyplot as plt
12
+ %matplotlib inline
13
+ from IPython.display import Image
14
+
15
+ import keras_cv
16
+ import keras_core as keras
17
+
18
+
19
+
20
+ from collections import defaultdict
21
+ import json
22
+
23
+ class COCOParser:
24
+ def __init__(self, anns_file, imgs_dir):
25
+ with open(anns_file, 'r') as f:
26
+ coco = json.load(f)
27
+
28
+ self.annIm_dict = defaultdict(list)
29
+ self.cat_dict = {}
30
+ self.annId_dict = {}
31
+ self.im_dict = {}
32
+ self.licenses_dict = {}
33
+
34
+ for ann in coco['annotations']:
35
+ self.annIm_dict[ann['image_id']].append(ann)
36
+ self.annId_dict[ann['id']]=ann
37
+ for img in coco['images']:
38
+ self.im_dict[img['id']] = img
39
+ for cat in coco['categories']:
40
+ self.cat_dict[cat['id']] = cat
41
+ for license in coco['licenses']:
42
+ self.licenses_dict[license['id']] = license
43
+
44
+ def get_imgIds(self):
45
+ return list(self.im_dict.keys())
46
+ def get_annIds(self, im_ids):
47
+ im_ids=im_ids if isinstance(im_ids, list) else [im_ids]
48
+ return [ann['id'] for im_id in im_ids for ann in self.annIm_dict[im_id]]
49
+ def load_anns(self, ann_ids):
50
+ im_ids=ann_ids if isinstance(ann_ids, list) else [ann_ids]
51
+ return [self.annId_dict[ann_id] for ann_id in ann_ids]
52
+ def load_cats(self, class_ids):
53
+ class_ids=class_ids if isinstance(class_ids, list) else [class_ids]
54
+ return [self.cat_dict[class_id] for class_id in class_ids]
55
+ def get_imgLicenses(self,im_ids):
56
+ im_ids=im_ids if isinstance(im_ids, list) else [im_ids]
57
+ lic_ids = [self.im_dict[im_id]["license"] for im_id in im_ids]
58
+ return [self.licenses_dict[lic_id] for lic_id in lic_ids]
59
+ coco_images_dir = "/kaggle/input/coco-2017-dataset/coco2017/train2017"
60
+ annot_file = "/kaggle/input/coco-2017-dataset/coco2017/annotations/instances_train2017.json"
61
+
62
+ coco = COCOParser(annot_file, coco_images_dir)
63
+ from PIL import Image
64
+ import numpy as np
65
+
66
+ # define a list of colors for drawing bounding boxes
67
+ color_list = ["pink", "red", "teal", "blue", "orange", "yellow", "black", "magenta","green","aqua"]*10
68
+ num_imgs_to_disp = 4
69
+ total_images = len(coco.get_imgIds()) # total number of images
70
+ sel_im_idxs = np.random.permutation(total_images)[:num_imgs_to_disp]
71
+ img_ids = coco.get_imgIds()
72
+ selected_img_ids = [img_ids[i] for i in sel_im_idxs]
73
+ ann_ids = coco.get_annIds(selected_img_ids)
74
+ im_licenses = coco.get_imgLicenses(selected_img_ids)
75
+
76
+ fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(15,10))
77
+ ax = ax.ravel()
78
+
79
+ #Loading and visualizing the dataset
80
+ for i, im in enumerate(selected_img_ids):
81
+ image = Image.open(f"{coco_images_dir}/{str(im).zfill(12)}.jpg")
82
+ ann_ids = coco.get_annIds(im)
83
+ annotations = coco.load_anns(ann_ids)
84
+
85
+ for ann in annotations:
86
+ bbox = ann['bbox']
87
+ x, y, w, h = [int(b) for b in bbox]
88
+ class_id = ann["category_id"]
89
+ class_name = coco.load_cats(class_id)[0]["name"]
90
+ license = coco.get_imgLicenses(im)[0]["name"]
91
+ color_ = color_list[class_id]
92
+ rect = plt.Rectangle((x, y), w, h, linewidth=2, edgecolor=color_, facecolor='none')
93
+ t_box=ax[i].text(x, y, class_name, color='red', fontsize=10)
94
+ t_box.set_bbox(dict(boxstyle='square, pad=0',facecolor='white', alpha=0.6, edgecolor='blue'))
95
+ ax[i].add_patch(rect)
96
+
97
+ ax[i].axis('off')
98
+ ax[i].imshow(image)
99
+ ax[i].set_xlabel('Longitude')
100
+ ax[i].set_title(f"License: {license}")
101
+ plt.tight_layout()
102
+ plt.show()