CoolFace
Apppublic

datasciencedojo/Finger-Counting-Right-Hand

sourceHugging Faceupdated 3y agoView on Hugging Face
1likes
app.py231 linesDownload Raw Back to root
1import cv22import time3import os4import mediapipe as mp5import gradio as gr6from threading import Thread7#from cvzone.HandTrackingModule import HandDetector8example_flag = False9 10class handDetector():11    def __init__(self, mode=True, modelComplexity=1, maxHands=2, detectionCon=0.5, trackCon=0.5):12        self.mode = mode13        self.maxHands = maxHands14        self.detectionCon = detectionCon15        self.modelComplex = modelComplexity16        self.trackCon = trackCon17        self.mpHands = mp.solutions.hands18        self.hands = self.mpHands.Hands(self.mode, self.maxHands,self.modelComplex,self.detectionCon, self.trackCon)19        self.mpDraw = mp.solutions.drawing_utils20 21    def findHands(self, img, draw=True,flipType=True):22        """23        Finds hands in a BGR image.24        :param img: Image to find the hands in.25        :param draw: Flag to draw the output on the image.26        :return: Image with or without drawings27        """28        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)29        #cv2.imshow('test',imgRGB)30        self.results = self.hands.process(imgRGB)31        allHands = []32        h, w, c = img.shape33        if self.results.multi_hand_landmarks:34            for handType, handLms in zip(self.results.multi_handedness, self.results.multi_hand_landmarks):35                myHand = {}36                ## lmList37                mylmList = []38                xList = []39                yList = []40                for id, lm in enumerate(handLms.landmark):41                    px, py, pz = int(lm.x * w), int(lm.y * h), int(lm.z * w)42                    mylmList.append([px, py, pz])43                    xList.append(px)44                    yList.append(py)45 46                ## bbox47                xmin, xmax = min(xList), max(xList)48                ymin, ymax = min(yList), max(yList)49                boxW, boxH = xmax - xmin, ymax - ymin50                bbox = xmin, ymin, boxW, boxH51                cx, cy = bbox[0] + (bbox[2] // 2), \52                         bbox[1] + (bbox[3] // 2)53 54                myHand["lmList"] = mylmList55                myHand["bbox"] = bbox56                myHand["center"] = (cx, cy)57 58                if flipType:59                    if handType.classification[0].label == "Right":60                        myHand["type"] = "Left"61                    else:62                        myHand["type"] = "Right"63                else:64                    myHand["type"] = handType.classification[0].label65                allHands.append(myHand)66 67                ## draw68                if draw:69                    self.mpDraw.draw_landmarks(img, handLms,70                                               self.mpHands.HAND_CONNECTIONS)71                    cv2.rectangle(img, (bbox[0] - 20, bbox[1] - 20),72                                  (bbox[0] + bbox[2] + 20, bbox[1] + bbox[3] + 20),73                                  (255, 0, 255), 2)74                    #cv2.putText(img, myHand["type"], (bbox[0] - 30, bbox[1] - 30), cv2.FONT_HERSHEY_PLAIN,2, (255, 0, 255), 2)75        if draw:76            return allHands, img77        else:78            return allHands79    def findPosition(self, img, handNo=0, draw=True,flipType=False):80 81        lmList = []82        if self.results.multi_hand_landmarks:83            myHand = self.results.multi_hand_landmarks[handNo]84            for id, lm in enumerate(myHand.landmark):85                # print(id, lm)86                h, w, c = img.shape87                cx, cy = int(lm.x * w), int(lm.y * h)88                # print(id, cx, cy)89                lmList.append([id, cx, cy])90                if draw:91                    cv2.circle(img, (cx, cy), 15, (255, 0, 255), cv2.FILLED)92        return lmList93 94 95 96 97def set_example_image(example: list) -> dict:98    return gr.inputs.Image.update(value=example[0])99 100 101def count(im):102  folderPath = "Count"103  myList = os.listdir(folderPath)104  overlayList = []105  for imPath in sorted(myList):106      image = cv2.imread(f'{folderPath}/{imPath}')107      # print(f'{folderPath}/{imPath}')108      overlayList.append(image)109 110  #print(len(overlayList))111  tipIds = [4, 8, 12, 16, 20]112  detector = handDetector(detectionCon=0.75)113 114  #img = cv2.imread('test.jpg')115  allhands,img = detector.findHands(cv2.flip(im[:,:,::-1], 1))116  cv2.imwrite('test3.png',img)117  118  lmList = detector.findPosition(img, draw=False,)119  # print(lmList)120 121  if len(lmList) != 0:122      fingers = []123 124      # Thumb125      if lmList[tipIds[0]][1] > lmList[tipIds[0] - 1][1]:126          fingers.append(1)127      else:128          fingers.append(0)129 130      # 4 Fingers131      for id in range(1, 5):132          if lmList[tipIds[id]][2] < lmList[tipIds[id] - 2][2]:133              fingers.append(1)134          else:135              fingers.append(0)136 137      # print(fingers)138      totalFingers = fingers.count(1)139      #print(totalFingers)140      text = f"Total finger count is {totalFingers}!"141 142      h, w, c = overlayList[totalFingers - 1].shape143      img = cv2.flip(img,1)144      img[0:h, 0:w] = overlayList[totalFingers - 1]145      146 147      cv2.rectangle(img, (20, 225), (170, 425), (0, 255, 0), cv2.FILLED)148      cv2.putText(img, str(totalFingers), (45, 375), cv2.FONT_HERSHEY_PLAIN,149                  10, (255, 0, 0), 25)150      return img[:,:,::-1]151  else:152      return cv2.flip(img[:,:,::-1],1)153 154css = """155.gr-button-lg {156    z-index: 14;157    width: 113px;158    height: 30px;159    left: 0px;160    top: 0px;161    padding: 0px;162    cursor: pointer !important; 163    background: none rgb(17, 20, 45) !important;164    border: none !important;165    text-align: center !important;166    font-size: 14px !important;167    font-weight: 500 !important;168    color: rgb(255, 255, 255) !important;169    line-height: 1 !important;170    border-radius: 6px !important;171    transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;172    box-shadow: none !important;173}174.gr-button-lg:hover{175    z-index: 14;176    width: 113px;177    height: 30px;178    left: 0px;179    top: 0px;180    padding: 0px;181    cursor: pointer !important; 182    background: none rgb(66, 133, 244) !important;183    border: none !important;184    text-align: center !important;185    font-size: 14px !important;186    font-weight: 500 !important;187    color: rgb(255, 255, 255) !important;188    line-height: 1 !important;189    border-radius: 6px !important;190    transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;191    box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;192}193 194footer {display:none !important} 195.output-markdown{display:none !important} 196#out_image {height: 22rem !important;}197 198"""199 200with gr.Blocks(title="Right Hand Finger Counting | Data Science Dojo", css=css) as demo:201  with gr.Tabs():202    with gr.TabItem('Upload'):203      with gr.Row():204        with gr.Column():205          img_input = gr.Image(shape=(640,480))206          image_button = gr.Button("Submit")207 208        with gr.Column():209          output = gr.Image(shape=(640,480), elem_id="out_image")210      with gr.Row():211          example_images = gr.Dataset(components=[img_input],samples=[["ex2.jpg"]])212 213    with gr.TabItem('Webcam'):214      with gr.Row():215        with gr.Column():216          img_input2 = gr.Webcam()217          image_button2 = gr.Button("Submit")218 219        with gr.Column():220          output2 = gr.outputs.Image()221 222    image_button.click(fn=count,223        inputs = img_input,224        outputs = output)        225    image_button2.click(fn=count,226        inputs = img_input2,227        outputs = output2)228    example_images.click(fn=set_example_image,inputs=[example_images],outputs=[img_input])229 230 231demo.launch(debug=True)