File size: 1,476 Bytes
b291ae2
 
 
 
 
e6b6213
 
b291ae2
 
 
 
 
e6b6213
b291ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import cv2
import torch
from ultralytics import YOLO
import gradio as gr

# Load the pre-trained YOLOv8 model
model = YOLO("./data/best.pt")  # Replace with path to your trained YOLOv8 model

# Function to process video frames and count wine bottles
def process_frame(frame):
    # Perform inference on the frame
    results = model(frame)

    # Extract results
    detections = results.pandas().xywh[results.pandas().xywh['class'] == 0]  # Assuming '0' is the class for wine bottles
    
    # Count the number of wine bottles detected
    bottle_count = len(detections)
    return bottle_count

# Classify stock based on bottle count
def classify_stock(bottle_count):
    if bottle_count > 50:
        return "Full"
    elif 20 <= bottle_count <= 50:
        return "Medium"
    else:
        return "Low"

# Video processing function to classify each frame and track stock level
def classify_video(video):
    cap = cv2.VideoCapture(video.name)
    stock_status = None

    while True:
        ret, frame = cap.read()
        if not ret:
            break
        
        bottle_count = process_frame(frame)
        stock_status = classify_stock(bottle_count)
    
    cap.release()
    return stock_status

# Gradio interface to upload a video and classify stock
def main(video_input):
    return classify_video(video_input)

# Creating the Gradio interface
iface = gr.Interface(fn=main, inputs=gr.Video(), outputs="text")

if __name__ == "__main__":
    iface.launch()