A computer vision-based dice recognition game using YOLOv8, Flask, and IP Webcam.
The Two Dice Game is an interactive web application that uses computer vision to detect and recognize dice values from a camera feed. Players place one die on each side of the camera view, and the application detects the values, calculates scores, and determines when the game ends.
- Two players (left and right) take turns rolling the dice.
- Each player places one die on their respective side of the camera view.
- The application recognizes the dice values and adds them to the player's score.
- If both dice show the same value, the game ends.
- The player with the highest score wins.
- Real-time dice detection using a pre-trained YOLOv8 model
- Live camera feed using IP Webcam
- Automatic scoring system
- Multi-attempt detection system for improved accuracy
- Game state management
- Responsive web interface
- Error handling and connection monitoring
- Backend: Python, Flask
- Computer Vision: OpenCV, YOLOv8 (Ultralytics)
- Frontend: HTML, CSS, JavaScript
- Camera: IP Webcam app (Android)
- Install the IP Webcam app on your Android device
- Open the app and start the server
- Note the IP address displayed in the app (e.g., http://192.168.1.73:8080)
-
Start the Flask server:
python app.py
-
Open a web browser and navigate to:
http://localhost:5000 -
Enter your IP Webcam URL in the format:
http://IP_ADDRESS:8080/shot.jpgFor example:
http://192.168.1.73:8080/shot.jpg -
Click "Update Camera" to connect to your IP Webcam
-
Click "Start Game" to begin playing
-
Position one die on each side of the camera view and click "Capture"
- Camera Feed: Shows the live feed from your IP Webcam with left and right sides marked
- Score Board: Displays current scores for both players
- Game Status: Shows game messages and instructions
- Dice Display: Shows the most recently captured dice values
- Last Capture: Displays the processed image with dice detection boxes
The dice detection model was trained using YOLOv8 on a dataset of dice images. The training process involves:
- Data preparation: Organizing images and annotations
- Model selection: Using YOLOv8x (extra large) pre-trained model
- Training configuration: Setting epochs, batch size, and image size
- Training: Running the model training process
- Evaluation: Testing model performance on test data
- Export: Saving the trained model for use in the application
- Model Architecture: YOLOv8x
- Training Data: Annotated dice images
- Classes: 6 (dice faces 1-6)
- Epochs: 20
- Image Size: 640x640
- Batch Size: 16
The training code can be found in train.ipynb.

- Image Acquisition: Frames are captured from the IP Webcam
- Image Enhancement: Contrast adjustment and sharpening for better recognition
- Multi-attempt Detection:
- Standard prediction
- Brightness-adjusted prediction
- Perspective-transformed prediction
- Duplicate Filtering: Removing duplicate detections of the same dice
- Confidence Thresholding: Using confidence scores to validate detections
- Historical Value Tracking: Remembering previous valid detections for reliability
def filter_duplicate_dice(dice_list):
# Sort by confidence (descending)
dice_list.sort(key=lambda x: x[1], reverse=True)
# Filter unique dice values
unique_dice = {}
for value, confidence, box in dice_list:
if value not in unique_dice:
unique_dice[value] = (value, confidence, box)
# Return the list of unique dice
return list(unique_dice.values())# Apply image enhancement
alpha = 1.2 # Contrast control
beta = 5 # Brightness control
frame = cv2.convertScaleAbs(frame, alpha=alpha, beta=beta)
# Apply slight sharpening
kernel = np.array([[-1,-1,-1], [-1, 9,-1], [-1,-1,-1]])
frame = cv2.filter2D(frame, -1, kernel)- Ensure your phone and computer are on the same network
- Check that your firewall isn't blocking the connection
- Verify the IP address is correct in the IP Webcam app
- Try disabling mobile data on your phone
- Ensure good lighting conditions
- Use dice with high contrast between dots and background
- Place dice on a plain, non-reflective surface
- Keep dice separated with clear space between them
- Try adjusting the camera angle if detection fails
- For better performance, use a computer with a CUDA-compatible GPU
- Close other resource-intensive applications
- Reduce the camera resolution in the IP Webcam app if the stream is laggy