Apr-12-2023, 11:38 AM
Hello all
I have jumped in the deep end of Python. Done a little programming before, but not much in Python.
I found some OpenCV skeleton tracking code and I have been adapting it to see what I can achieve.
Now it's a mess! First attempt and all that.
SORRY.... The post code facility doesn't work (in Chrome or Edge?) So all the formatting is messed up Fix for that?
I was trying to get it to detect when I lent over (head down towards shoulders). It does work, but I am highly aware that it is very crude.
My questions.... (which might be solved by guidance to a decent OpenCV online learning source?)
1. When you move in and out of webcam view, the 'try' loop will jump to the 'except' routine. I just printed a tracking error.
Is there a better way of handling that?
Can you identify when all/certain landmarks are within the camera view? (and therefore, I can start checking for their position)
2. My 'check for all attached webcams' routine just hangs. Might be the external webcam I was using. Currently commented out, but anything obvious?
3. How does OpenCV identify a target? If I play a video (instead of a webcam feed) of two people dancing, it will always follow the one on the left, until
they cross over, then it starts to track the one on the right. What determines the target?
4. Is it possible to identify multiple targets with OpenCV, and skeleton track just the one say furthest on the left?
Sorry it all seems a bit stabby in the dark, but jumping in the deep end seems to work best for me when learning stuff.
Should add I am running this on my laptop (not a Pi)
Tracking markers:
I have jumped in the deep end of Python. Done a little programming before, but not much in Python.
I found some OpenCV skeleton tracking code and I have been adapting it to see what I can achieve.
Now it's a mess! First attempt and all that.
SORRY.... The post code facility doesn't work (in Chrome or Edge?) So all the formatting is messed up Fix for that?
I was trying to get it to detect when I lent over (head down towards shoulders). It does work, but I am highly aware that it is very crude.
My questions.... (which might be solved by guidance to a decent OpenCV online learning source?)
1. When you move in and out of webcam view, the 'try' loop will jump to the 'except' routine. I just printed a tracking error.
Is there a better way of handling that?
Can you identify when all/certain landmarks are within the camera view? (and therefore, I can start checking for their position)
2. My 'check for all attached webcams' routine just hangs. Might be the external webcam I was using. Currently commented out, but anything obvious?
3. How does OpenCV identify a target? If I play a video (instead of a webcam feed) of two people dancing, it will always follow the one on the left, until
they cross over, then it starts to track the one on the right. What determines the target?
4. Is it possible to identify multiple targets with OpenCV, and skeleton track just the one say furthest on the left?
Sorry it all seems a bit stabby in the dark, but jumping in the deep end seems to work best for me when learning stuff.
Should add I am running this on my laptop (not a Pi)
Tracking markers:
Output:0 nose
1 left_eye_inner
2 left_eye
3 left_eye_outer
4 right_eye_inner
5 right_eye
6 right_eye_outer
7 left_ear
8 right_ear
9 mouth_left
10 mouth_right
11 left_shoulder
12 right shoulder
13 left_elbow
14 right_elbow
15 left_wrist
16 right_wrist
17 left_pinky
18 right_pinky
19 left_index
20 right_index
21 left_thumb
22 right_thumb
23 left_hip
24 right_hip
25 left_knee
26 right_knee
27 left_ankle
28 right_ankle
29 left_heel
30 right_heel
31 left_foot_index
32 right_foot_index
############################################## Human skeleton tracking routine ############################################## """ # TechVidvan Human pose estimator # import necessary packages import cv2 import mediapipe as mp mp_drawing = mp.solutions.drawing_utils # initialize Pose estimator mp_pose = mp.solutions.pose pose = mp_pose.Pose( min_detection_confidence=0.5, # If confidence if >0.5, then start tracking min_tracking_confidence=0.5) # If below min tracking, then go back to detection #static_image_mode = False # Whether to treat input images as static images or video stream - Default False #model_complexit = 1 # Specify complexity of the pose landmark. 0, 1 or 2. Default = 1 #smooth_landmarks = True # Reduces jitter in the prediction. Default is True cap = cv2.VideoCapture(0) # Use Webcam feed (built-in) #print("Searching for connected cameras...") # Search all ports for webcams. ## This can take some time ## NOT YET WORKING #valid_cams = [] #for i in range(8): #cap = cv2.VideoCapture(i) #if cap is None or not cap.isOpened(): #print('No video source available on port: ', i) #else: #valid_cams.append(i) #caps = [] #for webcam in valid_cams: #caps.append(cv2.VideoCapture(webcam)) #cap = cv2.VideoCapture(webcam) errorflag = False bowflag = False while cap.isOpened(): # read frame from capture object _, frame = cap.read() try: RGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # convert the frame to RGB format results = pose.process(RGB) # process the RGB frame to get the result #print(results.pose_landmarks) # Print the co-ords of ALL the skeleton landmarks mp_drawing.draw_landmarks( # draw detected skeleton on the frame frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS) for id, lm in enumerate(results.pose_landmarks.landmark): # Create a for loop to identify each skeleton joint h, w, c = frame.shape #print(id, lm) # Print ALL the skeleton joint position values if (id == 0): # Identify the nose cx0, cy0 = int(lm.x * w), int(lm.y * h) # Calculate the actual pixel value nose = cy0 if (id == 11): # Identify the left shoulder cx11, cy11 = int(lm.x * w), int(lm.y * h) # Calculate the actual pixel value lshoulder = cy11 if (id == 12): # Identify the right shoulder cx12, cy12 = int(lm.x * w), int(lm.y * h) # Calculate the actual pixel value rshoulder = cy12 targetrange = int(lshoulder - rshoulder) # Use the distance between the shoulders for ranging if ((((nose < (lshoulder+20)) and (nose > lshoulder)) or ((nose < (rshoulder+20)) and (nose > rshoulder))) and bowflag == False): # Check for a bow print("YOU nodded your head") bowflag = True if ((((nose > (lshoulder+40)) and (nose > lshoulder)) and ((nose > (rshoulder+40)) and (nose > rshoulder))) and bowflag == True): # Reset a bow bowflag = False cv2.imshow('Output', frame) # show the final output liveheight = 0 if errorflag == True: print("Tracking online") errorflag = False except: if errorflag == False: print("Tracking error") errorflag = True # Set the tracking error message flag #break # Disable the frame draw error break if cv2.waitKey(1) == ord('q'): print("Exit routine") break cap.release() cv2.destroyAllWindows()Thanks all