Update realtime_detect.py
Browse files- realtime_detect.py +74 -74
realtime_detect.py
CHANGED
|
@@ -1,74 +1,74 @@
|
|
| 1 |
-
import tkinter as tk
|
| 2 |
-
from tkinter import Label, Button
|
| 3 |
-
import cv2
|
| 4 |
-
from PIL import Image, ImageTk
|
| 5 |
-
import threading
|
| 6 |
-
from ultralytics import YOLO
|
| 7 |
-
|
| 8 |
-
class WasteDetectorApp:
|
| 9 |
-
def __init__(self, window):
|
| 10 |
-
self.window = window
|
| 11 |
-
self.window.title("Waste Detection Camera App")
|
| 12 |
-
|
| 13 |
-
self.model = YOLO("best.pt")
|
| 14 |
-
self.cap = None
|
| 15 |
-
self.running = False
|
| 16 |
-
self.writer = None
|
| 17 |
-
|
| 18 |
-
self.label = Label(window)
|
| 19 |
-
self.label.pack()
|
| 20 |
-
|
| 21 |
-
self.start_button = Button(window, text="Start Detection & Save", command=self.start_detection)
|
| 22 |
-
self.start_button.pack(pady=5)
|
| 23 |
-
|
| 24 |
-
self.stop_button = Button(window, text="Stop", command=self.stop_detection)
|
| 25 |
-
self.stop_button.pack(pady=5)
|
| 26 |
-
|
| 27 |
-
def start_detection(self):
|
| 28 |
-
if not self.running:
|
| 29 |
-
self.cap = cv2.VideoCapture(0)
|
| 30 |
-
self.running = True
|
| 31 |
-
|
| 32 |
-
# Set up video writer
|
| 33 |
-
fourcc = cv2.VideoWriter_fourcc(*'XVID')
|
| 34 |
-
fps = 20.0
|
| 35 |
-
width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 36 |
-
height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 37 |
-
self.writer = cv2.VideoWriter('output.avi', fourcc, fps, (width, height))
|
| 38 |
-
|
| 39 |
-
threading.Thread(target=self.detect_loop, daemon=True).start()
|
| 40 |
-
|
| 41 |
-
def stop_detection(self):
|
| 42 |
-
self.running = False
|
| 43 |
-
if self.cap:
|
| 44 |
-
self.cap.release()
|
| 45 |
-
if self.writer:
|
| 46 |
-
self.writer.release()
|
| 47 |
-
self.label.config(image='')
|
| 48 |
-
|
| 49 |
-
def detect_loop(self):
|
| 50 |
-
while self.running:
|
| 51 |
-
ret, frame = self.cap.read()
|
| 52 |
-
if not ret:
|
| 53 |
-
break
|
| 54 |
-
|
| 55 |
-
results = self.model.predict(frame, verbose=False)
|
| 56 |
-
annotated_frame = results[0].plot()
|
| 57 |
-
|
| 58 |
-
# Write to video file
|
| 59 |
-
self.writer.write(annotated_frame)
|
| 60 |
-
|
| 61 |
-
# Display to GUI
|
| 62 |
-
img_rgb = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)
|
| 63 |
-
img_pil = Image.fromarray(img_rgb)
|
| 64 |
-
imgtk = ImageTk.PhotoImage(image=img_pil)
|
| 65 |
-
self.label.imgtk = imgtk
|
| 66 |
-
self.label.configure(image=imgtk)
|
| 67 |
-
|
| 68 |
-
self.cap.release()
|
| 69 |
-
self.writer.release()
|
| 70 |
-
|
| 71 |
-
if __name__ == "__main__":
|
| 72 |
-
root = tk.Tk()
|
| 73 |
-
app = WasteDetectorApp(root)
|
| 74 |
-
root.mainloop()
|
|
|
|
| 1 |
+
import tkinter as tk
|
| 2 |
+
from tkinter import Label, Button
|
| 3 |
+
import cv2
|
| 4 |
+
from PIL import Image, ImageTk
|
| 5 |
+
import threading
|
| 6 |
+
from ultralytics import YOLO
|
| 7 |
+
|
| 8 |
+
class WasteDetectorApp:
|
| 9 |
+
def __init__(self, window):
|
| 10 |
+
self.window = window
|
| 11 |
+
self.window.title("Waste Detection Camera App")
|
| 12 |
+
|
| 13 |
+
self.model = YOLO("best.pt")
|
| 14 |
+
self.cap = None
|
| 15 |
+
self.running = False
|
| 16 |
+
self.writer = None
|
| 17 |
+
|
| 18 |
+
self.label = Label(window)
|
| 19 |
+
self.label.pack()
|
| 20 |
+
|
| 21 |
+
self.start_button = Button(window, text="Start Detection & Save", command=self.start_detection)
|
| 22 |
+
self.start_button.pack(pady=5)
|
| 23 |
+
|
| 24 |
+
self.stop_button = Button(window, text="Stop", command=self.stop_detection)
|
| 25 |
+
self.stop_button.pack(pady=5)
|
| 26 |
+
|
| 27 |
+
def start_detection(self):
|
| 28 |
+
if not self.running:
|
| 29 |
+
self.cap = cv2.VideoCapture(0)
|
| 30 |
+
self.running = True
|
| 31 |
+
|
| 32 |
+
# Set up video writer
|
| 33 |
+
fourcc = cv2.VideoWriter_fourcc(*'XVID')
|
| 34 |
+
fps = 20.0
|
| 35 |
+
width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 36 |
+
height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 37 |
+
self.writer = cv2.VideoWriter('output.avi', fourcc, fps, (width, height))
|
| 38 |
+
|
| 39 |
+
threading.Thread(target=self.detect_loop, daemon=True).start()
|
| 40 |
+
|
| 41 |
+
def stop_detection(self):
|
| 42 |
+
self.running = False
|
| 43 |
+
if self.cap:
|
| 44 |
+
self.cap.release()
|
| 45 |
+
if self.writer:
|
| 46 |
+
self.writer.release()
|
| 47 |
+
self.label.config(image='')
|
| 48 |
+
|
| 49 |
+
def detect_loop(self):
|
| 50 |
+
while self.running:
|
| 51 |
+
ret, frame = self.cap.read()
|
| 52 |
+
if not ret:
|
| 53 |
+
break
|
| 54 |
+
|
| 55 |
+
results = self.model.predict(frame, verbose=False)
|
| 56 |
+
annotated_frame = results[0].plot()
|
| 57 |
+
|
| 58 |
+
# Write to video file
|
| 59 |
+
self.writer.write(annotated_frame)
|
| 60 |
+
|
| 61 |
+
# Display to GUI
|
| 62 |
+
img_rgb = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)
|
| 63 |
+
img_pil = Image.fromarray(img_rgb)
|
| 64 |
+
imgtk = ImageTk.PhotoImage(image=img_pil)
|
| 65 |
+
self.label.imgtk = imgtk
|
| 66 |
+
self.label.configure(image=imgtk)
|
| 67 |
+
|
| 68 |
+
self.cap.release()
|
| 69 |
+
self.writer.release()
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
root = tk.Tk()
|
| 73 |
+
app = WasteDetectorApp(root)
|
| 74 |
+
root.mainloop()
|