Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,10 +19,6 @@ The application will display the image with bounding boxes around the objects.
|
|
| 19 |
st.markdown("### Step 1: Upload an Image for Object Detection")
|
| 20 |
uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
|
| 21 |
|
| 22 |
-
# Load class labels (COCO dataset)
|
| 23 |
-
with open("coco.names", "r") as f:
|
| 24 |
-
class_names = [line.strip() for line in f.readlines()]
|
| 25 |
-
|
| 26 |
# Function to detect objects using OpenCV's YOLO
|
| 27 |
def detect_objects_with_yolo(image):
|
| 28 |
# Load YOLO model configuration and weights
|
|
@@ -32,7 +28,7 @@ def detect_objects_with_yolo(image):
|
|
| 32 |
|
| 33 |
# Get the names of the output layers
|
| 34 |
layer_names = net.getLayerNames()
|
| 35 |
-
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()
|
| 36 |
|
| 37 |
# Prepare the image for YOLO
|
| 38 |
blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
|
|
@@ -41,8 +37,8 @@ def detect_objects_with_yolo(image):
|
|
| 41 |
|
| 42 |
# Process YOLO outputs
|
| 43 |
boxes = []
|
|
|
|
| 44 |
confidences = []
|
| 45 |
-
class_ids = []
|
| 46 |
|
| 47 |
for out in outs:
|
| 48 |
for detection in out:
|
|
@@ -58,21 +54,9 @@ def detect_objects_with_yolo(image):
|
|
| 58 |
y = int(center_y - h / 2)
|
| 59 |
boxes.append([x, y, w, h])
|
| 60 |
confidences.append(float(confidence))
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
# Apply Non-Maximum Suppression (NMS) to remove duplicate boxes
|
| 64 |
-
indices = cv2.dnn.NMSBoxes(boxes, confidences, score_threshold=0.5, nms_threshold=0.4)
|
| 65 |
-
final_boxes = []
|
| 66 |
-
final_labels = []
|
| 67 |
-
final_confidences = []
|
| 68 |
|
| 69 |
-
|
| 70 |
-
for i in indices.flatten():
|
| 71 |
-
final_boxes.append(boxes[i])
|
| 72 |
-
final_labels.append(class_ids[i])
|
| 73 |
-
final_confidences.append(confidences[i])
|
| 74 |
-
|
| 75 |
-
return final_boxes, final_labels, final_confidences
|
| 76 |
|
| 77 |
# Display the uploaded image and detect objects
|
| 78 |
if uploaded_file is not None:
|
|
@@ -86,7 +70,7 @@ if uploaded_file is not None:
|
|
| 86 |
# Draw bounding boxes on the image
|
| 87 |
for i, box in enumerate(boxes):
|
| 88 |
x, y, w, h = box
|
| 89 |
-
label =
|
| 90 |
confidence = confidences[i]
|
| 91 |
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 92 |
cv2.putText(image, f"{label} {confidence:.2f}", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
|
@@ -97,11 +81,11 @@ if uploaded_file is not None:
|
|
| 97 |
|
| 98 |
# Step 3: Display the count of detected objects dynamically
|
| 99 |
st.markdown("### Step 3: Detected Objects Count")
|
| 100 |
-
label_counts = Counter(
|
| 101 |
|
| 102 |
# Display counts in a well-formatted table
|
| 103 |
for obj, count in label_counts.items():
|
| 104 |
-
st.markdown(f"**{obj}**: {count}")
|
| 105 |
|
| 106 |
st.markdown("""
|
| 107 |
---
|
|
@@ -115,4 +99,4 @@ if uploaded_file is not None:
|
|
| 115 |
st.markdown("""
|
| 116 |
---
|
| 117 |
Made with ❤️ by [SenasuDemir](https://github.com/SenasuDemir).
|
| 118 |
-
""")
|
|
|
|
| 19 |
st.markdown("### Step 1: Upload an Image for Object Detection")
|
| 20 |
uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Function to detect objects using OpenCV's YOLO
|
| 23 |
def detect_objects_with_yolo(image):
|
| 24 |
# Load YOLO model configuration and weights
|
|
|
|
| 28 |
|
| 29 |
# Get the names of the output layers
|
| 30 |
layer_names = net.getLayerNames()
|
| 31 |
+
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
|
| 32 |
|
| 33 |
# Prepare the image for YOLO
|
| 34 |
blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
|
|
|
|
| 37 |
|
| 38 |
# Process YOLO outputs
|
| 39 |
boxes = []
|
| 40 |
+
labels = []
|
| 41 |
confidences = []
|
|
|
|
| 42 |
|
| 43 |
for out in outs:
|
| 44 |
for detection in out:
|
|
|
|
| 54 |
y = int(center_y - h / 2)
|
| 55 |
boxes.append([x, y, w, h])
|
| 56 |
confidences.append(float(confidence))
|
| 57 |
+
labels.append(class_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
return boxes, labels, confidences
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# Display the uploaded image and detect objects
|
| 62 |
if uploaded_file is not None:
|
|
|
|
| 70 |
# Draw bounding boxes on the image
|
| 71 |
for i, box in enumerate(boxes):
|
| 72 |
x, y, w, h = box
|
| 73 |
+
label = labels[i]
|
| 74 |
confidence = confidences[i]
|
| 75 |
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 76 |
cv2.putText(image, f"{label} {confidence:.2f}", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
|
|
|
| 81 |
|
| 82 |
# Step 3: Display the count of detected objects dynamically
|
| 83 |
st.markdown("### Step 3: Detected Objects Count")
|
| 84 |
+
label_counts = Counter(labels)
|
| 85 |
|
| 86 |
# Display counts in a well-formatted table
|
| 87 |
for obj, count in label_counts.items():
|
| 88 |
+
st.markdown(f"**Object ID {obj}**: {count}")
|
| 89 |
|
| 90 |
st.markdown("""
|
| 91 |
---
|
|
|
|
| 99 |
st.markdown("""
|
| 100 |
---
|
| 101 |
Made with ❤️ by [SenasuDemir](https://github.com/SenasuDemir).
|
| 102 |
+
""")
|