效果图:
获取视频(摄像头)
def main():
cap = cv2.VideoCapture(0) # Read from camera
while cap.isOpened():
try:
ret, frame = cap.read()
if ret:
# Display the video feed
cv2.imshow("Video Feed", frame)
# Perform gesture recognition
gesture = gesture_recognition(frame)
cv2.imshow("Gesture", gesture)
# Wait for user input to exit
key = cv2.waitKey(50) & 0xFF
if key == ord('q'):
break
except:
break
# Release resources and close windows
cap.release()
cv2.destroyAllWindows()
肤色检测和轮廓处理
def gesture_recognition(frame):
# Crop the video frame to the region of interest
roi = frame[:,:]
# Skin detection
YCrCb = cv2.cvtColor(roi, cv2.COLOR_BGR2YCR_CB)
(y,cr,cb) = cv2.split(YCrCb)
cr1 = cv2.GaussianBlur(cr, (5,5), 0)
_, skin = cv2.threshold(cr1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
res = cv2.bitwise_and(roi, roi, mask = skin)
# Edge detection
gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
dst = cv2.Laplacian(gray, cv2.CV_16S, ksize = 3)
laplacian = cv2.convertScaleAbs(dst)
# Find contours
h = cv2.findContours(laplacian, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
contour = h[0]
contour = sorted(contour, key = cv2.contourArea, reverse=True)
bg = np.ones(laplacian.shape, np.uint8) * 255
ret = cv2.drawContours(bg, contour[0], -1, (0,0,0), 3)
return ret
全部代码
import cv2
import numpy as np
# Function for gesture recognition
def gesture_recognition(frame):
# Crop the video frame to the region of interest
roi = frame[:,:]
# Skin detection
YCrCb = cv2.cvtColor(roi, cv2.COLOR_BGR2YCR_CB)
(y,cr,cb) = cv2.split(YCrCb)
cr1 = cv2.GaussianBlur(cr, (5,5), 0)
_, skin = cv2.threshold(cr1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
res = cv2.bitwise_and(roi, roi, mask = skin)
# Edge detection
gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
dst = cv2.Laplacian(gray, cv2.CV_16S, ksize = 3)
laplacian = cv2.convertScaleAbs(dst)
# Find contours
h = cv2.findContours(laplacian, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
contour = h[0]
contour = sorted(contour, key = cv2.contourArea, reverse=True)
bg = np.ones(laplacian.shape, np.uint8) * 255
ret = cv2.drawContours(bg, contour[0], -1, (0,0,0), 3)
return ret
# Main function
def main():
# cap = cv2.VideoCapture(r"E:\szg.mp4") # Read video file
cap = cv2.VideoCapture(0) # Read from camera
while cap.isOpened():
try:
ret, frame = cap.read()
if ret:
# Display the video feed
cv2.imshow("Video Feed", frame)
# Perform gesture recognition
gesture = gesture_recognition(frame)
cv2.imshow("Gesture", gesture)
# Wait for user input to exit
key = cv2.waitKey(50) & 0xFF
if key == ord('q'):
break
except:
break
# Release resources and close windows
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()