dnth commited on
Commit
1398914
1 Parent(s): c0ffd2a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from icevision.all import *
2
+ import icedata
3
+ import PIL, requests
4
+ import torch
5
+ from torchvision import transforms
6
+ import gradio as gr
7
+
8
+ # Download the dataset
9
+ url = "https://cvbp-secondary.z19.web.core.windows.net/datasets/object_detection/odFridgeObjects.zip"
10
+ dest_dir = "fridge"
11
+ data_dir = icedata.load_data(url, dest_dir)
12
+
13
+ # Create the parser
14
+
15
+
16
+ parser = parsers.VOCBBoxParser(annotations_dir="Images/Annotated/augmented", images_dir="Images/Annotated/augmented")
17
+
18
+
19
+ # Parse annotations to create records
20
+ train_records, valid_records = parser.parse()
21
+
22
+ class_map = parser.class_map
23
+
24
+ extra_args = {}
25
+ model_type = models.torchvision.retinanet
26
+ backbone = model_type.backbones.resnet50_fpn
27
+ # Instantiate the model
28
+ model = model_type.model(backbone=backbone(pretrained=True), num_classes=len(parser.class_map), **extra_args)
29
+
30
+ # Transforms
31
+ # size is set to 384 because EfficientDet requires its inputs to be divisible by 128
32
+ image_size = 640
33
+ train_tfms = tfms.A.Adapter([*tfms.A.aug_tfms(size=image_size, presize=768), tfms.A.Normalize()])
34
+ valid_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(image_size), tfms.A.Normalize()])
35
+ # Datasets
36
+ train_ds = Dataset(train_records, train_tfms)
37
+ valid_ds = Dataset(valid_records, valid_tfms)
38
+ # Data Loaders
39
+ train_dl = model_type.train_dl(train_ds, batch_size=8, num_workers=4, shuffle=True)
40
+ valid_dl = model_type.valid_dl(valid_ds, batch_size=8, num_workers=4, shuffle=False)
41
+ metrics = [COCOMetric(metric_type=COCOMetricType.bbox)]
42
+ learn = model_type.fastai.learner(dls=[train_dl, valid_dl], model=model, metrics=metrics)
43
+
44
+ learn = learn.load('model')
45
+
46
+ import os
47
+ for root, dirs, files in os.walk(r'sample_images/'):
48
+ for filename in files:
49
+ print(filename)
50
+
51
+ examples = ["sample_images/"+file for file in files]
52
+ article="<p style='text-align: center'><a href='https://dicksonneoh.com/fridge-detector/' target='_blank'>Blog post</a></p>"
53
+ enable_queue=True
54
+
55
+
56
+ #examples = [['sample_images/3.jpg']]
57
+ examples = [["sample_images/"+file] for file in files]
58
+
59
+ def show_preds(input_image, display_label, display_bbox, detection_threshold):
60
+
61
+ if detection_threshold==0: detection_threshold=0.5
62
+
63
+ img = PIL.Image.fromarray(input_image, 'RGB')
64
+
65
+ pred_dict = model_type.end2end_detect(img, valid_tfms, model, class_map=class_map, detection_threshold=detection_threshold,
66
+ display_label=display_label, display_bbox=display_bbox, return_img=True,
67
+ font_size=16, label_color="#FF59D6")
68
+
69
+ return pred_dict['img']
70
+
71
+ # display_chkbox = gr.inputs.CheckboxGroup(["Label", "BBox"], label="Display", default=True)
72
+ display_chkbox_label = gr.inputs.Checkbox(label="Label", default=True)
73
+ display_chkbox_box = gr.inputs.Checkbox(label="Box", default=True)
74
+
75
+ detection_threshold_slider = gr.inputs.Slider(minimum=0, maximum=1, step=0.1, default=0.5, label="Detection Threshold")
76
+
77
+ outputs = gr.outputs.Image(type="pil")
78
+
79
+ # Option 1: Get an image from local drive
80
+ gr_interface = gr.Interface(fn=show_preds, inputs=["image", display_chkbox_label, display_chkbox_box, detection_threshold_slider], outputs=outputs, title='IceApp - Fridge Object', article=article, examples=examples)
81
+
82
+ # # Option 2: Grab an image from a webcam
83
+ # gr_interface = gr.Interface(fn=show_preds, inputs=["webcam", display_chkbox_label, display_chkbox_box, detection_threshold_slider], outputs=outputs, title='IceApp - COCO', live=False)
84
+
85
+ # # Option 3: Continuous image stream from the webcam
86
+ # gr_interface = gr.Interface(fn=show_preds, inputs=["webcam", display_chkbox_label, display_chkbox_box, detection_threshold_slider], outputs=outputs, title='IceApp - COCO', live=True)
87
+
88
+
89
+ gr_interface.launch(inline=False, share=True, debug=True)