Articles
1. Count circles using HoughCircles method
2. How to capture webcam screen shot with spacebar keypress
1. Count circles using HoughCircles method

#!/usr/bin/python3

import os
import cv2 as cv
import numpy as np

# returns total number of circles detected in the image
def detectCircles(file):
    img = cv.imread(file, cv.IMREAD_GRAYSCALE) # Read the image in grayscale
    assert img is not None, "file could not be read, check with os.path.exists()"
    img = cv.GaussianBlur(img, (7, 7), 1.5) # Apply GaussianBlur to reduce noise
    circles = cv.HoughCircles(
        img,
        cv.HOUGH_GRADIENT,
        dp=1,  # Inverse ratio of the accumulator resolution to the image resolution.
        minDist=50,  # Minimum distance between detected centers.
        param1=80,  # Upper threshold for the internal Canny edge detector.
        param2=43,  # Threshold for center detection.
        minRadius=0,  # Minimum radius to be detected.
        maxRadius=0  # Maximum radius to be detected.
    )
    return circles[0].shape[0]

if __name__=="__main__":

    directory="Shapes"
    files=os.listdir(directory) # List all the files in the directory
    files.sort() # Sort the files in ascending order

    for file in files:
        file_path = os.path.join(directory, file) # Create the path to the file
        NumCircles=detectCircles(file_path) # Call the function to detect the number of circles
        print(f"Image {file} has {NumCircles} Circles") # Print the result

Output
------
Image shapes1.png has 2 Circles
Image shapes2.png has 3 Circles
Image shapes3.png has 23 Circles
Image shapes4.png has 3 Circles
Image shapes5.png has 2 Circles
2. How to capture webcam screen shot with spacebar keypress

#!/usr/bin/python3
import cv2

cam = cv2.VideoCapture(0)
cv2.namedWindow('webcam screenshot')
img_counter = 0
while True:
  ret,frame = cam.read()
  if not ret:
    print('failed to grab frame')
    break
  cv2.imshow("test",frame)
  k = cv2.waitKey(1)
  if k == 27:
    print("Escape key pressed, clossing loop")
    break
  if k == 32:
    img_name = f'opencv_frame_{img_counter}.png'
    cv2.imwrite(img_name,frame)
    print('screenschot taken')
    img_counter += 1

cam.release()
cv2.destroyAllWindows()