python - 相机在Opencv中看到时如何计算连接
问题描述
我最近在 opencv 中开始了一个自己的项目。opencv 中的二头肌计数器计算关节(手腕、肘部、肩部),但即使关节不在摄像机视野中,它也会计数。我在谷歌上搜索了几个小时,但没有。有人知道该怎么做吗?请告诉我。
问题 = 计算不在相机视野中的连接。
代码:
import cv2
import mediapipe as mp
import numpy as np
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
def vidcam():
cap = cv2.VideoCapture(0)
titel = 'Biceps Counter' # Titel of app
counter = 0
stage = None
## setup mediapipe instance
with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
while cap.isOpened():
ret, frame = cap.read()
# Recolor image to RGB
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
#make detection
results = pose.process(image)
# Recolor back to BGR
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Extract landmarks
# here try: except:
try:
landmarks = results.pose_landmarks.landmark
# Get coordinates
shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y]
elbow = [landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].x, landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y]
wrist = [landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x, landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y]
# Test here
if landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x in results:
print("Cant find point")
else:
print("find it")
# end test
# Calculate Angle
angle = calculate_angle(shoulder, elbow, wrist)
# Visualize Angle
cv2.putText(image, str(angle),
tuple(np.multiply(elbow, [640, 480]).astype(int)),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA
)
# movement counter
if angle > 150:
stage = 'down'
if angle < 50 and stage == 'down':
stage = 'up'
counter +=1
print(counter)
except:
print("Something when wrong... Try again if not working contact support")
# Box for Counter
cv2.rectangle(image, (40,40), (100,100), (0,0,0), -1)
# Display Counter
cv2.putText(image, 'Bicep Counter', (20,20),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0), 1, cv2.LINE_AA)
cv2.putText(image, str(counter), (65,75),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255,255,255), 2, cv2.LINE_AA)
# Rendering Detections
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
mp_drawing.DrawingSpec(color=(245, 66, 66), thickness=2, circle_radius=2), # Line Blue
mp_drawing.DrawingSpec(color=(245, 66, 230), thickness=2, circle_radius=2), # Joints Pink
)
cv2.imshow(titel, image)
pressedKey = cv2.waitKey(1) & 0xFF
if pressedKey == ord('q'):
print("q is pressed")
break
close()
def calculate_angle(a,b,c):
a = np.array(a) # First -> example joint 1 SHOULDER
b = np.array(b) # Second -> example joint 2 ELBOW
c = np.array(c) # Last -> example joint 3 WRIST
# (value c[y] - b[y]), (value c[x] - b[x])
radians = np.arctan2(c[1]-b[1], c[0]-b[0]) - np.arctan2(a[1]-b[1], a[0]-b[0])
angle = np.abs(radians * 180.0/np.pi)
if angle > 180.0: # Max 180° degree
angle = 360 - angle
return angle
def close():
cap = cv2.VideoCapture(0)
cap.release()
cv2.destroyAllWindows()
在另一个文件中,我像这样调用 vidcam 函数
vidcam()
解决方案
推荐阅读
- wordpress - 有没有办法阻止用户访问所有站点,而不仅仅是 wp-login.php
- php - 如何访问位于 codeigniter 4 中“folder_name/controller_name”的控制器?
- python - 使用 Python 从语义上检测文本块
- android - Expo v31 Android 发布版本不会加载 JS 包(iOS 版本适用于调试和发布配置)
- gem5 - 如何用树构建gem5?
- python - Python Pandas,布尔索引:如何选择具有“真”值的所有行
- amazon-web-services - 在 Auto Scaling AMI 启动中如何在新实例中复制我的私钥文件
- rfid - hf14b raw 命令是否允许我发送和接收 APDU 消息?
- python - Python 线程不能同时工作
- php - Do-While 循环未按要求停止