首页 > 解决方案 > 计算两个 TensorFlow 检测到的对象之间的欧几里得距离

问题描述

我正在创建一个程序,该程序可以使用 opencv 和 tensorflow 检测何时举手。我以前的方法是使用 HAAR-Cascade,它给出了“好的”结果,但我想探索使用 tensorflow 的可能性。

我之前使用 HAAR-Cascade 和 opencv 的方法是通过计算人脸中心和手心之间的欧几里德距离,我使用 cvRectangle 来理解它,但是在 tensorflow 的情况下不使用 cvRectangle 以及如何告诉程序被检测到的对象是手还是脸。

哈尔方法:


#detect faces and draw bounding box
face_gray = cv2.cvtColor(masked_img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(face_gray, 1.3, 5)
for (x_f,y_f,w_f,h_f) in faces:
    cv2.rectangle(masked_img,(x_f,y_f),(x_f+w_f,y_f+h_f),(0,255,0),2)
    cv2.rectangle(sourceImage,(x_f,y_f),(x_f+w_f,y_f+h_f),(0,255,0),2)

#detect hands and draw bounding box
hands = hand_cascade.detectMultiScale(masked_img, 1.3, 5)
for (x_h,y_h,w_h,h_h) in hands:
    cv2.rectangle(masked_img,(x_h,y_h),(x_h+w_h,y_h+h_h),(0,0,255),2)
    cv2.rectangle(sourceImage,(x_h,y_h),(x_h+w_h,y_h+h_h),(0,0,255),2)

#detect hand-raising
#euclidean distance between center of face and center of hand must be less than 2 times the width of face
#if fulfilled draw another bounding box
for (x_f,y_f,w_f,h_f) in faces:
    for (x_h,y_h,w_h,h_h) in hands:
        dx = math.fabs((x_f+0.5*w_f)-(x_h+0.5*w_h))
        dy = math.fabs((y_f+0.5*h_f)-(y_h+0.5*h_h))

        if (dx <= 2*w_f):
            if dy <= 2*h_f:
                cv2.rectangle(masked_img,(x_f-w_f,y_f-w_f),(x_f+2*w_f,y_f+2*h_f),(255,0,0,),2)
                cv2.rectangle(sourceImage,(x_f-w_f,y_f-w_f),(x_f+2*w_f,y_f+2*h_f),(255,0,0,),2)
                print("Hand Raised Detected")

张量流:

vis_util.visualize_boxes_and_labels_on_image_array(
    frame,
    np.squeeze(boxes),
    np.squeeze(classes).astype(np.int32),
    np.squeeze(scores),
    category_index,
    use_normalized_coordinates=True,
    line_thickness=8,
    min_score_thresh=0.60)

标签: pythonopencvtensorflow

解决方案


推荐阅读