Recently I developed a gesture processing project (you can learn it even if you have zero foundation, it is for those with zero foundation). I will briefly review the principle here. Generally speaking, it is relatively simple. The main knowledge used is opencv and basic python syntax. Image processing basics.
Final result:
Get video (camera)
There’s not much to say about this part, it’s just about getting the camera.
cap = cv2.VideoCapture("C:/Users/lenovo/Videos/1.mp4")#Read file #cap = cv2.VideoCapture(0)#Read camera while(True): ret, frame = cap.read() key = cv2.waitKey(50) & 0xFF if key == ord('q'): break cap.release() cv2.destroyAllWindows()
Skin color detection
The elliptical skin color detection model is used here.
In the RGB space, the skin color of a human face is greatly affected by brightness, so it is difficult to separate skin color points from non-skin color points. That is to say, after processing in this space, the skin color points are discrete points with many non-skin color points embedded in them. , which brings difficulties to skin color area calibration (face calibration, eyes, etc.).
If you convert RGB to YCrCb space, you can ignore the influence of Y (brightness), because this space is very little affected by brightness, and skin colors will produce good clustering.
In this way, the three-dimensional space will be two-dimensional CrCb, and the skin color points will form a certain shape. For example, for a face, you will see a face area, and for an arm, you will see the shape of an arm.
def A(img): YCrCb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB) #Convert to YCrCb space (y,cr,cb) = cv2.split(YCrCb) #Split the Y, Cr, Cb values cr1 = cv2.GaussianBlur(cr, (5,5), 0) _, skin = cv2.threshold(cr1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) #Ostu processing res = cv2.bitwise_and(img,img, mask = skin) return res
Contour processing
Contour processing mainly uses two functions, cv2.findContours and cv2.drawContours. The usage of these two functions is easy to find. The main problem in this part is that there are many extracted contours, but We only need the outline of the hand, so we’ll use the sorted function to find the largest outline.
def B(img): #binaryimg = cv2.Canny(Laplacian, 50, 200) #Binarization, canny detection h = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) #Find contours contour = h[0] contour = sorted(contour, key = cv2.contourArea, reverse=True)#Sort by contour area area #contourmax = contour[0][:, 0, :]#Retain the coordinates of the contour point with the largest area bg = np.ones(dst.shape, np.uint8) *255#Create a white curtain ret = cv2.drawContours(bg,contour[0],-1,(0,0,0),3) #Draw black contours return ret
All code
""" Read frames from video and save them as pictures""" import cv2 import numpy as np cap = cv2.VideoCapture("C:/Users/lenovo/Videos/1.mp4")#Read file #cap = cv2.VideoCapture(0)#Read camera #skintest def A(img): YCrCb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB) #Convert to YCrCb space (y,cr,cb) = cv2.split(YCrCb) #Split the Y, Cr, Cb values cr1 = cv2.GaussianBlur(cr, (5,5), 0) _, skin = cv2.threshold(cr1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) #Ostu processing res = cv2.bitwise_and(img,img, mask = skin) return res def B(img): #binaryimg = cv2.Canny(Laplacian, 50, 200) #Binarization, canny detection h = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) #Find contours contour = h[0] contour = sorted(contour, key = cv2.contourArea, reverse=True)#Sort by contour area area #contourmax = contour[0][:, 0, :]#Retain the coordinates of the contour point with the largest area bg = np.ones(dst.shape, np.uint8) *255#Create a white curtain ret = cv2.drawContours(bg,contour[0],-1,(0,0,0),3) #Draw black contours return ret while(True): ret, frame = cap.read() #The following three lines can be adjusted according to your own computer src = cv2.resize(frame,(400,350), interpolation=cv2.INTER_CUBIC)#Window size cv2.rectangle(src, (90, 60), (300, 300), (0, 255, 0))#Frame the interception position roi = src[60:300, 90:300] # Get gesture block diagram res = A(roi) # Perform skin color detection cv2.imshow("0",roi) gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY) dst = cv2.Laplacian(gray, cv2.CV_16S, ksize = 3) Laplacian = cv2.convertScaleAbs(dst) contour = B(Laplacian)#Contour processing cv2.imshow("2",contour) key = cv2.waitKey(50) & 0xFF if key == ord('q'): break cap.release() cv2.destroyAllWindows()
Interested friends will receive a complete set of Python learning materials, including interview questions, resume information, etc. See below for details.
1. Learning routes in all directions of Python
The technical points in all directions of Python have been compiled to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the following knowledge points to ensure that you learn more comprehensively.
2. Essential development tools for Python
The tools have been organized for you, and you can get started directly after installation!
3. Latest Python study notes
When I learn a certain basic and have my own understanding ability, I will read some books or handwritten notes compiled by my seniors. These notes record their understanding of some technical points in detail. These understandings are relatively unique and can be learned. to a different way of thinking.
4. Python video collection
Watch a comprehensive zero-based learning video. Watching videos is the fastest and most effective way to learn. It is easy to get started by following the teacher’s ideas in the video, from basic to in-depth.
5. Practical cases
What you learn on paper is ultimately shallow. You must learn to type along with the video and practice it in order to apply what you have learned into practice. At this time, you can learn from some practical cases.
6. Interview Guide
Resume template
If there is any infringement, please contact us for deletion