You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.1 KiB
Python
80 lines
2.1 KiB
Python
import cv2
|
|
import mediapipe as mp
|
|
|
|
import analysisPoint as mp_drawing
|
|
mp_holistic = mp.solutions.holistic
|
|
import numpy as np
|
|
|
|
class MediapipeProcess:
|
|
|
|
def mediapipe_det(image,holistic):
|
|
|
|
'''
|
|
调用模型推理获得检测结果
|
|
'''
|
|
|
|
image.flags.writeable = False
|
|
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
|
results = holistic.process(image)
|
|
|
|
return results
|
|
|
|
def get_analysis_result(image,results):
|
|
|
|
'''
|
|
images: 检测的图片
|
|
results: 图片的检测结果
|
|
对上述结果进行分析
|
|
'''
|
|
|
|
image.flags.writeable = True
|
|
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
|
|
|
# face_result = mp_drawing.draw_landmarks(
|
|
# image,
|
|
# results.face_landmarks,
|
|
# mp_holistic.FACEMESH_CONTOURS)
|
|
|
|
right_hand_result = mp_drawing.draw_landmarks(
|
|
image,
|
|
results.right_hand_landmarks,
|
|
mp_holistic.HAND_CONNECTIONS)
|
|
|
|
left_hand_result = mp_drawing.draw_landmarks(
|
|
image,
|
|
results.left_hand_landmarks,
|
|
mp_holistic.HAND_CONNECTIONS)
|
|
|
|
# face_bbox = MediapipeProcess.point_to_bbox(face_result)
|
|
right_hand_bbox = MediapipeProcess.point_to_bbox(right_hand_result)
|
|
left_hand_bbox = MediapipeProcess.point_to_bbox(left_hand_result)
|
|
|
|
result_dict = {'hand_bbox':[right_hand_bbox,left_hand_bbox]}
|
|
|
|
return result_dict
|
|
|
|
def point_to_bbox(result_list):
|
|
|
|
'''
|
|
根据关键点坐标,获取坐标点的最小外接矩形
|
|
'''
|
|
|
|
result_array = np.array(result_list)
|
|
|
|
if result_array.all():
|
|
|
|
rect = cv2.minAreaRect(result_array) # 得到最小外接矩形的(中心(x,y), (宽,高), 旋转角度)
|
|
bbox = cv2.boxPoints(rect) # 获取最小外接矩形的4个顶点坐标(ps: cv2.boxPoints(rect) for OpenCV 3.x)
|
|
bbox = np.int0(bbox)
|
|
bbox=bbox.tolist()
|
|
|
|
left_top = [min(bbox, key=lambda p: p[0])[0], min(bbox, key=lambda p: p[1])[1]]
|
|
right_bottom = [max(bbox, key=lambda p: p[0])[0], max(bbox, key=lambda p: p[1])[1]]
|
|
|
|
bbox_list = left_top + right_bottom
|
|
|
|
|
|
return bbox_list
|
|
|
|
|