Welcome to the fascinating world of computer vision and Python programming! In this blog, we're embarking on an exciting journey to explore the realm of finger detection. Whether you're a curious beginner or an experienced developer looking to enhance your skills, join us as we unravel the magic of creating a Finger Counter using the powerful combination of computer vision and OpenCV in Python.
Before we dive into coding, let's ensure you have the necessary tools set up. We'll be working with Python, OpenCV, and a dash of computer vision knowledge. Don't worry if you're new to these technologies; we'll guide you every step of the way.
A well-known Python computer vision library called OpenCV is used to create the finger detection model. This model detects hand gestures in photos, creates hand outlines, counts fingers, and shows the count on the picture.
Strong open-source computer vision library OpenCV makes image and video processing easier for tasks like gesture analysis and object detection.
Python is a popular programming language in AI and data science that is easy to learn and adaptable. As such, it's a great option for creating OpenCV applications.
Here is entire code in Python:
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
# cap = cv2.VideoCapture(0)
def handGesture(img):
# ret,img = cap.read()
img = cv2.resize(img, (int(img.shape[1]/4), int(img.shape[0]/4)))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
ret,thresh1 = cv2.threshold(blur,70,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
contours, hierarchy = cv2.findContours(thresh1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
drawing = np.zeros(img.shape,np.uint8)
max_area=0
for i in range(len(contours)):
cnt=contours[i]
area = cv2.contourArea(cnt)
if (area>max_area):
max_area=area
ci=i
cnt=contours[ci]
hull = cv2.convexHull(cnt)
moments = cv2.moments(cnt)
if moments['m00']!=0:
cx = int(moments['m10']/moments['m00']) # cx = M10/M00
cy = int(moments['m01']/moments['m00']) # cy = M01/M00
centr=(cx,cy)
cv2.circle(img,centr,5,[255, 0, 0],2)
cv2.drawContours(drawing,[cnt],0,(0,255,0),2)
cv2.drawContours(drawing,[hull],0,(255, 0, 0),2)
cnt = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
hull = cv2.convexHull(cnt,returnPoints = False)
if (1):
defects = cv2.convexityDefects(cnt,hull)
mind=0
maxd=0
for i in range(defects.shape[0]):
s,e,f,d = defects[i,0]
start = tuple(cnt[s][0])
end = tuple(cnt[e][0])
far = tuple(cnt[f][0])
dist = cv2.pointPolygonTest(cnt,centr,True)
cv2.line(img,start,end,[255, 0, 0],5)
cv2.circle(img,far,10,[255, 0, 0],-1)
text = "Number of Fingers: " + str(i)
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
font_thickness = 2
text_size = cv2.getTextSize(text, font, font_scale, font_thickness)[0]
text_x = (img.shape[1] - text_size[0]) // 2
text_y = 100
cv2.putText(img, text, (text_x, text_y), font, font_scale, (255, 0, 0), font_thickness)
i=0
return (img)
input = cv2.imread("image3.jpg")
output = handGesture(input)
cv2_imshow(output)
0 Comments