Felipefe20 commited on
Commit
c281bc7
1 Parent(s): 5f27e52

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ultralytics import YOLO
2
+ import cv2
3
+ import math
4
+ # start webcam
5
+ cap = cv2.VideoCapture(0)
6
+ cap.set(3, 640)
7
+ cap.set(4, 480)
8
+
9
+ # model
10
+ model = YOLO("yolo-Weights/best.pt")
11
+
12
+ # object classes
13
+ classNames = ["plush","lego","book"]
14
+
15
+
16
+ while True:
17
+ success, img = cap.read()
18
+ results = model(img, stream=True)
19
+
20
+ # coordinates
21
+ for r in results:
22
+ boxes = r.boxes
23
+
24
+ for box in boxes:
25
+ # bounding box
26
+ x1, y1, x2, y2 = box.xyxy[0]
27
+ x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values
28
+
29
+ # put box in cam
30
+ cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
31
+
32
+ # confidence
33
+ confidence = math.ceil((box.conf[0]*100))/100
34
+ print("Confidence --->",confidence)
35
+
36
+ # class name
37
+ cls = int(box.cls[0])
38
+ print("Class name -->", classNames[cls])
39
+
40
+ # object details
41
+ org = [x1, y1]
42
+ font = cv2.FONT_HERSHEY_SIMPLEX
43
+ fontScale = 1
44
+ color = (255, 0, 0)
45
+ thickness = 2
46
+
47
+ cv2.putText(img, classNames[cls], org, font, fontScale, color, thickness)
48
+
49
+ cv2.imshow('Webcam', img)
50
+ if cv2.waitKey(1) == ord('q'):
51
+ break
52
+
53
+ cap.release()
54
+ cv2.destroyAllWindows()
55
+