计算两个TensorFlow检测对象之间的欧几里得距离

时间:2019-06-18 21:13:27

标签: python opencv tensorflow

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

我以前使用HAAR-Cascade和opencv的方法是通过计算脸部中心与手中心之间的欧几里得距离,我使用cvRectangle可以理解它,但是在张量流情况下不使用cvRectangle以及如何告诉程序被检测到的物体是手还是脸。

HAAR方法:


#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")

Tensorflow:

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)

0 个答案:

没有答案