ayaanzaveri commited on
Commit
2387c38
1 Parent(s): 319d01a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoFeatureExtractor, AutoModelForObjectDetection
2
+ import matplotlib.pyplot as plt
3
+ import matplotlib.patches as patches
4
+ from random import choice
5
+ from PIL import Image
6
+ import os
7
+ from matplotlib import rcParams, font_manager
8
+
9
+ extractor = AutoFeatureExtractor.from_pretrained("facebook/detr-resnet-50")
10
+
11
+ model = AutoModelForObjectDetection.from_pretrained("facebook/detr-resnet-50")
12
+
13
+ from transformers import pipeline
14
+
15
+ pipe = pipeline('object-detection', model=model, feature_extractor=extractor)
16
+
17
+ img_url = st.text_input('Image URL', 'https://images.unsplash.com/photo-1556911220-bff31c812dba?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2468&q=80')
18
+
19
+ output = pipe(img_url)
20
+
21
+ fpath = os.path.join(r"Poppins-SemiBold.ttf")
22
+ prop = font_manager.FontProperties(fname=fpath)
23
+
24
+ img = Image.open('kitchen.jpg')
25
+ plt.figure(dpi=2400)
26
+
27
+ # Create figure and axes
28
+ fig, ax = plt.subplots()
29
+
30
+ # Display the image
31
+ ax.imshow(img)
32
+
33
+ colors = ["#ef4444", "#f97316", "#eab308", "#84cc16", "#06b6d4", "#6366f1"]
34
+
35
+ # Create a Rectangle patch
36
+ for prediction in output:
37
+ selected_color = choice(colors)
38
+ x, y, w, h = prediction['box']['xmin'], prediction['box']['ymin'], prediction['box']['xmax'] - prediction['box']['xmin'], prediction['box']['ymax'] - prediction['box']['ymin']
39
+ rect = patches.FancyBboxPatch((x, y), w, h, linewidth=1.25, edgecolor=selected_color, facecolor='none', boxstyle="round,pad=-0.0040,rounding_size=10",)
40
+ ax.add_patch(rect)
41
+ plt.text(x, y-25, f"{prediction['label']}: {round(prediction['score']*100, 1)}%", fontsize=100, color=selected_color, fontproperties=prop)
42
+
43
+ plt.axis('off')
44
+
45
+ plt.savefig('kitchen-bbox.jpg', dpi=1200, bbox_inches='tight')
46
+
47
+ image = Image.open('kitchen-bbox.jpg')
48
+
49
+ st.image(image, caption='DETR Image')
50
+
51
+ plt.show()