diff --git a/main.py b/main.py new file mode 100644 index 0000000..a6396f6 --- /dev/null +++ b/main.py @@ -0,0 +1,42 @@ +import cv2 +import numpy as np +import os + + +def main(): + cap = cv2.VideoCapture(0) + x, y, w, h = 300, 50, 350, 350 + + while (cap.isOpened()): + ret, img = cap.read() + img = cv2.flip(img, 1) + hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) + mask2 = cv2.inRange(hsv, np.array([2, 50, 60]), np.array([25, 150, 255])) + res = cv2.bitwise_and(img, img, mask=mask2) + gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY) + median = cv2.GaussianBlur(gray, (5, 5), 0) + + kernel_square = np.ones((5, 5), np.uint8) + dilation = cv2.dilate(median, kernel_square, iterations=2) + opening = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel_square) + ret, thresh = cv2.threshold(opening, 30, 255, cv2.THRESH_BINARY) + + thresh = thresh[y:y + h, x:x + w] + contours = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1] + if len(contours) > 0: + contour = max(contours, key=cv2.contourArea) + if cv2.contourArea(contour) > 2500: + x1, y1, w1, h1 = cv2.boundingRect(contour) + handImage = thresh[y1:y1 + h1, x1:x1 + w1] + handImage = cv2.resize(handImage, (50, 50)) + cv2.imshow("Hand", handImage) + print(len(np.array(handImage)[0][0])) + + + cv2.imshow("Frame", img) + cv2.imshow("Contours", thresh) + k = cv2.waitKey(10) + if k == 27: + break + +main() \ No newline at end of file