-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython sortify_main.py
More file actions
64 lines (50 loc) · 1.7 KB
/
python sortify_main.py
File metadata and controls
64 lines (50 loc) · 1.7 KB
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
56
57
58
59
60
61
62
63
64
# Date: 03/09/2025
#Author: Rosey
#Aim: To use my custom dataset and my trained model to test and perform object detection
# A smart sorting bin which actually sorts out bins into various classes be it nylon,bottles or cans
# Data was collected: About 200 samples of plastic bottles, nylon and cans were snapped
#
#Job Status: COMPLETED!
#import necessary libraries
import cv2
import serial
import time
from ultralytics import YOLO
# Load model
model = YOLO("best.pt")
# Connect to Arduino
arduino = serial.Serial('COM10', 9600) # change COM10 to your correct port
time.sleep(2) # wait for Arduino reset
# Open webcam
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
# Run detection
results = model.predict(frame, conf=0.6, verbose=False)
for r in results:
for box in r.boxes:
cls = int(box.cls) # fixed indexing
label = model.names[cls].lower()
command = None
if label == "can":
command = b'C'
elif label == "nylon":
command = b'N'
elif label == "bottle":
command = b'B'
if command:
print(f"Sending to Arduino: {command.decode()} (Detected: {label})")
arduino.write(command)
# Draw on frame
x1, y1, x2, y2 = map(int, box.xyxy[0])
cv2.rectangle(frame, (x1, y1), (x2, y2), (0,255,0), 2)
cv2.putText(frame, label, (x1, y1-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,255,0), 2)
cv2.imshow("Sorter", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
arduino.close()