syedfaisalabrar commited on
Commit
b4a7ece
·
verified ·
1 Parent(s): f65a1e2

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import cv2
4
+ import numpy as np
5
+ from PIL import Image
6
+ from ultralytics import YOLO
7
+
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
+ title="License Field Detection & Cropping"
37
+ )
38
+
39
+ iface.launch()