Login
cam.py
Raw
Expires in -1756322410 second
import cv2
import face_recognition
import mediapipe as mp
import time


# Kamera bağlantısı
#RTSP_URL = "rtsp://admin:[email protected]:554/cam/realmonitor?channel=1&subtype=1&unicast=true&proto=Onvif"

known_image = face_recognition.load_image_file("face.jpg")
known_encoding = face_recognition.face_encodings(known_image)[0]

# Tanınan yüzler
known_faces = [known_encoding]

# El vucut pozisyonları
mp_pose = mp.solutions.pose
mp_hands = mp.solutions.hands
mp_draw = mp.solutions.drawing_utils

pose = mp_pose.Pose()
hands = mp_hands.Hands(static_image_mode=False, max_num_hands=1)

# Kamera oluşturuluyor
cap = cv2.VideoCapture(RTSP_URL)
#cap = cv2.VideoCapture(0)

# Hangi aralıkta denetleme yapılacak
frame_count = 0
recognition_interval = 15

cam_width = 0
cam_height = 0

def crop_zone(frame):
x1, y1 = face_zone_top_left
x2, y2 = face_zone_bottom_right
return frame[y1:y2, x1:x2]

def distance(p1, p2):
return ((p1.x - p2.x)**2 + (p1.y - p2.y)**2)**0.5

if not cap.isOpened():
print("Video açılamadı.")
else:
# Genişlik (en) ve yükseklik (boy) bilgilerini al
cam_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # En (pixel cinsinden)
cam_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # Boy (pixel cinsinden)

print(f"Çözünürlük: {cam_width} x {cam_height}")

#cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1152)
#cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 648)

# Kamera boyutlandırma
new_width = int(cam_width * 0.5)
new_height = int(cam_height * 0.5)

# Yüz tarama bölgesi (X, Y) koordinatları
face_zone_top_left = (int(new_width*0.2), int(new_height*0.2))
face_zone_bottom_right = (int(new_width*0.8), int(new_height*0.8))


person_recognized = False

label = ""


while True:

time.sleep(0.1)



ret, frame = cap.read()
if not ret:
break

resized = cv2.resize(frame, (new_width, new_height))

# 1. Çerçeve çiz
#cv2.rectangle(resized, face_zone_top_left, face_zone_bottom_right, (255, 255, 0), 2)

zone_frame = crop_zone(resized)
rgb_zone = cv2.cvtColor(zone_frame, cv2.COLOR_BGR2RGB)

h, w, _ = zone_frame.shape




frame_count += 1
if frame_count % recognition_interval == 0:

frame_count = 0





face_locations = face_recognition.face_locations(zone_frame)

face_encodings = face_recognition.face_encodings(rgb_zone, face_locations)

person_recognized = False
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
match = face_recognition.compare_faces(known_faces, face_encoding)[0]
if match:
person_recognized = True
#print("TANIDIK KISI")
break

label = ""

if person_recognized == True:
rgb_full = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)

pose_results = pose.process(rgb_full)
hand_results = hands.process(rgb_full)

if pose_results.pose_landmarks:
right_shoulder = pose_results.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_SHOULDER]
shoulder_y = right_shoulder.y # Normalized (0-1)

#cv2.circle(zone_frame, (int(right_shoulder.x * w), int(right_shoulder.y * h)), 10, (255, 0, 0), -1)

if hand_results.multi_hand_landmarks:
hand = hand_results.multi_hand_landmarks[0]
wrist = hand.landmark[0]
thumb_tip = hand.landmark[4]
thumb_ip = hand.landmark[3]

handy2 = hand_results.multi_hand_landmarks[0].landmark


# Omuz kontrolü
if wrist.y < shoulder_y:
label += "El omuzun ÜSTÜNDE, "

# Baş parmak yönü kontrolü
if thumb_tip.y < thumb_ip.y:
label += "Başparmak YUKARI"
else:
label += "Başparmak AŞAĞI"


# 2. Parmaklar açık mı?
fingers_open = [
handy2[8].y < handy2[6].y,
handy2[12].y < handy2[10].y,
handy2[16].y < handy2[14].y,
handy2[20].y < handy2[18].y
]

# 3. Parmaklar bitişik mi?
fingers_together = (
distance(handy2[8], handy2[12]) < 0.06 and
distance(handy2[12], handy2[16]) < 0.06 and
distance(handy2[16], handy2[20]) < 0.06
)

fingers_together = True

if all(fingers_open) and fingers_together:
label = "DUR İŞARETİ ALGILANDI"


else:
label += "El omuzun ALTINDA"


print(label)

# Çizim
#mp_draw.draw_landmarks(zone_frame, hand, mp_hands.HAND_CONNECTIONS)


#if person_recognized == True:
#cv2.putText(zone_frame, "TANIDIK KISI", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
#cv2.putText(zone_frame, label, (20, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)

# Görüntüyü göster
cv2.imshow("Izleme", zone_frame)

if cv2.waitKey(1) & 0xFF == ord("q"):
break

cap.release()
cv2.destroyAllWindows()