remove several index/coordinates

This commit is contained in:
kicap 2024-07-18 19:02:38 +08:00
parent 0d177710bb
commit f37210e579
1 changed files with 16 additions and 16 deletions

32
app3.py
View File

@ -82,7 +82,7 @@ def generate_frames(stat):
# Convert palm center to pixel coordinates # Convert palm center to pixel coordinates
h, w, _ = frame.shape h, w, _ = frame.shape
palm_center_px = int(palm_center_x * w) 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 # 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 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 gesture = None
if is_pointing_gesture(landmarks): if is_pointing_gesture(landmarks):
gesture = "Pointing" gesture = "Pointing"
# check the idx of the hand # Check the idx of the hand
pointing_hand = hand_landmarks.landmark pointing_hand = hand_landmarks.landmark
else: else:
gesture = "Open Palm" gesture = "Open Palm"
@ -109,12 +109,14 @@ def generate_frames(stat):
if open_palm: if open_palm:
finger_tip = pointing_hand[mp_hands.HandLandmark.INDEX_FINGER_TIP] 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 + 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 ########## # ############# ini utk kira palm center ##########
# calculate the finger tip distance from palm center # Calculate the finger tip distance from palm center
# Calculate the Euclidean distance between the finger tip and the palm center
finger_tip_distance = np.sqrt((finger_tip.x - palm_center_x) ** 2 + finger_tip_distance = np.sqrt((finger_tip.x - palm_center_x) ** 2 +
(finger_tip.y - palm_center_y) ** 2) (finger_tip.y - palm_center_y) ** 2)
@ -122,24 +124,20 @@ def generate_frames(stat):
threshold_distance = 0.34 threshold_distance = 0.34
threshold_distance2 = 0.37 threshold_distance2 = 0.37
# Print the finger tip distance # Print the finger tip distance
print(finger_tip_distance) print(finger_tip_distance)
# Check if the finger tip is close to the palm center within the threshold 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_image_path = all_data[21]["image_path"]
the_name = all_data[21]["name"] the_name = all_data[21]["name"]
the_keterangan = all_data[21]["keterangan"] the_keterangan = all_data[21]["keterangan"]
# closest_landmark_name = landmark_names[21] cv2.putText(frame, 'Closest Landmark: Tengah Telapak Tangan',
cv2.putText(frame, f'Closest Landmark: Tengah Telapak Tangan',
(int(finger_tip.x * w), int(finger_tip.y * h)), (int(finger_tip.x * w), int(finger_tip.y * h)),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
########## ending palm center ########## ########## ending palm center ##########
elif distances: elif distances:
closest_landmark_idx = np.argmin(distances) closest_landmark_idx = np.argmin(distances)
# print("ini index", closest_landmark_idx)
the_image_path = all_data[closest_landmark_idx]["image_path"] the_image_path = all_data[closest_landmark_idx]["image_path"]
the_name = all_data[closest_landmark_idx]["name"] the_name = all_data[closest_landmark_idx]["name"]
the_keterangan = all_data[closest_landmark_idx]["keterangan"] 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, cv2.putText(frame, 'No open palm detected', (cx, cy - 50), cv2.FONT_HERSHEY_SIMPLEX, 1,
(0, 0, 255), 2, cv2.LINE_AA) (0, 0, 255), 2, cv2.LINE_AA)
# Draw landmarks on the frame # Draw landmarks on the frame, excluding specific indices
draw_landmarks_no_lines(frame, hand_landmarks, dot_drawing_spec) 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: else:
the_image_path = "gambar/tutorial.jpg" the_image_path = "gambar/tutorial.jpg"
the_name = None the_name = None
the_keterangan = "Posisikan satu tangan kepada menunjuk dan tangan lainnya kepada terbuka dan tunjukkan ke arah titik tangan" the_keterangan = "Posisikan satu tangan kepada menunjuk dan tangan lainnya kepada terbuka dan tunjukkan ke arah titik tangan"
# Convert the frame to bytes # Convert the frame to bytes
ret, buffer = cv2.imencode('.jpg', frame) ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes() frame = buffer.tobytes()
@ -174,6 +173,7 @@ def generate_frames(stat):
@app.route('/') @app.route('/')
def index(): def index():
return render_template('index2.html') return render_template('index2.html')