import cv2
import mediapipe as mp
import tkinter as tk
from tkinter import ttk
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
# 사용할 카메라 선택
def select_camera():
index = 0
cameras = []
while True:
cap = cv2.VideoCapture(index)
if not cap.read()[0]:
break
else:
cameras.append(index)
cap.release()
index += 1
return cameras
# 카메라 선택 콜백 함수
def on_select(event):
global selected_camera
selected_camera = int(camera_combobox.get())
# 카메라 선택 GUI 생성
def create_gui():
global camera_combobox
root = tk.Tk()
root.title("카메라 선택")
tk.Label(root, text="사용할 카메라를 선택하세요:").pack(pady=10)
cameras = select_camera()
camera_combobox = ttk.Combobox(root, values=cameras)
camera_combobox.pack(pady=10)
camera_combobox.bind("<<ComboboxSelected>>", on_select)
tk.Button(root, text="확인", command=root.quit).pack(pady=10)
root.mainloop()
# 카메라 선택 GUI 생성 및 실행
selected_camera = None
create_gui()
# 선택된 카메라로 비디오 캡처 시작
cap = cv2.VideoCapture(selected_camera)
with mp_hands.Hands(
max_num_hands=2,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as hands:
while cap.isOpened():
success, image = cap.read()
if not success:
print("카메라를 찾을 수 없습니다.")
break
# 성능을 위해 이미지를 쓰지 않음 모드로 변환
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
image.flags.writeable = False
results = hands.process(image)
# 이미지에 손 주석 그리기
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# 이미지 출력
cv2.imshow('MediaPipe Hands', image)
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()