Introduction: In this tutorial, we will create a real-time coin detection and value calculation system using OpenCV and cvzone. The goal is to detect coins in a video feed, calculate their areas, and estimate their value based on their size. We will use Python for this project.
Prerequisites:
- Basic knowledge of Python
- Familiarity with OpenCV and image processing concepts
- Python 3.x installed
- OpenCV and cvzone libraries installed (you can install them with
pip install opencv-python cvzone
)
Let’s get started!
Step 1: Import necessary libraries
First, we need to import the required libraries: OpenCV for image processing, cvzone for additional computer vision tools, and NumPy for numerical operations.
import cv2
import cvzone
import numpy as np
Step 2: Set up video capture and window settings
We’ll use OpenCV’s VideoCapture function to capture video from the default camera (camera index 0). We’ll also set the frame width and height to 240 pixels each.
cap = cv2.VideoCapture(0)
cap.set(3, 240)
cap.set(4, 240)
Step 3: Create trackbars for threshold settings
We’ll create an empty function for the trackbar callback and create two trackbars for adjusting the Canny edge detection thresholds.
def empty(a):
pass
cv2.namedWindow("Settings")
cv2.resizeWindow("Settings", 640, 240)
cv2.createTrackbar("Threshold1", "Settings", 51, 255, empty)
cv2.createTrackbar("Threshold2", "Settings", 239, 255, empty)
Step 4: Define the preProcessing function
The preProcessing function applies a Gaussian blur, Canny edge detection, dilation, and morphological closing to the input image.
def preProcessing(img):
imgPre = cv2.GaussianBlur(img, (5, 5), 3)
threshold1 = cv2.getTrackbarPos("Threshold1", "Settings")
threshold2 = cv2.getTrackbarPos("Threshold2", "Settings")
imgPre = cv2.Canny(imgPre, threshold1, threshold2)
kernel = np.ones((3, 3), np.uint8)
imgPre = cv2.dilate(imgPre, kernel, iterations=1)
imgPre = cv2.morphologyEx(imgPre, cv2.MORPH_CLOSE, kernel)
return imgPre
Step 5: Process video frames
In the main loop, we capture video frames, preprocess them, and find contours using cvzone. Then, we calculate the total value of detected coins based on their areas and display the results.
while True:
success, img = cap.read()
imgPre = preProcessing(img)
imgContours, conFound = cvzone.findContours(img, imgPre, minArea=20)
total_money = 0
if conFound:
for contour in conFound:
peri = cv2.arcLength(contour['cnt'], True)
approx = cv2.approxPolyDP(contour['cnt'], 0.02 * peri, True)
if len(approx) > 5:
area = contour['area']
# Coin value calculation logic based on area
# ...
imageStacked = cvzone.stackImages([img, imgPre, imgContours], 2, 1)
cvzone.putTextRect(imageStacked, f"{total_money} AZN", (500, 900))
#cvzone.findContours(img,)
cv2.imshow("img", imageStacked)
cv2.waitKey(1)
Step 6: Run the script
Save the script and run it. A new window will display the real-time video feed along with the detected contours and calculated coin values. Adjust the trackbars in the “Settings” window to fine-tune the Canny edge detection thresholds for better results.
Conclusion:
In this tutorial, we learned how to create a real-time coin detection and value calculation system using OpenCV and cvzone. By following these steps, you can create a simple application that processes video frames and estimates the total value of coins based on their areas. You can further improve this project by refining the coin value estimation logic, using machine learning techniques, or even adding support for different currencies.