{"id":336654,"date":"2022-08-06T09:00:09","date_gmt":"2022-08-06T09:00:09","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=336654"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=336654","title":{"rendered":"<span>\u0420\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0434\u043d\u044f\u0442\u044b\u0445 \u043f\u0430\u043b\u044c\u0446\u0435\u0432 \u043d\u0430 Python+OpenCV<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u0438 \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u0434\u0441\u0447\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043f\u043e\u0434\u043d\u044f\u0442\u044b\u0445 \u043f\u0430\u043b\u044c\u0446\u0435\u0432.<\/p>\n<p>\u0412\u0441\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043d\u0430 \u043c\u043e\u0435\u043c <a href=\"https:\/\/github.com\/paveldat\/finger_counter\" rel=\"noopener noreferrer nofollow\">Github<\/a>.<\/p>\n<p>\u041a\u043e\u0434 \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043d\u0430\u0447\u0430\u043b\u0430, \u043d\u043e \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u043c\u043e\u0438\u043c\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u043c\u0438 <a href=\"https:\/\/habr.com\/ru\/users\/Pavel_Dat\/posts\/\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u044f\u043c\u0438<\/a>.<\/p>\n<p>\u041f\u043e\u0434\u0433\u043e\u0442\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0441\u0440\u0435\u0434\u0443 \u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438:<\/p>\n<pre><code>pip install mediapipe pip install opencv-python pip install math<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b  <code>HandTrackingModule.py<\/code> \u0441 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u043c \u0434\u043b\u044f \u043c\u043e\u0438\u0445 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0435\u0439 \u043a\u043b\u0430\u0441\u0441\u043e\u043c <code>handDetector<\/code>:<\/p>\n<pre><code class=\"python\">import cv2 import mediapipe as mp import time import math  class handDetector(): def __init__(self, mode=False, maxHands=2, modelComplexity=1, detectionCon=0.5, trackCon=0.5): self.mode = mode self.maxHands = maxHands self.modelComplexity = modelComplexity self.detectionCon = detectionCon self.trackCon = trackCon  self.mpHands = mp.solutions.hands self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.modelComplexity, self.detectionCon, self.trackCon) self.mpDraw = mp.solutions.drawing_utils self.tipIds = [4, 8, 12, 16, 20]   def findHands(self, img, draw=True): imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) self.results = self.hands.process(imgRGB) #print(results.multi_hand_landmarks)  if self.results.multi_hand_landmarks: for handLms in self.results.multi_hand_landmarks: if draw: self.mpDraw.draw_landmarks(img, handLms, self.mpHands.HAND_CONNECTIONS) return img  def findPosition(self, img, handNo=0, draw=True): xList = [] yList = [] bbox = [] self.lmList = [] if self.results.multi_hand_landmarks: myHand = self.results.multi_hand_landmarks[handNo] for id, lm in enumerate(myHand.landmark): #print(id, lm) h, w, c = img.shape cx, cy = int(lm.x*w), int(lm.y*h) xList.append(cx) yList.append(cy) #print(id, cx, cy) self.lmList.append([id, cx, cy]) if draw: cv2.circle(img, (cx, cy), 5, (255,0,255), cv2.FILLED) xmin, xmax = min(xList), max(xList) ymin, ymax = min(yList), max(yList) bbox = xmin, ymin, xmax, ymax  if draw: cv2.rectangle(img, (bbox[0]-20, bbox[1]-20), (bbox[2]+20, bbox[3]+20), (0, 255, 0), 2) return self.lmList, bbox  def findDistance(self, p1, p2, img, draw=True): x1, y1 = self.lmList[p1][1], self.lmList[p1][2] x2, y2 = self.lmList[p2][1], self.lmList[p2][2] cx, cy = (x1+x2)\/\/2, (y1+y2)\/\/2  if draw: cv2.circle(img, (x1,y1), 15, (255,0,255), cv2.FILLED) cv2.circle(img, (x2,y2), 15, (255,0,255), cv2.FILLED) cv2.line(img, (x1,y1), (x2,y2), (255,0,255), 3) cv2.circle(img, (cx,cy), 15, (255,0,255), cv2.FILLED)  length = math.hypot(x2-x1, y2-y1) return length, img, [x1, y1, x2, y2, cx, cy]  def fingersUp(self): fingers = []  # Thumb if self.lmList[self.tipIds[0]][1] &lt; self.lmList[self.tipIds[0]-1][1]: fingers.append(1) else: fingers.append(0)  # 4 Fingers for id in range(1,5): if self.lmList[self.tipIds[id]][2] &lt; self.lmList[self.tipIds[id]-2][2]: fingers.append(1) else: fingers.append(0) return fingers  def main(): pTime = 0 cTime = 0 cap = cv2.VideoCapture(0) detector = handDetector() while True: success, img = cap.read() img = detector.findHands(img) lmList = detector.findPosition(img) if len(lmList) != 0: print(lmList[1])  cTime = time.time() fps = 1. \/ (cTime - pTime) pTime = cTime  cv2.putText(img, str(int(fps)), (10,70), cv2.FONT_HERSHEY_PLAIN, 3, (255,0,255), 3)  cv2.imshow(\"Image\", img) cv2.waitKey(1)   if __name__ == \"__main__\": main()<\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u043c \u0438 \u044f \u0435\u0433\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 OpenCV.<\/p>\n<p>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438. \u0421\u043a\u0430\u0447\u0430\u0435\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0441 <a href=\"https:\/\/github.com\/paveldat\/finger_counter\/tree\/main\/fingers\" rel=\"noopener noreferrer nofollow\">Github<\/a> \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u043c \u0438\u0445 \u0432 \u043f\u0430\u043f\u043a\u0443 <code>fingers<\/code>. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0438\u0445 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f, \u043b\u043e\u0433\u0438\u043a\u0430 \u0442\u0443\u0442 \u043f\u0440\u043e\u0441\u0442\u0430\u044f &#8212; \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>&lt;num>.jpg<\/code>, \u0433\u0434\u0435 <code>num<\/code> &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044b\u0445 \u043f\u0430\u043b\u044c\u0446\u0435\u0432.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b <code>main.py<\/code> \u0438 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438:<\/p>\n<pre><code class=\"python\">import cv2 import time import os import HandTrackingModule as htm<\/code><\/pre>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043a\u0430\u043c\u0435\u0440\u0443:<\/p>\n<pre><code class=\"python\">wCam, hCam = 640, 480  cap = cv2.VideoCapture(0) cap.set(3, wCam) cap.set(4, hCam)<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u043a\u0430\u043c\u0435\u0440\u044b \u043c\u043e\u0433\u0443\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438, \u043f\u043e\u043c\u0435\u043d\u044f\u0439\u0442\u0435\u00a0<code>0<\/code>\u00a0\u0438\u0437 `cap = cv2.VideoCapture(0)` \u043d\u0430\u00a0<code>1<\/code>\u00a0\u0438\u043b\u0438\u00a0<code>2<\/code>.  <\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0432\u0441\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">folderPath = \"fingers\" # name of the folder, where there are images of fingers fingerList = os.listdir(folderPath) # list of image titles in 'fingers' folder overlayList = [] for imgPath in fingerList:     image = cv2.imread(f'{folderPath}\/{imgPath}')     overlayList.append(image)<\/code><\/pre>\n<p>\u041e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435:<\/p>\n<pre><code class=\"python\">pTime = 0  detector = htm.handDetector(detectionCon=0.75) totalFingers = 0<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0446\u0438\u043a\u043b (\u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443, \u0435\u0441\u043b\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f), \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043a\u0430\u043c\u0435\u0440\u0443 \u0438 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u0440\u0443\u043a\u0443 \u0432 \u043a\u0430\u0434\u0440\u0435:<\/p>\n<pre><code class=\"python\">while True:     sucess, img = cap.read()     img = cv2.flip(img, 1)      img = detector.findHands(img)     lmList, bbox = detector.findPosition(img, draw=False)<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0441 \u043f\u043e\u0437\u0438\u0446\u0438\u044f\u043c\u0438 \u0440\u0443\u043a\u0438 \u043d\u0435 \u043f\u0443\u0441\u0442\u043e\u0439, \u0442\u043e \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044b\u0445 \u043f\u0430\u043b\u044c\u0446\u0435\u0432:<\/p>\n<pre><code class=\"python\">if lmList:         fingersUp = detector.fingersUp()         totalFingers = fingersUp.count(1)<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0431\u0443\u0434\u0435\u043c \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u043f\u043e\u0434\u043d\u044f\u0442\u044b\u0445 \u043f\u0430\u043b\u044c\u0446\u0435\u0432:<\/p>\n<pre><code class=\"python\">h, w, c = overlayList[totalFingers].shape img[0:h, 0:w] = overlayList[totalFingers]<\/code><\/pre>\n<p>\u0418 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435, \u0441\u0447\u0438\u0442\u0430\u0435\u043c <code>FPS<\/code> \u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u043d\u0430\u0434\u043f\u0438\u0441\u0438 \u0432 \u043e\u043a\u043d\u0435:<\/p>\n<pre><code class=\"python\">cTime = time.time()     fps = 1\/ (cTime-pTime)     pTime = cTime      cv2.putText(img, f'FPS: {int(fps)}', (400, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)     cv2.rectangle(img, (20, 225), (170, 425), (0, 255, 0), cv2.FILLED)     cv2.putText(img, str(totalFingers), (45, 375), cv2.FONT_HERSHEY_PLAIN, 10, (255, 0, 0), 25)      cv2.imshow(\"Image\", img)     cv2.waitKey(1)<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ef9\/8a3\/b3e\/ef98a3b3e1c1ad09c6bc3bcb1fefcb36.gif\" width=\"640\" height=\"480\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ef9\/8a3\/b3e\/ef98a3b3e1c1ad09c6bc3bcb1fefcb36.gif\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0412\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442?<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/679460\/\"> https:\/\/habr.com\/ru\/post\/679460\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u0438 \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u0434\u0441\u0447\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043f\u043e\u0434\u043d\u044f\u0442\u044b\u0445 \u043f\u0430\u043b\u044c\u0446\u0435\u0432.<\/p>\n<p>\u0412\u0441\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043d\u0430 \u043c\u043e\u0435\u043c <a href=\"https:\/\/github.com\/paveldat\/finger_counter\" rel=\"noopener noreferrer nofollow\">Github<\/a>.<\/p>\n<p>\u041a\u043e\u0434 \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043d\u0430\u0447\u0430\u043b\u0430, \u043d\u043e \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u043c\u043e\u0438\u043c\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u043c\u0438 <a href=\"https:\/\/habr.com\/ru\/users\/Pavel_Dat\/posts\/\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u044f\u043c\u0438<\/a>.<\/p>\n<p>\u041f\u043e\u0434\u0433\u043e\u0442\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0441\u0440\u0435\u0434\u0443 \u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438:<\/p>\n<pre><code>pip install mediapipe pip install opencv-python pip install math<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b  <code>HandTrackingModule.py<\/code> \u0441 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u043c \u0434\u043b\u044f \u043c\u043e\u0438\u0445 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0435\u0439 \u043a\u043b\u0430\u0441\u0441\u043e\u043c <code>handDetector<\/code>:<\/p>\n<pre><code class=\"python\">import cv2 import mediapipe as mp import time import math  class handDetector(): def __init__(self, mode=False, maxHands=2, modelComplexity=1, detectionCon=0.5, trackCon=0.5): self.mode = mode self.maxHands = maxHands self.modelComplexity = modelComplexity self.detectionCon = detectionCon self.trackCon = trackCon  self.mpHands = mp.solutions.hands self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.modelComplexity, self.detectionCon, self.trackCon) self.mpDraw = mp.solutions.drawing_utils self.tipIds = [4, 8, 12, 16, 20]   def findHands(self, img, draw=True): imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) self.results = self.hands.process(imgRGB) #print(results.multi_hand_landmarks)  if self.results.multi_hand_landmarks: for handLms in self.results.multi_hand_landmarks: if draw: self.mpDraw.draw_landmarks(img, handLms, self.mpHands.HAND_CONNECTIONS) return img  def findPosition(self, img, handNo=0, draw=True): xList = [] yList = [] bbox = [] self.lmList = [] if self.results.multi_hand_landmarks: myHand = self.results.multi_hand_landmarks[handNo] for id, lm in enumerate(myHand.landmark): #print(id, lm) h, w, c = img.shape cx, cy = int(lm.x*w), int(lm.y*h) xList.append(cx) yList.append(cy) #print(id, cx, cy) self.lmList.append([id, cx, cy]) if draw: cv2.circle(img, (cx, cy), 5, (255,0,255), cv2.FILLED) xmin, xmax = min(xList), max(xList) ymin, ymax = min(yList), max(yList) bbox = xmin, ymin, xmax, ymax  if draw: cv2.rectangle(img, (bbox[0]-20, bbox[1]-20), (bbox[2]+20, bbox[3]+20), (0, 255, 0), 2) return self.lmList, bbox  def findDistance(self, p1, p2, img, draw=True): x1, y1 = self.lmList[p1][1], self.lmList[p1][2] x2, y2 = self.lmList[p2][1], self.lmList[p2][2] cx, cy = (x1+x2)\/\/2, (y1+y2)\/\/2  if draw: cv2.circle(img, (x1,y1), 15, (255,0,255), cv2.FILLED) cv2.circle(img, (x2,y2), 15, (255,0,255), cv2.FILLED) cv2.line(img, (x1,y1), (x2,y2), (255,0,255), 3) cv2.circle(img, (cx,cy), 15, (255,0,255), cv2.FILLED)  length = math.hypot(x2-x1, y2-y1) return length, img, [x1, y1, x2, y2, cx, cy]  def fingersUp(self): fingers = []  # Thumb if self.lmList[self.tipIds[0]][1] &lt; self.lmList[self.tipIds[0]-1][1]: fingers.append(1) else: fingers.append(0)  # 4 Fingers for id in range(1,5): if self.lmList[self.tipIds[id]][2] &lt; self.lmList[self.tipIds[id]-2][2]: fingers.append(1) else: fingers.append(0) return fingers  def main(): pTime = 0 cTime = 0 cap = cv2.VideoCapture(0) detector = handDetector() while True: success, img = cap.read() img = detector.findHands(img) lmList = detector.findPosition(img) if len(lmList) != 0: print(lmList[1])  cTime = time.time() fps = 1. \/ (cTime - pTime) pTime = cTime  cv2.putText(img, str(int(fps)), (10,70), cv2.FONT_HERSHEY_PLAIN, 3, (255,0,255), 3)  cv2.imshow(\"Image\", img) cv2.waitKey(1)   if __name__ == \"__main__\": main()<\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u043c \u0438 \u044f \u0435\u0433\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 OpenCV.<\/p>\n<p>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438. \u0421\u043a\u0430\u0447\u0430\u0435\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0441 <a href=\"https:\/\/github.com\/paveldat\/finger_counter\/tree\/main\/fingers\" rel=\"noopener noreferrer nofollow\">Github<\/a> \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u043c \u0438\u0445 \u0432 \u043f\u0430\u043f\u043a\u0443 <code>fingers<\/code>. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0438\u0445 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f, \u043b\u043e\u0433\u0438\u043a\u0430 \u0442\u0443\u0442 \u043f\u0440\u043e\u0441\u0442\u0430\u044f &#8212; \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>&lt;num>.jpg<\/code>, \u0433\u0434\u0435 <code>num<\/code> &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044b\u0445 \u043f\u0430\u043b\u044c\u0446\u0435\u0432.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b <code>main.py<\/code> \u0438 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438:<\/p>\n<pre><code class=\"python\">import cv2 import time import os import HandTrackingModule as htm<\/code><\/pre>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043a\u0430\u043c\u0435\u0440\u0443:<\/p>\n<pre><code class=\"python\">wCam, hCam = 640, 480  cap = cv2.VideoCapture(0) cap.set(3, wCam) cap.set(4, hCam)<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u043a\u0430\u043c\u0435\u0440\u044b \u043c\u043e\u0433\u0443\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438, \u043f\u043e\u043c\u0435\u043d\u044f\u0439\u0442\u0435\u00a0<code>0<\/code>\u00a0\u0438\u0437 `cap = cv2.VideoCapture(0)` \u043d\u0430\u00a0<code>1<\/code>\u00a0\u0438\u043b\u0438\u00a0<code>2<\/code>.  <\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0432\u0441\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">folderPath = \"fingers\" # name of the folder, where there are images of fingers fingerList = os.listdir(folderPath) # list of image titles in 'fingers' folder overlayList = [] for imgPath in fingerList:     image = cv2.imread(f'{folderPath}\/{imgPath}')     overlayList.append(image)<\/code><\/pre>\n<p>\u041e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435:<\/p>\n<pre><code class=\"python\">pTime = 0  detector = htm.handDetector(detectionCon=0.75) totalFingers = 0<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0446\u0438\u043a\u043b (\u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443, \u0435\u0441\u043b\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f), \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043a\u0430\u043c\u0435\u0440\u0443 \u0438 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u0440\u0443\u043a\u0443 \u0432 \u043a\u0430\u0434\u0440\u0435:<\/p>\n<pre><code class=\"python\">while True:     sucess, img = cap.read()     img = cv2.flip(img, 1)      img = detector.findHands(img)     lmList, bbox = detector.findPosition(img, draw=False)<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0441 \u043f\u043e\u0437\u0438\u0446\u0438\u044f\u043c\u0438 \u0440\u0443\u043a\u0438 \u043d\u0435 \u043f\u0443\u0441\u0442\u043e\u0439, \u0442\u043e \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044b\u0445 \u043f\u0430\u043b\u044c\u0446\u0435\u0432:<\/p>\n<pre><code class=\"python\">if lmList:         fingersUp = detector.fingersUp()         totalFingers = fingersUp.count(1)<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0431\u0443\u0434\u0435\u043c \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u043f\u043e\u0434\u043d\u044f\u0442\u044b\u0445 \u043f\u0430\u043b\u044c\u0446\u0435\u0432:<\/p>\n<pre><code class=\"python\">h, w, c = overlayList[totalFingers].shape img[0:h, 0:w] = overlayList[totalFingers]<\/code><\/pre>\n<p>\u0418 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435, \u0441\u0447\u0438\u0442\u0430\u0435\u043c <code>FPS<\/code> \u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u043d\u0430\u0434\u043f\u0438\u0441\u0438 \u0432 \u043e\u043a\u043d\u0435:<\/p>\n<pre><code class=\"python\">cTime = time.time()     fps = 1\/ (cTime-pTime)     pTime = cTime      cv2.putText(img, f'FPS: {int(fps)}', (400, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)     cv2.rectangle(img, (20, 225), (170, 425), (0, 255, 0), cv2.FILLED)     cv2.putText(img, str(totalFingers), (45, 375), cv2.FONT_HERSHEY_PLAIN, 10, (255, 0, 0), 25)      cv2.imshow(\"Image\", img)     cv2.waitKey(1)<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442?<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/679460\/\"> https:\/\/habr.com\/ru\/post\/679460\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-336654","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/336654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=336654"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/336654\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=336654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=336654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=336654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}