nagasurendra commited on
Commit
a4bc6c5
·
verified ·
1 Parent(s): 681150b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -50
app.py CHANGED
@@ -1,54 +1,45 @@
1
- import cv2
2
- import torch
3
- from ultralytics import YOLO
4
  import gradio as gr
5
-
6
- # Load the pre-trained YOLOv8 model
7
- model = YOLO("./data/best.pt") # Replace with path to your trained YOLOv8 model
8
-
9
- # Function to process video frames and count wine bottles
10
- def process_frame(frame):
11
- # Perform inference on the frame
12
- results = model(frame)
13
-
14
- # Extract results
15
- detections = results.pandas().xywh[results.pandas().xywh['class'] == 0] # Assuming '0' is the class for wine bottles
16
-
17
- # Count the number of wine bottles detected
18
- bottle_count = len(detections)
19
- return bottle_count
20
-
21
- # Classify stock based on bottle count
22
- def classify_stock(bottle_count):
23
- if bottle_count > 50:
24
- return "Full"
25
- elif 20 <= bottle_count <= 50:
26
- return "Medium"
 
 
 
 
27
  else:
28
- return "Low"
29
-
30
- # Video processing function to classify each frame and track stock level
31
- def classify_video(video):
32
- cap = cv2.VideoCapture(video.name)
33
- stock_status = None
34
-
35
- while True:
36
- ret, frame = cap.read()
37
- if not ret:
38
- break
39
-
40
- bottle_count = process_frame(frame)
41
- stock_status = classify_stock(bottle_count)
42
-
43
- cap.release()
44
- return stock_status
45
-
46
- # Gradio interface to upload a video and classify stock
47
- def main(video_input):
48
- return classify_video(video_input)
49
-
50
- # Creating the Gradio interface
51
- iface = gr.Interface(fn=main, inputs=gr.Video(), outputs="text")
52
 
53
  if __name__ == "__main__":
54
- iface.launch()
 
 
 
 
1
  import gradio as gr
2
+ from ultralytics import YOLO
3
+ from PIL import Image
4
+
5
+ # -----------------------------
6
+ # Load YOLO model
7
+ # -----------------------------
8
+ model = YOLO("./data/best.pt") # make sure this path matches your folder structure
9
+
10
+ # -----------------------------
11
+ # Prediction function
12
+ # -----------------------------
13
+ def predict(image):
14
+ # Run prediction
15
+ results = model.predict(image, conf=0.5)
16
+
17
+ # Annotated image with bounding boxes
18
+ result_img = results[0].plot()
19
+
20
+ # Extract detected labels
21
+ detected_labels = results[0].boxes.cls.tolist()
22
+ names = results[0].names
23
+ detected_objects = [names[int(cls_id)] for cls_id in detected_labels]
24
+
25
+ # Text output
26
+ if detected_objects:
27
+ label_text = f"✅ Detected objects: {', '.join(detected_objects)}"
28
  else:
29
+ label_text = "❌ No objects detected."
30
+
31
+ return Image.fromarray(result_img), label_text
32
+
33
+ # -----------------------------
34
+ # Gradio Interface
35
+ # -----------------------------
36
+ demo = gr.Interface(
37
+ fn=predict,
38
+ inputs=gr.Image(type="pil"),
39
+ outputs=[gr.Image(type="pil", label="Detection Result"), gr.Textbox(label="Detected Objects")],
40
+ title="🥤 Bottle Detection with YOLOv11",
41
+ description="Upload an image to check if a **bottle** is detected using your trained YOLOv11 model."
42
+ )
 
 
 
 
 
 
 
 
 
 
43
 
44
  if __name__ == "__main__":
45
+ demo.launch()