42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
import cv2
|
|
import numpy as np
|
|
|
|
# read webcam
|
|
webcam = cv2.VideoCapture(0)
|
|
|
|
# visualize webcam
|
|
|
|
while True:
|
|
ret, frame = webcam.read()
|
|
|
|
Blur = cv2.medianBlur(frame,3)
|
|
dimensions = frame.shape
|
|
|
|
grayimg = cv2.cvtColor(Blur, cv2.COLOR_BGR2GRAY)
|
|
circles = cv2.HoughCircles(grayimg, cv2.HOUGH_GRADIENT,1.2,dimensions[0]/50)
|
|
|
|
thickness = 2
|
|
#Draw detected circles
|
|
if circles is not None:
|
|
print("Found circle")
|
|
circles = np.uint16(circles[0,:])
|
|
#print(circles)
|
|
print(type(circles.shape))
|
|
for (x, y, diameter) in circles:
|
|
#draw the outer circle
|
|
cv2.circle(frame, (x,y), diameter, (0,0,255), thickness, cv2.LINE_AA)
|
|
#draw the outer circle
|
|
cv2.circle(frame, (x,y), 2, (0,0,255), thickness, cv2.LINE_AA)
|
|
cv2.putText(frame, 'Total Circles: ' + str(circles.shape[0]), (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 4)
|
|
|
|
else:
|
|
print("Cannot detect circle.")
|
|
|
|
cv2.imshow('frame', frame)
|
|
if cv2.waitKey(40) & 0xFF == ord('q'):
|
|
break
|
|
|
|
|
|
|
|
webcam.release()
|
|
cv2.destroyAllWindows() |