-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_cascade.py
127 lines (75 loc) · 2.18 KB
/
custom_cascade.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
1) veri seti:
n,p
2) cascade programı indir
3) cascade oluştur
4) cascade kullanarak tespit algoritması yaz
"""
import cv2
import os
# resim depo klasörü
path = "images"
# resim boyutu
imgWidth = 180
imgHeight = 120
# video capture
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
cap.set(10, 180)
global countFolder
def saveDataFunc():
global countFolder
countFolder = 0
while os.path.exists(path + str(countFolder)):
countFolder += 1
os.makedirs(path+str(countFolder))
saveDataFunc()
count = 0
countSave = 0
while True:
success, img = cap.read()
if success:
img = cv2.resize(img, (imgWidth, imgHeight))
if count % 5 == 0:
cv2.imwrite(path+str(countFolder)+"/"+str(countSave)+"_"+".png",img)
countSave += 1
print(countSave)
count += 1
cv2.imshow("Image",img)
if cv2.waitKey(1) & 0xFF == ord("q"): break
cap.release()
cv2.destroyAllWindows()
#%%
import cv2
objectName = "buraya nesneni yaz"
frameWidth = 280
frameHeight = 360
color = (255,0,0)
cap = cv2.VideoCapture(0)
cap.set(3,frameWidth)
cap.set(4,frameHeight)
def empty(a):pass
# trackbar
cv2.namedWindow("Sonuc")
cv2.resizeWindow("Sonuc", frameWidth, frameHeight + 100)
cv2.createTrackbar("Scale","Sonuc",400,1000,empty)
cv2.createTrackbar("Neighbor","Sonuc",4,50,empty)
# cascade classifier
cascade = cv2.CascadeClassifier("cascade.xml")
while True:
# read img
success, img = cap.read()
if success:
# convert bgr2gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# detection parameters
scaleVal = 1 + (cv2.getTrackbarPos("Scale","Sonuc")/1000)
neighbor = cv2.getTrackbarPos("Neighbor","Sonuc")
# detectiom
rects = cascade.detectMultiScale(gray, scaleVal, neighbor)
for (x,y,w,h) in rects:
cv2.rectangle(img, (x,y),(x+w,y+h), color, 3)
cv2.putText(img, objectName, (x, y-5), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, color, 2)
cv2.imshow("Sonuc", img)
if cv2.waitKey(1) & 0xFF == ord("q"): break