Python Forum
Newbie.... run for cover. OpenCV question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie.... run for cover. OpenCV question
#1
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:

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
buran write Apr-12-2023, 12:22 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Please post your code in python tags. (the python button)
Reply
#3
Ahh... there is a Python button! I only saw the post code icon
Now I know. Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 706 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 696 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Newbie question about switching between files - Python/Pycharm Busby222 3 623 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  numpy newbie question bcwilly_ca 4 1,195 Feb-10-2023, 05:55 PM
Last Post: jefsummers
  Question from complete python's newbie Davicom 3 2,381 Jun-09-2021, 06:09 PM
Last Post: bowlofred
  Newbie question about running Python via GUI on OSX ejwjohn 8 3,527 Feb-05-2021, 03:20 PM
Last Post: Larz60+
  super newbie question: escape character tsavoSG 3 2,485 Jan-13-2021, 04:31 AM
Last Post: tsavoSG
  newbie question....importing a created class ridgerunnersjw 5 2,679 Oct-01-2020, 07:59 PM
Last Post: ridgerunnersjw
  Dumb newbie question JonEdward 5 3,278 Jul-22-2020, 10:06 PM
Last Post: snippsat
  Newbie Question juljan 4 2,805 Jan-13-2020, 03:07 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020