'How can I change the pool ball detection code to OpenCV to remove the shaking?

I'm using pool ball detection with the HoughCircles function in OpenCV.

import numpy as np
import cv2 as cv
cap = cv.VideoCapture(1)
if not cap.isOpened():
    print("Cannot open camera")
    exit()
while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    # Our operations on the frame come here
    grayFrame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    blurFrame = cv.medianBlur(grayFrame,5)
    circles = cv.HoughCircles(blurFrame, cv.HOUGH_GRADIENT, 1, 10,
                              param1=15, param2=25, minRadius=10, maxRadius=20)
    print(circles)
    flag = np.any(circles)
    if flag:
        circles = np.uint16(np.around(circles))
        for i in circles[0, :]:
            # draw the outer circle
            cv.circle(frame, (i[0], i[1]), i[2], (0, 0, 255), 2)
            # draw the center of the circle
            cv.circle(frame, (i[0], i[1]), 2, (0, 255, 0), 3)
    else:
        continue
        # Display the resulting frame
    cv.imshow('frame', frame)
    if cv.waitKey(1) == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

And I get this result: https://youtu.be/Hc7pHGq7bkU

Original video: https://youtu.be/JrxOq1V9cTQ

What can I add or change to remove the shaking



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source