ismot Stuti commited on
Commit
595ba46
·
0 Parent(s):

Duplicate from Stuti/detect-for-drone

Browse files

Co-authored-by: Stuti <[email protected]>

.gitattributes ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.npy filter=lfs diff=lfs merge=lfs -text
13
+ *.npz filter=lfs diff=lfs merge=lfs -text
14
+ *.onnx filter=lfs diff=lfs merge=lfs -text
15
+ *.ot filter=lfs diff=lfs merge=lfs -text
16
+ *.parquet filter=lfs diff=lfs merge=lfs -text
17
+ *.pickle filter=lfs diff=lfs merge=lfs -text
18
+ *.pkl filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pt filter=lfs diff=lfs merge=lfs -text
21
+ *.pth filter=lfs diff=lfs merge=lfs -text
22
+ *.rar filter=lfs diff=lfs merge=lfs -text
23
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
24
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
25
+ *.tflite filter=lfs diff=lfs merge=lfs -text
26
+ *.tgz filter=lfs diff=lfs merge=lfs -text
27
+ *.wasm filter=lfs diff=lfs merge=lfs -text
28
+ *.xz filter=lfs diff=lfs merge=lfs -text
29
+ *.zip filter=lfs diff=lfs merge=lfs -text
30
+ *.zst filter=lfs diff=lfs merge=lfs -text
31
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
1daaadc1e83fcecc7bfa920ed2773653.jpeg ADDED
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Detect For Drone
3
+ emoji: 🐢
4
+ colorFrom: red
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: 3.3
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: Stuti/detect-for-drone
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import gradio as gr
3
+ import matplotlib.pyplot as plt
4
+ import requests, validators
5
+ import torch
6
+ import pathlib
7
+ from PIL import Image
8
+ from transformers import AutoFeatureExtractor, DetrForObjectDetection
9
+ import os
10
+
11
+ # colors for visualization
12
+ COLORS = [
13
+ [0.000, 0.447, 0.741],
14
+ [0.850, 0.325, 0.098],
15
+ [0.929, 0.694, 0.125],
16
+ [0.494, 0.184, 0.556],
17
+ [0.466, 0.674, 0.188],
18
+ [0.301, 0.745, 0.933]
19
+ ]
20
+
21
+ def make_prediction(img, feature_extractor, model):
22
+ inputs = feature_extractor(img, return_tensors="pt")
23
+ outputs = model(**inputs)
24
+ img_size = torch.tensor([tuple(reversed(img.size))])
25
+ processed_outputs = feature_extractor.post_process(outputs, img_size)
26
+ return processed_outputs[0]
27
+
28
+ def fig2img(fig):
29
+ buf = io.BytesIO()
30
+ fig.savefig(buf)
31
+ buf.seek(0)
32
+ img = Image.open(buf)
33
+ return img
34
+
35
+
36
+ def visualize_prediction(pil_img, output_dict, threshold=0.7, id2label=None):
37
+ keep = output_dict["scores"] > threshold
38
+ boxes = output_dict["boxes"][keep].tolist()
39
+ scores = output_dict["scores"][keep].tolist()
40
+ labels = output_dict["labels"][keep].tolist()
41
+ if id2label is not None:
42
+ labels = [id2label[x] for x in labels]
43
+
44
+ plt.figure(figsize=(16, 10))
45
+ plt.imshow(pil_img)
46
+ ax = plt.gca()
47
+ colors = COLORS * 100
48
+ for score, (xmin, ymin, xmax, ymax), label, color in zip(scores, boxes, labels, colors):
49
+ ax.add_patch(plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, fill=False, color=color, linewidth=3))
50
+ ax.text(xmin, ymin, f"{label}: {score:0.2f}", fontsize=15, bbox=dict(facecolor="yellow", alpha=0.5))
51
+ plt.axis("off")
52
+ return fig2img(plt.gcf())
53
+
54
+ def detect_objects(model_name,image_input,threshold):
55
+
56
+ #Extract model and feature extractor
57
+ feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
58
+
59
+ if 'detr' in model_name:
60
+ model = DetrForObjectDetection.from_pretrained(model_name)
61
+
62
+ if image_input:
63
+ image = image_input
64
+
65
+ #Make prediction
66
+ processed_outputs = make_prediction(image, feature_extractor, model)
67
+
68
+ #Visualize prediction
69
+ viz_img = visualize_prediction(image, processed_outputs, threshold, model.config.id2label)
70
+
71
+ return viz_img
72
+
73
+ def set_example_image(example: list) -> dict:
74
+ return gr.Image.update(value=example[0])
75
+
76
+
77
+
78
+ title = """<h1 id="title">Detection for Drone</h1>"""
79
+
80
+ description = """
81
+ Links to HuggingFace Models:
82
+ - [facebook/detr-resnet-50](https://huggingface.co/facebook/detr-resnet-50)
83
+ - [facebook/detr-resnet-101](https://huggingface.co/facebook/detr-resnet-101)
84
+ """
85
+
86
+ models = ["facebook/detr-resnet-50","facebook/detr-resnet-101"]
87
+ #examples = ['1daaadc1e83fcecc7bfa920ed2773653.jpeg']
88
+ css = '''
89
+ h1#title {
90
+ text-align: center;
91
+ }
92
+ '''
93
+ demo = gr.Blocks(css=css)
94
+
95
+ with demo:
96
+ gr.Markdown(title)
97
+ gr.Markdown(description)
98
+ options = gr.Dropdown(choices=models,label='Select Object Detection Model',show_label=True)
99
+ slider_input = gr.Slider(minimum=0.2,maximum=1,value=0.7,label='Prediction Threshold')
100
+
101
+ with gr.Tabs():
102
+ with gr.TabItem('Image Upload'):
103
+ with gr.Row():
104
+ img_input = gr.Image(type='pil')
105
+ img_output_from_upload= gr.Image(shape=(650,650))
106
+
107
+ with gr.Row():
108
+ example_images = gr.Dataset(components=[img_input],
109
+ samples=[[path.as_posix()]
110
+ for path in sorted(pathlib.Path('images').rglob('*.jpeg'))])
111
+
112
+ img_but = gr.Button('Detect')
113
+
114
+
115
+ img_but.click(detect_objects,inputs=[options,img_input,slider_input],outputs=img_output_from_upload,queue=True)
116
+ example_images.click(fn=set_example_image,inputs=[example_images],outputs=[img_input])
117
+
118
+
119
+ #gr.Markdown("![visitor badge](https://visitor-badge.glitch.me/badge?page_id=nickmuchi-object-detection-with-detr-and-yolos)")
120
+
121
+
122
+ demo.launch(enable_queue=True)
images/.DS_Store ADDED
Binary file (6.15 kB). View file
 
images/1daaadc1e83fcecc7bfa920ed2773653.jpeg ADDED
images/FLN_8240-1024x682-1.jpeg ADDED
images/I0000SBQgME2LSFI.jpeg ADDED
images/miami-beach-floridanorth-beachcondominium-residential-apartment-apartments-building-buildings-housingbalcony-viewaerial-overhead-view-from-abovea-R3A8YH.jpeg ADDED
images/woman-locking-door-while-standing-with-dog-at-balcony-MEUF03776.jpeg ADDED
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ beautifulsoup4==4.9.3
2
+ bs4==0.0.1
3
+ requests-file==1.5.1
4
+ torch==1.10.1
5
+ git+https://github.com/huggingface/transformers.git
6
+ validators==0.18.2
7
+ timm==0.5.4