Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,66 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
import torch
|
| 7 |
from PIL import Image
|
| 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 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
def main():
|
| 70 |
-
option = st.selectbox("Which model should we use?", ("facebook/detr-resnet-50", "facebook/detr-resnet-101"))
|
| 71 |
-
feature_extractor, model = get_hf_components(option)
|
| 72 |
-
url = st.text_input("URL to some image", "http://images.cocodataset.org/val2017/000000039769.jpg")
|
| 73 |
-
img = get_img_from_url(url)
|
| 74 |
-
processed_outputs = make_prediction(img, feature_extractor, model)
|
| 75 |
-
threshold = st.slider("Prediction Threshold", 0.0, 1.0, 0.7)
|
| 76 |
-
viz_img = visualize_prediction(img, processed_outputs, threshold, model.config.id2label)
|
| 77 |
-
st.image(viz_img)
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
if __name__ == "__main__":
|
| 81 |
-
main()
|
|
|
|
| 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 |
+
import streamlit as st
|
| 9 |
+
import urllib.request
|
| 10 |
+
import requests
|
| 11 |
+
|
| 12 |
+
extractor = AutoFeatureExtractor.from_pretrained("facebook/detr-resnet-101")
|
| 13 |
+
|
| 14 |
+
model = AutoModelForObjectDetection.from_pretrained("facebook/detr-resnet-101")
|
| 15 |
+
|
| 16 |
+
from transformers import pipeline
|
| 17 |
+
|
| 18 |
+
pipe = pipeline('object-detection', model=model, feature_extractor=extractor)
|
| 19 |
+
|
| 20 |
+
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')
|
| 21 |
+
|
| 22 |
+
st.caption('Downloading Image...')
|
| 23 |
+
|
| 24 |
+
img_data = requests.get(img_url).content
|
| 25 |
+
with open('detect.jpg', 'wb') as handler:
|
| 26 |
+
handler.write(img_data)
|
| 27 |
+
|
| 28 |
+
st.caption('Running Detection...')
|
| 29 |
+
|
| 30 |
+
output = pipe(img_url)
|
| 31 |
+
|
| 32 |
+
st.caption('Adding Predictions to Image...')
|
| 33 |
+
|
| 34 |
+
fpath = "Poppins-SemiBold.ttf"
|
| 35 |
+
prop = font_manager.FontProperties(fname=fpath)
|
| 36 |
+
|
| 37 |
+
img = Image.open('detect.jpg')
|
| 38 |
+
plt.figure(dpi=2400)
|
| 39 |
+
|
| 40 |
+
# Create figure and axes
|
| 41 |
+
fig, ax = plt.subplots()
|
| 42 |
+
|
| 43 |
+
# Display the image
|
| 44 |
+
ax.imshow(img)
|
| 45 |
+
|
| 46 |
+
colors = ["#ef4444", "#f97316", "#eab308", "#84cc16", "#06b6d4", "#6366f1"]
|
| 47 |
+
|
| 48 |
+
# Create a Rectangle patch
|
| 49 |
+
for prediction in output:
|
| 50 |
+
selected_color = choice(colors)
|
| 51 |
+
x, y, w, h = prediction['box']['xmin'], prediction['box']['ymin'], prediction['box']['xmax'] - prediction['box']['xmin'], prediction['box']['ymax'] - prediction['box']['ymin']
|
| 52 |
+
rect = patches.FancyBboxPatch((x, y), w, h, linewidth=1.25, edgecolor=selected_color, facecolor='none', boxstyle="round,pad=-0.0040,rounding_size=10",)
|
| 53 |
+
ax.add_patch(rect)
|
| 54 |
+
plt.text(x, y-25, f"{prediction['label']}: {round(prediction['score']*100, 1)}%", fontsize=5, color=selected_color, fontproperties=prop)
|
| 55 |
+
|
| 56 |
+
plt.axis('off')
|
| 57 |
+
|
| 58 |
+
plt.savefig('detect-bbox.jpg', dpi=1200, bbox_inches='tight')
|
| 59 |
+
|
| 60 |
+
image = Image.open('detect-bbox.jpg')
|
| 61 |
+
|
| 62 |
+
st.image(image, caption='DETR Image')
|
| 63 |
+
|
| 64 |
+
plt.show()
|
| 65 |
+
|
| 66 |
+
st.caption('Done!')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|