-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry_codes_01.py
52 lines (39 loc) · 1.55 KB
/
try_codes_01.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import cv2
import numpy as np
# Function to detect hand using skin color segmentation
def detect_hand(frame):
# Convert the frame to the HSV color space
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Define the range of skin color in HSV
lower_skin = np.array([0, 20, 70], dtype=np.uint8)
upper_skin = np.array([20, 255, 255], dtype=np.uint8)
# Create a binary mask using the inRange function
mask = cv2.inRange(hsv, lower_skin, upper_skin)
# Apply morphological operations to remove noise
mask = cv2.medianBlur(mask, 5)
mask = cv2.dilate(mask, None, iterations=2)
# Find contours in the mask
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Find the largest contour (assumed to be the hand)
if len(contours) > 0:
max_contour = max(contours, key=cv2.contourArea)
if cv2.contourArea(max_contour) > 10000: # Adjust this value based on your needs
x, y, w, h = cv2.boundingRect(max_contour)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
return frame
# Open the webcam
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
# Detect the hand in the frame
result_frame = detect_hand(frame)
# Display the result
cv2.imshow('Hand Detection', result_frame)
# Exit the loop if the 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the webcam and close all windows
cap.release()
cv2.destroyAllWindows()