-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathyuz_tespiti.py
More file actions
82 lines (60 loc) · 2.71 KB
/
yuz_tespiti.py
File metadata and controls
82 lines (60 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Python v3, OpenCV v3.4
# -*- coding: utf-8 -*-
#######################################
__file__ = "yuz_tespiti.py"
__author__ = "Mesut Pişkin"
__version__ = "1.0"
__email__ = "mesutpiskin@outlook.com"
#######################################
import numpy as np
import cv2
# Tespit edilen yuz karesinin ne kadar genisletilecegini ifade eder.
shiftValue = 20
# Kamera goruntusunu boyutlandırmak ıcın bu degerler kullanılacak.
resizeX = 460
resizeY = 300
# Basari oranı bu esik degerine gore kıyaslanacak, esikden kucukse o tahmin elenir.
thresholdValue = 0.3
# Model dosyalarini oku ve dnn agini olustur.
dnnNetwork = cv2.dnn.readNetFromCaffe("data/deploy.prototxt.txt", "data/res10SSD.caffemodel")
# Kamerayi baslat.
videoCapture = cv2.VideoCapture(0)
while True:
# Kamera goruntusunu oku.
ret, frame = videoCapture.read()
# Kamera goruntusunu boyutlandir. Daha dusuk cozunurluk daha hızlı sonuclar almanızı saglar (DNN icin gecerli degil).
frame = cv2.resize(frame, (resizeX, resizeY))
# Satir sutun sayısını al (genislik yukseklik).
(h, w) = frame.shape[:2]
# Sinir agi 300x300 seklinde bir input beklemektedir.
# Sinir ağı için görüntüyü 300x300 haline getir ve giriş için hazırla.
dnnBlobObject = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0,
(300, 300), (104.0, 177.0, 123.0))
# Goruntuyu sinir agina input olarak olarak ver.
dnnNetwork.setInput(dnnBlobObject)
resultDetections = dnnNetwork.forward()
# Tum sinir agi ciktilari
for i in range(0, resultDetections.shape[2]):
# Sinir agi buldugu nesne icin ne kadarlık bir basari oranı belirlemis
confidence = resultDetections[0, 0, i, 2]
# Basari oranı thresholdValue den kucukse o tahmini kabul etme.
if confidence < thresholdValue:
continue
# Tespit edilen nesnenin, int tipinde (x1, y1) (genislik, yukseklik) koordinatları .
resultArea = resultDetections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = resultArea.astype("int")
# DNN sonucu dogruluk oranı yuzdelik formata cevrilir.
percent = "{:.2f}%".format(confidence * 100)
# Koordinatlara ve genislik yukseklik verisine gore nesne dortgen icerisinde isaretlenecek.
y = startY - 10 if startY - 10 > 10 else startY + 10
cv2.rectangle(frame, (startX - shiftValue, startY - shiftValue), (endX + shiftValue, endY + shiftValue),(0, 255, 255), 1)
cv2.putText(frame, percent, (startX - shiftValue , y - shiftValue),cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 255), 1)
# Sonucu frame icerisinde goruntule.
cv2.imshow("DNN ILE YUZ TESPITI", frame)
# ESC ile uygulamayi kapat.
key = cv2.waitKey(1) & 0xFF
if key == 27:
break
# Pencereleri ve kamera nesnesini sonlandir.
cv2.destroyAllWindows()
videoCapture.stop()