Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
|
| 8 |
+
st.set_page_config(
|
| 9 |
+
page_title="Smart Vision Inspector",
|
| 10 |
+
page_icon="👁️",
|
| 11 |
+
layout="wide"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
st.title("👁️ Smart Vision Inspector")
|
| 15 |
+
|
| 16 |
+
st.markdown("""
|
| 17 |
+
Upload an image and perform:
|
| 18 |
+
|
| 19 |
+
- Object Detection
|
| 20 |
+
- Image Analysis
|
| 21 |
+
- Bounding Box Visualization
|
| 22 |
+
- AI Powered Insights
|
| 23 |
+
""")
|
| 24 |
+
|
| 25 |
+
@st.cache_resource
|
| 26 |
+
def load_detector():
|
| 27 |
+
|
| 28 |
+
detector = pipeline(
|
| 29 |
+
"object-detection",
|
| 30 |
+
model="facebook/detr-resnet-50"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
return detector
|
| 34 |
+
|
| 35 |
+
detector = load_detector()
|
| 36 |
+
|
| 37 |
+
uploaded_file = st.file_uploader(
|
| 38 |
+
"Upload Image",
|
| 39 |
+
type=["jpg", "jpeg", "png"]
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
if uploaded_file:
|
| 43 |
+
|
| 44 |
+
image = Image.open(uploaded_file)
|
| 45 |
+
|
| 46 |
+
st.image(
|
| 47 |
+
image,
|
| 48 |
+
caption="Uploaded Image",
|
| 49 |
+
use_container_width=True
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
image_np = np.array(image)
|
| 53 |
+
|
| 54 |
+
image_bgr = cv2.cvtColor(
|
| 55 |
+
image_np,
|
| 56 |
+
cv2.COLOR_RGB2BGR
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
results = detector(image)
|
| 60 |
+
|
| 61 |
+
object_counts = {}
|
| 62 |
+
|
| 63 |
+
for obj in results:
|
| 64 |
+
|
| 65 |
+
label = obj["label"]
|
| 66 |
+
|
| 67 |
+
object_counts[label] = (
|
| 68 |
+
object_counts.get(label, 0) + 1
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
box = obj["box"]
|
| 72 |
+
|
| 73 |
+
x1 = int(box["xmin"])
|
| 74 |
+
y1 = int(box["ymin"])
|
| 75 |
+
x2 = int(box["xmax"])
|
| 76 |
+
y2 = int(box["ymax"])
|
| 77 |
+
|
| 78 |
+
cv2.rectangle(
|
| 79 |
+
image_bgr,
|
| 80 |
+
(x1, y1),
|
| 81 |
+
(x2, y2),
|
| 82 |
+
(0, 255, 0),
|
| 83 |
+
2
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
cv2.putText(
|
| 87 |
+
image_bgr,
|
| 88 |
+
label,
|
| 89 |
+
(x1, y1 - 10),
|
| 90 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
| 91 |
+
0.6,
|
| 92 |
+
(0,255,0),
|
| 93 |
+
2
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
result_rgb = cv2.cvtColor(
|
| 97 |
+
image_bgr,
|
| 98 |
+
cv2.COLOR_BGR2RGB
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
st.subheader(
|
| 102 |
+
"Detection Results"
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
st.image(
|
| 106 |
+
result_rgb,
|
| 107 |
+
use_container_width=True
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
st.subheader(
|
| 111 |
+
"Detected Objects"
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
st.json(object_counts)
|
| 115 |
+
|
| 116 |
+
st.subheader(
|
| 117 |
+
"Summary"
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
summary = []
|
| 121 |
+
|
| 122 |
+
for k, v in object_counts.items():
|
| 123 |
+
|
| 124 |
+
summary.append(
|
| 125 |
+
f"{v} {k}"
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
st.success(
|
| 129 |
+
"Detected: " + ", ".join(summary)
|
| 130 |
+
)
|