21501A0580 commited on
Commit
338e2a9
·
verified ·
1 Parent(s): 1aba411

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -0
app.py CHANGED
@@ -9,6 +9,8 @@ from datetime import datetime, timedelta
9
  import re
10
  from ultralytics import YOLO
11
  import pandas as pd
 
 
12
 
13
  # Initialize models
14
  ocr = PaddleOCR(lang='en')
@@ -239,12 +241,25 @@ if uploaded_file is not None:
239
  results = object_model(image_path)
240
  detected_objects = []
241
 
 
 
 
242
  for result in results:
243
  boxes = result.boxes.data.cpu().numpy()
244
  for box in boxes:
245
  class_id = int(box[5])
246
  confidence = box[4] # Assuming the confidence score is at index 4
247
  detected_objects.append((result.names[class_id], confidence))
 
 
 
 
 
 
 
 
 
 
248
 
249
  # Count occurrences and average confidence of each object
250
  object_data = {}
 
9
  import re
10
  from ultralytics import YOLO
11
  import pandas as pd
12
+ import cv2
13
+ from PIL import Image
14
 
15
  # Initialize models
16
  ocr = PaddleOCR(lang='en')
 
241
  results = object_model(image_path)
242
  detected_objects = []
243
 
244
+ # Load the image using OpenCV
245
+ image = cv2.imread(image_path)
246
+
247
  for result in results:
248
  boxes = result.boxes.data.cpu().numpy()
249
  for box in boxes:
250
  class_id = int(box[5])
251
  confidence = box[4] # Assuming the confidence score is at index 4
252
  detected_objects.append((result.names[class_id], confidence))
253
+
254
+ # Draw bounding box and label on the image
255
+ x1, y1, x2, y2 = map(int, box[:4])
256
+ label = f"{result.names[class_id]} {confidence * 100:.2f}%"
257
+ cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
258
+ cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
259
+
260
+ # Convert the image back to RGB for display in Streamlit
261
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
262
+ st.image(image_rgb, caption='Detected Objects', use_container_width=True)
263
 
264
  # Count occurrences and average confidence of each object
265
  object_data = {}