David Thomas commited on
Commit
4b0ce20
·
1 Parent(s): 1f13352
Files changed (5) hide show
  1. .env +1 -0
  2. app.py +40 -0
  3. cow1.jpg +0 -0
  4. requirements.txt +4 -0
  5. test.ipynb +255 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ HUGGINGFACE_KEY = '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'
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ Streamlit UI for object detection with DETR. """
2
+
3
+ # Use a pipeline as a high-level helper
4
+ from transformers import pipeline
5
+ import streamlit as st
6
+ from PIL import Image
7
+ import pandas as pd
8
+
9
+ pipe = pipeline("object-detection", model="facebook/detr-resnet-101")
10
+
11
+ # Set the title
12
+ st.title("Vision Quest 2")
13
+
14
+ results = None
15
+ image = None
16
+
17
+ # Create a file uploader and set the upload type to images
18
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
19
+
20
+ if uploaded_file:
21
+ upload_image_button = st.button("Upload Image")
22
+ if upload_image_button:
23
+ with st.spinner("Uploading Image...")
24
+ # Convert the image to a file object
25
+ image = Image.open(uploaded_file)
26
+
27
+ # Process the image through the pipeline
28
+ results = pipe(image)
29
+
30
+ col1, col2 = st.columns(2)
31
+ if image and results:
32
+ with col1:
33
+ st.image(image, use_column_width=True)
34
+ with col2:
35
+ # Display the individual objects, the bounding boxes, and the confidence
36
+ # And then display the total number of each type of object
37
+ # Create a dataframe to hold the results
38
+ df = pd.DataFrame(results)
39
+ st.dataframe(df)
40
+
cow1.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ transformers
2
+ streamlit
3
+ PIL
4
+ pandas
test.ipynb ADDED
@@ -0,0 +1,255 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 2,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "data": {
10
+ "application/vnd.jupyter.widget-view+json": {
11
+ "model_id": "27a09705e50844998302f7953225305f",
12
+ "version_major": 2,
13
+ "version_minor": 0
14
+ },
15
+ "text/plain": [
16
+ "Downloading (…)lve/main/config.json: 0%| | 0.00/4.38k [00:00<?, ?B/s]"
17
+ ]
18
+ },
19
+ "metadata": {},
20
+ "output_type": "display_data"
21
+ },
22
+ {
23
+ "data": {
24
+ "application/vnd.jupyter.widget-view+json": {
25
+ "model_id": "b08c2c5730864354b556eee28bf230fe",
26
+ "version_major": 2,
27
+ "version_minor": 0
28
+ },
29
+ "text/plain": [
30
+ "Downloading model.safetensors: 0%| | 0.00/243M [00:00<?, ?B/s]"
31
+ ]
32
+ },
33
+ "metadata": {},
34
+ "output_type": "display_data"
35
+ },
36
+ {
37
+ "data": {
38
+ "application/vnd.jupyter.widget-view+json": {
39
+ "model_id": "58774a0b69624064b710db8d601fca0e",
40
+ "version_major": 2,
41
+ "version_minor": 0
42
+ },
43
+ "text/plain": [
44
+ "Downloading model.safetensors: 0%| | 0.00/179M [00:00<?, ?B/s]"
45
+ ]
46
+ },
47
+ "metadata": {},
48
+ "output_type": "display_data"
49
+ },
50
+ {
51
+ "name": "stderr",
52
+ "output_type": "stream",
53
+ "text": [
54
+ "Some weights of the model checkpoint at facebook/detr-resnet-101 were not used when initializing DetrForObjectDetection: ['model.backbone.conv_encoder.model.layer1.0.downsample.1.num_batches_tracked', 'model.backbone.conv_encoder.model.layer3.0.downsample.1.num_batches_tracked', 'model.backbone.conv_encoder.model.layer2.0.downsample.1.num_batches_tracked', 'model.backbone.conv_encoder.model.layer4.0.downsample.1.num_batches_tracked']\n",
55
+ "- This IS expected if you are initializing DetrForObjectDetection from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
56
+ "- This IS NOT expected if you are initializing DetrForObjectDetection from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
57
+ ]
58
+ },
59
+ {
60
+ "data": {
61
+ "application/vnd.jupyter.widget-view+json": {
62
+ "model_id": "b5e6dfa6bff2482faefa2663727a576e",
63
+ "version_major": 2,
64
+ "version_minor": 0
65
+ },
66
+ "text/plain": [
67
+ "Downloading (…)rocessor_config.json: 0%| | 0.00/274 [00:00<?, ?B/s]"
68
+ ]
69
+ },
70
+ "metadata": {},
71
+ "output_type": "display_data"
72
+ },
73
+ {
74
+ "name": "stderr",
75
+ "output_type": "stream",
76
+ "text": [
77
+ "Could not find image processor class in the image processor config or the model config. Loading based on pattern matching with the model's feature extractor configuration.\n",
78
+ "The `max_size` parameter is deprecated and will be removed in v4.26. Please specify in `size['longest_edge'] instead`.\n"
79
+ ]
80
+ }
81
+ ],
82
+ "source": [
83
+ "# Use a pipeline as a high-level helper\n",
84
+ "from transformers import pipeline\n",
85
+ "\n",
86
+ "pipe = pipeline(\"object-detection\", model=\"facebook/detr-resnet-101\")"
87
+ ]
88
+ },
89
+ {
90
+ "cell_type": "code",
91
+ "execution_count": 3,
92
+ "metadata": {},
93
+ "outputs": [
94
+ {
95
+ "data": {
96
+ "text/plain": [
97
+ "[{'score': 0.9799804091453552,\n",
98
+ " 'label': 'cow',\n",
99
+ " 'box': {'xmin': 601, 'ymin': 366, 'xmax': 638, 'ymax': 429}},\n",
100
+ " {'score': 0.9278073906898499,\n",
101
+ " 'label': 'cow',\n",
102
+ " 'box': {'xmin': 51, 'ymin': 266, 'xmax': 123, 'ymax': 319}},\n",
103
+ " {'score': 0.9865541458129883,\n",
104
+ " 'label': 'cow',\n",
105
+ " 'box': {'xmin': 440, 'ymin': 328, 'xmax': 499, 'ymax': 391}},\n",
106
+ " {'score': 0.9414395093917847,\n",
107
+ " 'label': 'cow',\n",
108
+ " 'box': {'xmin': 291, 'ymin': 337, 'xmax': 373, 'ymax': 402}},\n",
109
+ " {'score': 0.995155930519104,\n",
110
+ " 'label': 'cow',\n",
111
+ " 'box': {'xmin': 711, 'ymin': 523, 'xmax': 847, 'ymax': 603}},\n",
112
+ " {'score': 0.9969741106033325,\n",
113
+ " 'label': 'cow',\n",
114
+ " 'box': {'xmin': 1042, 'ymin': 588, 'xmax': 1221, 'ymax': 705}},\n",
115
+ " {'score': 0.9744983911514282,\n",
116
+ " 'label': 'cow',\n",
117
+ " 'box': {'xmin': 1474, 'ymin': 408, 'xmax': 1598, 'ymax': 483}},\n",
118
+ " {'score': 0.9618602991104126,\n",
119
+ " 'label': 'cow',\n",
120
+ " 'box': {'xmin': 584, 'ymin': 684, 'xmax': 779, 'ymax': 810}},\n",
121
+ " {'score': 0.9941285848617554,\n",
122
+ " 'label': 'cow',\n",
123
+ " 'box': {'xmin': 1125, 'ymin': 486, 'xmax': 1249, 'ymax': 579}},\n",
124
+ " {'score': 0.9376370906829834,\n",
125
+ " 'label': 'cow',\n",
126
+ " 'box': {'xmin': 1103, 'ymin': 298, 'xmax': 1172, 'ymax': 341}},\n",
127
+ " {'score': 0.9970544576644897,\n",
128
+ " 'label': 'cow',\n",
129
+ " 'box': {'xmin': 1025, 'ymin': 668, 'xmax': 1211, 'ymax': 805}},\n",
130
+ " {'score': 0.9351339340209961,\n",
131
+ " 'label': 'cow',\n",
132
+ " 'box': {'xmin': 228, 'ymin': 338, 'xmax': 297, 'ymax': 401}},\n",
133
+ " {'score': 0.9771629571914673,\n",
134
+ " 'label': 'cow',\n",
135
+ " 'box': {'xmin': 1063, 'ymin': 361, 'xmax': 1110, 'ymax': 422}},\n",
136
+ " {'score': 0.9911984801292419,\n",
137
+ " 'label': 'cow',\n",
138
+ " 'box': {'xmin': 712, 'ymin': 429, 'xmax': 764, 'ymax': 505}},\n",
139
+ " {'score': 0.9905621409416199,\n",
140
+ " 'label': 'cow',\n",
141
+ " 'box': {'xmin': 1073, 'ymin': 446, 'xmax': 1171, 'ymax': 524}},\n",
142
+ " {'score': 0.9994051456451416,\n",
143
+ " 'label': 'cow',\n",
144
+ " 'box': {'xmin': 594, 'ymin': 593, 'xmax': 1017, 'ymax': 814}},\n",
145
+ " {'score': 0.9972768425941467,\n",
146
+ " 'label': 'cow',\n",
147
+ " 'box': {'xmin': 1399, 'ymin': 593, 'xmax': 1655, 'ymax': 753}},\n",
148
+ " {'score': 0.993872880935669,\n",
149
+ " 'label': 'cow',\n",
150
+ " 'box': {'xmin': 4, 'ymin': 711, 'xmax': 225, 'ymax': 815}},\n",
151
+ " {'score': 0.9839267134666443,\n",
152
+ " 'label': 'cow',\n",
153
+ " 'box': {'xmin': 844, 'ymin': 343, 'xmax': 918, 'ymax': 395}},\n",
154
+ " {'score': 0.977581799030304,\n",
155
+ " 'label': 'cow',\n",
156
+ " 'box': {'xmin': 1179, 'ymin': 367, 'xmax': 1240, 'ymax': 425}},\n",
157
+ " {'score': 0.9804152250289917,\n",
158
+ " 'label': 'cow',\n",
159
+ " 'box': {'xmin': 610, 'ymin': 322, 'xmax': 672, 'ymax': 384}}]"
160
+ ]
161
+ },
162
+ "execution_count": 3,
163
+ "metadata": {},
164
+ "output_type": "execute_result"
165
+ }
166
+ ],
167
+ "source": [
168
+ "pipe('./cow1.jpg')"
169
+ ]
170
+ },
171
+ {
172
+ "cell_type": "code",
173
+ "execution_count": 4,
174
+ "metadata": {},
175
+ "outputs": [],
176
+ "source": [
177
+ "results = pipe('./cow1.jpg')"
178
+ ]
179
+ },
180
+ {
181
+ "cell_type": "code",
182
+ "execution_count": 5,
183
+ "metadata": {},
184
+ "outputs": [
185
+ {
186
+ "data": {
187
+ "text/plain": [
188
+ "{'score': 0.9799804091453552,\n",
189
+ " 'label': 'cow',\n",
190
+ " 'box': {'xmin': 601, 'ymin': 366, 'xmax': 638, 'ymax': 429}}"
191
+ ]
192
+ },
193
+ "execution_count": 5,
194
+ "metadata": {},
195
+ "output_type": "execute_result"
196
+ }
197
+ ],
198
+ "source": [
199
+ "results[0]"
200
+ ]
201
+ },
202
+ {
203
+ "cell_type": "code",
204
+ "execution_count": 6,
205
+ "metadata": {},
206
+ "outputs": [
207
+ {
208
+ "name": "stdout",
209
+ "output_type": "stream",
210
+ "text": [
211
+ "Total cows: 21\n"
212
+ ]
213
+ }
214
+ ],
215
+ "source": [
216
+ "# Add together all of the results to get the total number of cows\n",
217
+ "total_cows = 0\n",
218
+ "for result in results:\n",
219
+ " if result[\"label\"] == \"cow\":\n",
220
+ " total_cows += 1\n",
221
+ "\n",
222
+ "print(f\"Total cows: {total_cows}\")"
223
+ ]
224
+ },
225
+ {
226
+ "cell_type": "code",
227
+ "execution_count": null,
228
+ "metadata": {},
229
+ "outputs": [],
230
+ "source": []
231
+ }
232
+ ],
233
+ "metadata": {
234
+ "kernelspec": {
235
+ "display_name": "py310",
236
+ "language": "python",
237
+ "name": "python3"
238
+ },
239
+ "language_info": {
240
+ "codemirror_mode": {
241
+ "name": "ipython",
242
+ "version": 3
243
+ },
244
+ "file_extension": ".py",
245
+ "mimetype": "text/x-python",
246
+ "name": "python",
247
+ "nbconvert_exporter": "python",
248
+ "pygments_lexer": "ipython3",
249
+ "version": "3.10.12"
250
+ },
251
+ "orig_nbformat": 4
252
+ },
253
+ "nbformat": 4,
254
+ "nbformat_minor": 2
255
+ }