Calculate euclidian distance between two TensorFlow detected object

Viewed 502

I am creating a program that can detect when a hand is being raised using opencv and tensorflow. My previous approach was using HAAR-Cascade and it gives and 'okay' result but I want to explore the possibilities of using tensorflow instead.

My previous method using HAAR-Cascade and opencv is by calculating the euclidian distance between center of face and center of hand and I understand it using cvRectangle but in the case of tensorflow is not using cvRectangle and how do I tell the program the object being detected is hand or face.

HAAR method:


#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 Answers
Related