syedfaisalabrar commited on
Commit
e539001
·
verified ·
1 Parent(s): 1a1af4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -14
app.py CHANGED
@@ -8,33 +8,32 @@ from ultralytics import YOLO
8
  # Load YOLO model
9
  model = YOLO("best.pt").to("cpu")
10
 
11
- def detect_and_crop(image):
12
- """Detect objects using YOLO and crop them from the image."""
13
  image_np = np.array(image)
 
 
14
  results = model(image_np, conf=0.85, device='cpu')
15
 
16
- cropped_images = {}
17
  for result in results:
18
  for box in result.boxes:
19
  x1, y1, x2, y2 = map(int, box.xyxy[0])
20
  class_name = model.names[int(box.cls[0])]
21
- cropped = image_np[y1:y2, x1:x2]
22
- cropped_images[class_name] = Image.fromarray(cropped)
 
 
 
23
 
24
- return cropped_images
25
-
26
- def predict(image):
27
- """Process image: detect objects and crop them."""
28
- cropped_images = detect_and_crop(image)
29
- return list(cropped_images.values()) if cropped_images else image
30
 
31
  # Gradio interface
32
  iface = gr.Interface(
33
  fn=predict,
34
  inputs="image",
35
- # outputs="image",
36
- outputs=gr.Gallery(),
37
- title="License Field Detection & Cropping"
38
  )
39
 
40
  iface.launch()
 
8
  # Load YOLO model
9
  model = YOLO("best.pt").to("cpu")
10
 
11
+ def predict(image):
12
+ """Detect objects using YOLO and draw bounding boxes on the image."""
13
  image_np = np.array(image)
14
+
15
+ # Perform inference
16
  results = model(image_np, conf=0.85, device='cpu')
17
 
18
+ # Draw bounding boxes
19
  for result in results:
20
  for box in result.boxes:
21
  x1, y1, x2, y2 = map(int, box.xyxy[0])
22
  class_name = model.names[int(box.cls[0])]
23
+
24
+ # Draw rectangle and label
25
+ cv2.rectangle(image_np, (x1, y1), (x2, y2), (0, 255, 0), 2)
26
+ cv2.putText(image_np, class_name, (x1, y1 - 10),
27
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
28
 
29
+ return Image.fromarray(image_np)
 
 
 
 
 
30
 
31
  # Gradio interface
32
  iface = gr.Interface(
33
  fn=predict,
34
  inputs="image",
35
+ outputs="image",
36
+ title="YOLO Object Detection"
 
37
  )
38
 
39
  iface.launch()