File size: 2,108 Bytes
2387c38
 
 
 
 
 
 
315cda9
912906e
44aa716
2387c38
 
 
 
 
 
 
 
 
 
 
df6c7a9
1e8df9a
660b68d
44aa716
 
2387c38
df6c7a9
1e8df9a
44aa716
912906e
df6c7a9
1e8df9a
44aa716
2387c38
 
912906e
2387c38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d0b6a9
2387c38
 
 
912906e
2387c38
912906e
2387c38
 
 
1e8df9a
 
df6c7a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from transformers import AutoFeatureExtractor, AutoModelForObjectDetection
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from random import choice
from PIL import Image
import os
from matplotlib import rcParams, font_manager
import streamlit as st
import urllib.request
import requests

extractor = AutoFeatureExtractor.from_pretrained("facebook/detr-resnet-50")

model = AutoModelForObjectDetection.from_pretrained("facebook/detr-resnet-50")

from transformers import pipeline

pipe = pipeline('object-detection', model=model, feature_extractor=extractor)

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')

st.caption('Downloading Image...')

img_data = requests.get(img_url).content
with open('detect.jpg', 'wb') as handler:
    handler.write(img_data)

st.caption('Running Detection...')

output = pipe(img_url)

st.caption('Adding Predictions to Image...')

fpath = "Poppins-SemiBold.ttf"
prop = font_manager.FontProperties(fname=fpath)

img = Image.open('detect.jpg')
plt.figure(dpi=2400)

# Create figure and axes
fig, ax = plt.subplots()

# Display the image
ax.imshow(img)

colors = ["#ef4444", "#f97316", "#eab308", "#84cc16", "#06b6d4", "#6366f1"]

# Create a Rectangle patch
for prediction in output:
    selected_color = choice(colors)
    x, y, w, h = prediction['box']['xmin'], prediction['box']['ymin'], prediction['box']['xmax'] - prediction['box']['xmin'], prediction['box']['ymax'] - prediction['box']['ymin']
    rect = patches.FancyBboxPatch((x, y), w, h, linewidth=1.25, edgecolor=selected_color, facecolor='none', boxstyle="round,pad=-0.0040,rounding_size=10",)
    ax.add_patch(rect)
    plt.text(x, y-25, f"{prediction['label']}: {round(prediction['score']*100, 1)}%", fontsize=5, color=selected_color, fontproperties=prop)

plt.axis('off')

plt.savefig('detect-bbox.jpg', dpi=1200, bbox_inches='tight')

image = Image.open('detect-bbox.jpg')

st.image(image, caption='DETR Image')

plt.show()

st.caption('Done!')