Untitled
unknown
plain_text
2 years ago
2.4 kB
4
Indexable
Never
import cv2 import numpy as np # Load the classifier classifier = cv2.CascadeClassifier("playing_card.xml") # Load the font for displaying text font = cv2.FONT_HERSHEY_SIMPLEX # Define the card values card_values = { 1: "A", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 10: "10", 11: "J", 12: "Q", 13: "K" } # Initialize the card count and high card count card_count = 0 high_card_count = 0 # Define the threshold for high cards high_card_threshold = 10 # Define the total number of cards in the deck total_cards = 52 # Initialize the user interface cv2.namedWindow("Card Detector") cv2.namedWindow("Remaining Cards") # Initialize the webcam cap = cv2.VideoCapture(0) # Initialize the list of remaining cards remaining_cards = list(range(1, 14)) * 4 while True: # Read a frame from the webcam ret, frame = cap.read() # Convert the frame to grayscale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect playing cards in the frame cards = classifier.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5) # Draw rectangles around the detected cards and display the card count for (x, y, w, h) in cards: card_value = get_card_value(gray[y:y+h, x:x+w]) card_count += 1 if card_value >= 10: high_card_count += 1 cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.putText(frame, card_values[card_value], (x, y), font, 1, (0, 255, 0), 2) if card_value in remaining_cards: remaining_cards.remove(card_value) cv2.imshow("Remaining Cards", draw_remaining_cards(remaining_cards, card_values)) # Calculate the percentage of high cards remaining in the deck high_cards_remaining = len([c for c in remaining_cards if c >= 10]) remaining_card_count = len(remaining_cards) high_card_percentage = round((high_cards_remaining / remaining_card_count) * 100, 2) # Display the webcam feed and the card count and high card percentage cv2.imshow("Card Detector", frame) print("Card Count:", card_count) print("High Card Percentage:", high_card_percentage) # Exit the loop if the 'q' key is pressed if cv2.waitKey(1) == ord('q'): break # Release the webcam and close the windows cap.release() cv2.destroyAllWindows()