-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtrainer.py
42 lines (38 loc) · 977 Bytes
/
trainer.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
import cv2
import numpy as np
import util as ut
cam=int(raw_input("Enter Camera Index : "))
cap=cv2.VideoCapture(cam)
train_folder=raw_input("Enter Train Folder name : ")
i=1
j=1
name=""
while(cap.isOpened()):
_,img=cap.read()
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,th1 = cv2.threshold(gray.copy(),130,255,cv2.THRESH_BINARY)
_,contours,hierarchy = cv2.findContours(th1.copy(),cv2.RETR_EXTERNAL, 2)
cnt=ut.getMaxContour(contours,4000)
if cnt!=None:
x,y,w,h = cv2.boundingRect(cnt)
imgT=img[y:y+h,x:x+w]
imgT=cv2.bitwise_and(imgT,imgT,mask=th1[y:y+h,x:x+w])
imgT=cv2.resize(imgT,(200,200))
cv2.imshow('Trainer',imgT)
cv2.imshow('Frame',img)
cv2.imshow('Thresh',th1)
k = 0xFF & cv2.waitKey(10)
if k == 27:
break
if k == ord('s'):
name=str(i)+"_"+str(j)+".jpg"
cv2.imwrite(train_folder+'/'+name,imgT)
if(j<40):
j+=1
else:
while(0xFF & cv2.waitKey(0)!=ord('n')):
j=1
j=1
i+=1
cap.release()
cv2.destroyAllWindows()