My program is supposed to use circle detection in order to detect droplets falling through the drip chamber of an IV setup. It's then supposed to calculate the flow rate per minute based on the number of circles it detects. I got this code from a youtube video and mixed it with chatGPT. There is also an ROI in which the code focuses on in the video for better results. This has been bothering me for a while and I looked for fixed online but they are not working. the error is at the
print(circles)
# Adjust the circle coordinates based on the ROI
chosen[:, 0] += ROI_X
chosen[:, 1] += ROI_Y
section. Here is the full code:
import numpy as np
import cv2 as cv
import time
# Constants
PIXEL_TO_VOLUME_FACTOR = 0.0001 # Conversion factor from pixel count to volume (ml)
TIME_INTERVAL = 1.0 # Time interval between consecutive frames (seconds)
# Define the ROI coordinates and dimensions
ROI_X = 100 # x-coordinate of the top-left corner of the ROI
ROI_Y = 100 # y-coordinate of the top-left corner of the ROI
ROI_WIDTH = 20 # width of the ROI
ROI_HEIGHT = 20 # height of the ROI
capture = cv.VideoCapture(r'C:\Users\Jonah Anonuevo\Documents\SY 2022-2023\4th Quarter\Research\testvid.mov')
prevCircle = None
prevTime = time.time()
flow_rate = 0.0
while True:
ret, frame = capture.read()
# Crop the frame to the ROI
roi_frame = frame[ROI_Y:ROI_Y + ROI_HEIGHT, ROI_X:ROI_X + ROI_WIDTH]
grayFrame = cv.cvtColor(roi_frame, cv.COLOR_BGR2GRAY)
blurFrame = cv.GaussianBlur(grayFrame, (17, 17), 2)
circles = cv.HoughCircles(blurFrame, cv.HOUGH_GRADIENT, 1.2, 100, 100, 120, 5, 50, 100)
if circles is not None and len(circles) > 0: # Check if circles is not None and not empty
circles = np.uint16(np.around(circles))
chosen = circles[:2] # Get the first set of circle coordinates
print(circles)
# Adjust the circle coordinates based on the ROI
chosen[:, 0] += ROI_X
chosen[:, 1] += ROI_Y
# for c in chosen:
cv.circle(frame, (c[0], c[1]), 1, (0, 100, 100), 3)
cv.circle(frame, (c[0], c[1]), c[2], (255, 0, 255), 3)
# Calculate flow rate
current_time = time.time()
elapsed_time = current_time - prevTime
if prevCircle is not None:
flow_rate = np.linalg.norm([chosen[0, 0] - prevCircle[0], chosen[0, 1] - prevCircle[1]]) * PIXEL_TO_VOLUME_FACTOR / elapsed_time
prevTime = current_time
#Display the flow rate
print("Flow Rate: {:.2f} ml/s".format(flow_rate))
prevCircle = chosen[0] # Store only the first circle for comparison
cv.imshow('Circles', frame)
if cv.waitKey(1) == ord('q'): # ends the loop when "q" is pressed
break
capture.release()
cv.destroyAllWindows()
After running, I get this:
PS C:\Users\Jonah Anonuevo\Documents\SY 2022-2023\4th Quarter\Research\Test Codes> c:; cd 'c:\Users\Jonah Anonuevo\Documents\SY 2022-2023\4th Quarter\Research\Test Codes'; & 'C:\Program Files\Python311\python.exe' 'c:\Users\Jonah Anonuevo\.vscode\extensions\ms-python.python-2023.12.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '51286' '--' 'C:\Users\Jonah Anonuevo\Documents\SY 2022-2023\4th Quarter\Research\Test Codes\chatgpt circle detection.py'
[[100]
[ 0]
[ 0]
[ 0]]
I tried fixes online but none of them seem to work. Also tried asking my computer science-inclined friends for help but to no avail.