#finger_count.py
import os
import cv2
import mediapipe as mp
import time
import numpy as np
import math
cap = cv2.VideoCapture("assets/finger.mp4")
# Define the codec and create VideoWriter object
fps = 25.175
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
size = (int(frame_width), int(frame_height))
#print(size)
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
path = 'C:/Users/zchen/PycharmProjects/opencv/googleNet/record'
out = cv2.VideoWriter()
success = out.open(os.path.join(path, "finger count.mov"), fourcc, fps, size, True)
mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils
index_up, middle_up, ring_up, pinky_up, thumb_up = False, False, False, False, False
while True:
ret, frame = cap.read()
if ret == False: break
frame_RGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(frame_RGB)
if results.multi_hand_landmarks:
for handLms in results.multi_hand_landmarks:
"""
for id, lm in enumerate(handLms.landmark):
#print(id, lm)
h, w, c = frame.shape
cx, cy = int(lm.x * w), int(lm.y * h)
#print(id, cx, cy)
#draw circle at thumb tip
if id == 4:
cv2.circle(frame, (cx, cy), 15, (255, 0, 255), cv2.FILLED)
"""
h_marks = handLms.landmark
#print(h_marks[4], h_marks[8])
index_up = h_marks[8].y < h_marks[7].y
middle_up = h_marks[12].y < h_marks[11].y
ring_up = h_marks[16].y < h_marks[15].y
pinky_up = h_marks[20].y < h_marks[19].y
thumb_up = h_marks[4].x < h_marks[3].x
print(index_up, middle_up, ring_up, pinky_up, thumb_up)
mpDraw.draw_landmarks(frame, handLms, mpHands.HAND_CONNECTIONS)
if thumb_up:
cv2.line(frame, (200, 380), (130, 200), (255, 0, 0), 30)
if index_up:
cv2.line(frame, (220, 280), (220, 50), (255, 0, 0), 23)
if middle_up:
cv2.line(frame, (270, 280), (270, 30), (255, 0, 0), 25)
if ring_up:
cv2.line(frame, (320, 200), (320, 40), (255, 0, 0), 22)
if pinky_up:
cv2.line(frame, (370, 280), (370, 90), (255, 0, 0), 15)
cv2.rectangle(frame, (200, 200), (380, 400), (255, 0, 0), cv2.FILLED)
cv2.imshow("frame", frame)
out.write(frame)
if cv2.waitKey(1) == ord('q'):
break
if cv2.waitKey(1) == ord('p'):
cv2.waitKey(-1) # wait until any key is pressed
cap.release()
cv2.destroyAllWindows()
reference:
No comments:
Post a Comment