From f37210e5792d609829f4827e32e1ee38bcfaf093 Mon Sep 17 00:00:00 2001 From: kicap Date: Thu, 18 Jul 2024 19:02:38 +0800 Subject: [PATCH] remove several index/coordinates --- app3.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/app3.py b/app3.py index ce40255..43aad42 100644 --- a/app3.py +++ b/app3.py @@ -82,7 +82,7 @@ def generate_frames(stat): # Convert palm center to pixel coordinates h, w, _ = frame.shape palm_center_px = int(palm_center_x * w) - palm_center_py = int(palm_center_y * h) - 90 # Shift the center point downwards by 20 pixels + palm_center_py = int(palm_center_y * h) - 90 # Shift the center point downwards by 90 pixels # Draw a circle at the palm center cv2.circle(frame, (palm_center_px, palm_center_py), 10, (255, 0, 0), -1) # Increase the radius to 10 @@ -91,7 +91,7 @@ def generate_frames(stat): gesture = None if is_pointing_gesture(landmarks): gesture = "Pointing" - # check the idx of the hand + # Check the idx of the hand pointing_hand = hand_landmarks.landmark else: gesture = "Open Palm" @@ -109,12 +109,14 @@ def generate_frames(stat): if open_palm: finger_tip = pointing_hand[mp_hands.HandLandmark.INDEX_FINGER_TIP] + # Indices to exclude + excluded_indices = {1, 3, 5, 6, 10, 13, 16, 17, 18} distances = [np.sqrt((landmark.x - finger_tip.x) ** 2 + - (landmark.y - finger_tip.y) ** 2) for landmark in open_palm] + (landmark.y - finger_tip.y) ** 2) + for i, landmark in enumerate(open_palm) if i not in excluded_indices] - ############# ini utk kira palm center ########## # - # calculate the finger tip distance from palm center - # Calculate the Euclidean distance between the finger tip and the palm center + ############# ini utk kira palm center ########## + # Calculate the finger tip distance from palm center finger_tip_distance = np.sqrt((finger_tip.x - palm_center_x) ** 2 + (finger_tip.y - palm_center_y) ** 2) @@ -122,24 +124,20 @@ def generate_frames(stat): threshold_distance = 0.34 threshold_distance2 = 0.37 - # Print the finger tip distance print(finger_tip_distance) # Check if the finger tip is close to the palm center within the threshold distance - if finger_tip_distance > threshold_distance and finger_tip_distance < threshold_distance2: + if threshold_distance < finger_tip_distance < threshold_distance2: the_image_path = all_data[21]["image_path"] the_name = all_data[21]["name"] the_keterangan = all_data[21]["keterangan"] - # closest_landmark_name = landmark_names[21] - cv2.putText(frame, f'Closest Landmark: Tengah Telapak Tangan', + cv2.putText(frame, 'Closest Landmark: Tengah Telapak Tangan', (int(finger_tip.x * w), int(finger_tip.y * h)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) ########## ending palm center ########## elif distances: closest_landmark_idx = np.argmin(distances) - # print("ini index", closest_landmark_idx) - the_image_path = all_data[closest_landmark_idx]["image_path"] the_name = all_data[closest_landmark_idx]["name"] the_keterangan = all_data[closest_landmark_idx]["keterangan"] @@ -154,15 +152,16 @@ def generate_frames(stat): cv2.putText(frame, 'No open palm detected', (cx, cy - 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA) - # Draw landmarks on the frame - draw_landmarks_no_lines(frame, hand_landmarks, dot_drawing_spec) + # Draw landmarks on the frame, excluding specific indices + excluded_indices = {1, 3, 5, 6, 10, 13, 16, 17, 18} + for i, landmark in enumerate(hand_landmarks.landmark): + if i not in excluded_indices: + cv2.circle(frame, (int(landmark.x * w), int(landmark.y * h)), 10, (255, 0, 0), -1) else: the_image_path = "gambar/tutorial.jpg" the_name = None the_keterangan = "Posisikan satu tangan kepada menunjuk dan tangan lainnya kepada terbuka dan tunjukkan ke arah titik tangan" - - # Convert the frame to bytes ret, buffer = cv2.imencode('.jpg', frame) frame = buffer.tobytes() @@ -174,6 +173,7 @@ def generate_frames(stat): + @app.route('/') def index(): return render_template('index2.html')