回到顶部 回到顶部

#智造#人工智能手指识别 简单

头像 gada888 2021.12.14 331 3

这个项目用python语言写的手指识别,更进一步的实际应用会考虑用在手语识别上。程序会移植到单片机上,并且加上语音功能。前期是做一个测试,可以完成的相似应用还可以有很多。测试结果还是令人满意的,无论是运行速度还是识别精确度都是ok的。

 

project-image

代码运行环境是pycharm。之后会测试visual studio环境。代码在运行前要先安装opencv-python和mediapipe两个支持库。

project-image

代码似乎对初学者有点不大友好,注释不大好写,内容较专业,用视频解释可能会好些,以后会考虑在视频中去做解释。

代码
import cv2
import mediapipe as mp
import time

mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands = 1,min_detection_confidence=0.8, min_tracking_confidence=0.8)
drawing_spec = mp_drawing.DrawingSpec(color=(255, 255, 255),thickness=8, circle_radius=8)

tipIds = [4, 8, 12, 16, 20]
cap = cv2.VideoCapture(0)
while cap.isOpened():
    prev_time = time.time()
    success, frame = cap.read()
    if not success:
        print("Ignoring empty camera frame.")
        continue
    image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    results = hands.process(image)
    lmList = []
    if results.multi_hand_landmarks:
        for hand_landmarks in results.multi_hand_landmarks:
            mp_drawing.draw_landmarks(frame, hand_landmarks,mp_hands.HAND_CONNECTIONS,drawing_spec,drawing_spec)

            for id, lm in enumerate(hand_landmarks.landmark):
                h, w, c = frame.shape
                cx, cy = int(lm.x * w), int(lm.y * h)
                lmList.append([id, cx, cy])
                # print(lmList)
                cv2.circle(frame, (cx, cy), 8, (38, 107, 170), cv2.FILLED)
                if id == 3 or id == 4:
                    cv2.circle(frame, (cx, cy), 5, (0, 0, 255), cv2.FILLED)
                    cv2.putText(frame,str(id),(cx+8, cy),cv2.FONT_HERSHEY_COMPLEX,.6,(0,0,255),1)
            if len(lmList) != 0:
                fingers = []
                # Thumb
                if lmList[tipIds[0]][1] > lmList[tipIds[0] - 1][1]:
                    print(lmList[tipIds[0]][1], lmList[tipIds[0] - 1][1])
                    fingers.append(1)
                else:
                    fingers.append(0)
                # 4 Fingers
                for id in range(1, 5):
                    if lmList[tipIds[id]][2] < lmList[tipIds[id] - 2][2]:
                        fingers.append(1)
                    else:
                        fingers.append(0)
                totalFingers = fingers.count(1)
                print(totalFingers)
    else:
        totalFingers = 0

    cv2.rectangle(frame, (6, 121), (114, 229), (255, 255, 255), cv2.FILLED)
    cv2.rectangle(frame, (10, 125), (110, 225), (0, 0, 0), cv2.FILLED)
    cv2.putText(frame, str(totalFingers), (30, 210), cv2.FONT_HERSHEY_PLAIN, 6, (0, 0, 255), 8)
    cv2.putText(frame, 'fps:'+str(int(1 / (time.time() - prev_time))), (3, 40), cv2.FONT_HERSHEY_PLAIN, 3, (0, 0, 255), 3)
    print(1 / (time.time() - prev_time))
    cv2.imshow('finger counter', frame)
    if cv2.waitKey(1) & 0xFF == 27:
        break
cap.release()
cv2.destroyAllWindows()

评论

user-avatar
  • 轩轩1

    轩轩12022.07.05

    这个需要硬件吗?

    0
    • 轩轩1

      轩轩12022.07.05

      这个需要硬件吗?

      0
      • DFBJoKxrFU9

        DFBJoKxrFU92022.05.11

        明白了

        0