-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetector.py
63 lines (54 loc) · 1.83 KB
/
detector.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 cvzone
import math
import time
from utils import findPokerHand
cap = cv2.VideoCapture(0) # For Webcam
cap.set(3, 1280)
cap.set(4, 720)
#cap = cv2.VideoCapture("../Videos/motorbikes.mp4") # For Video
model = YOLO("playingCards.pt")
classNames = ['10C', '10D', '10H', '10S',
'2C', '2D', '2H', '2S',
'3C', '3D', '3H', '3S',
'4C', '4D', '4H', '4S',
'5C', '5D', '5H', '5S',
'6C', '6D', '6H', '6S',
'7C', '7D', '7H', '7S',
'8C', '8D', '8H', '8S',
'9C', '9D', '9H', '9S',
'AC', 'AD', 'AH', 'AS',
'JC', 'JD', 'JH', 'JS',
'KC', 'KD', 'KH', 'KS',
'QC', 'QD', 'QH', 'QS']
while True:
new_frame_time = time.time()
success, img = cap.read()
results = model(img, stream=True)
hand = []
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)
# cv2.rectangle(img,(x1,y1),(x2,y2),(255,0,255),3)
w, h = x2 - x1, y2 - y1
cvzone.cornerRect(img, (x1, y1, w, h))
# Confidence
conf = math.ceil((box.conf[0] * 100)) / 100
# Class Name
cls = int(box.cls[0])
cvzone.putTextRect(img, f'{classNames[cls]} {conf}', (max(0, x1), max(35, y1)), scale=1, thickness=1)
if conf > 0.5:
hand.append(classNames[cls])
print(hand)
hand = list(set(hand))
print(hand)
if len(hand) == 5:
results = findPokerHand(hand)
print(results)
cvzone.putTextRect(img, f"Your Hand : {results}", (300, 75), scale=3, thickness=5)
cv2.imshow("Image", img)
cv2.waitKey(1)