-
Notifications
You must be signed in to change notification settings - Fork 1
/
yoloCode_test1.py
63 lines (49 loc) · 2.29 KB
/
yoloCode_test1.py
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
from ultralytics import YOLO
import cv2
import math
# start webcam
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
# model
model = YOLO("yolo-Weights/newModel.pt") #determine when to restart #set limit of 5 seconds? take maximum of output, result = max(result, item)
#store input. Strictly 1 item at a time.
#what would output be given an undefined? - garbage i guess lol
#edge cases: having multiple items, stop/start time, we need to reset output for next item, exclude person, if object has an image of a person - should ignore person, just take the other output (if output is something like plastic + person)
#classnames: plastic, garbage, compost
#TAKE OUTPUT FROM MACHINE LEARNING -> SEND CLASSNAME TO CHATGPT API -> CHATGPT API TELLS US IF PLASTIC, GARBAGE, OR COMPOST -> GIVE TO ARDUINO 0, 1, 2
#EXAMPLE OF INPUT FOR API: OUTPUT FROM ALGO is toothbrush, send toothbrush to api and ask - does it belong in plastic, garbage, or compost? ->gives answer!
#if input something like juice box, person -> need to have code to filter out any invalid inputs then send to chatgpt api
# object classes
classNames = ["Battery", "Biological", "Brown glass", "Cardboard", "Clothes", "Green Grass", "Metal", "Paper", "Plastic",
"Shoes", "Trash", "White glass"]
while True:
success, img = cap.read()
results = model(img, stream=True)
# coordinates
for r in results:
boxes = r.boxes
for box in boxes:
# bounding box
x1, y1, x2, y2 = box.xyxy[0]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values
# put box in cam
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
# confidence
confidence = math.ceil((box.conf[0]*100))/100
print("Confidence --->",confidence)
# class name
cls = int(box.cls[0])
print("Class name -->", classNames[cls])
# object details
org = [x1, y1]
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
color = (255, 0, 0)
thickness = 2
cv2.putText(img, classNames[cls], org, font, fontScale, color, thickness)
cv2.imshow('Webcam', img)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()