Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import tempfile
|
4 |
+
import os
|
5 |
+
import numpy as np
|
6 |
+
from ultralytics import YOLO
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
def process_video(video_path, model):
|
10 |
+
cap = cv2.VideoCapture(video_path)
|
11 |
+
temp_output = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
12 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
13 |
+
out = cv2.VideoWriter(temp_output.name, fourcc, cap.get(cv2.CAP_PROP_FPS),
|
14 |
+
(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))
|
15 |
+
|
16 |
+
while cap.isOpened():
|
17 |
+
ret, frame = cap.read()
|
18 |
+
if not ret:
|
19 |
+
break
|
20 |
+
|
21 |
+
results = model(frame)
|
22 |
+
for result in results:
|
23 |
+
for box in result.boxes:
|
24 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
25 |
+
label = result.names[int(box.cls[0])]
|
26 |
+
conf = float(box.conf[0])
|
27 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
28 |
+
cv2.putText(frame, f'{label}: {conf:.2f}', (x1, y1 - 10),
|
29 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
30 |
+
|
31 |
+
out.write(frame)
|
32 |
+
|
33 |
+
cap.release()
|
34 |
+
out.release()
|
35 |
+
return temp_output.name
|
36 |
+
|
37 |
+
def main():
|
38 |
+
st.set_page_config(page_title="Bottle Label Checker", page_icon="🍾")
|
39 |
+
st.title("Bottle Label Checking System using YOLO & Gemini")
|
40 |
+
|
41 |
+
uploaded_video = st.file_uploader("Upload a video", type=["mp4", "avi", "mov", "mkv"])
|
42 |
+
|
43 |
+
if uploaded_video is not None:
|
44 |
+
temp_video_path = os.path.join(tempfile.gettempdir(), uploaded_video.name)
|
45 |
+
with open(temp_video_path, "wb") as f:
|
46 |
+
f.write(uploaded_video.read())
|
47 |
+
|
48 |
+
st.video(temp_video_path)
|
49 |
+
|
50 |
+
model = YOLO("yolov8n.pt") # Load YOLO model
|
51 |
+
|
52 |
+
if st.button("Process Video"):
|
53 |
+
st.write("Processing video... This may take some time.")
|
54 |
+
output_path = process_video(temp_video_path, model)
|
55 |
+
|
56 |
+
st.video(output_path)
|
57 |
+
st.success("Processing complete!")
|
58 |
+
|
59 |
+
with open(output_path, "rb") as file:
|
60 |
+
st.download_button("Download Processed Video", file, file_name="processed_video.mp4", mime="video/mp4")
|
61 |
+
|
62 |
+
if __name__ == "__main__":
|
63 |
+
main()
|