-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_example
118 lines (103 loc) · 3.06 KB
/
get_example
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
107
108
109
110
111
112
113
114
115
116
117
# coding: utf-8
# In[2]:
from keras.models import load_model
from imutils import paths
import numpy as np
import imutils
import cv2
import pickle
import cv2
import win32api
import win32con
import time
import os
sample_nums = 0
minValue = 70
x0 = 400
y0 = 200
height = 200
width = 200
counter = 0
gestname = ""
path = ""
saveimg = False
def saveROIImg(img):
global counter,saveimg,gestname,sample_nums
if counter > sample_nums:
saveimg = False
counter = 0
gestname = ""
return
counter = counter + 1
name = gestname + str(counter)
print("Saving img:",name)
cv2.imwrite(path+name + ".png", img)
time.sleep(0.04 )
#肤色检测函数
def binaryMask(frame, x0, y0, width, height, framecount, plot ):
cv2.rectangle(frame, (x0,y0),(x0+width,y0+height),(0,255,0),1)
#roi = cv2.UMat(frame[y0:y0+height, x0:x0+width])
roi = frame[y0:y0+height, x0:x0+width]
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),2)
th3 = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,2)
ret, res = cv2.threshold(th3, minValue, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
if saveimg==True:
saveROIImg(res)
return res
font = cv2.FONT_HERSHEY_SIMPLEX
cap = cv2.VideoCapture(0)
cv2.namedWindow('Original', cv2.WINDOW_NORMAL)
# set rt size as 640x480
ret = cap.set(3,640)
ret = cap.set(4,480)
framecount = 0
fps = ""
start = time.time()
plot = np.zeros((512,512,3), np.uint8)
while(True):
ret, frame = cap.read()
frame = cv2.flip(frame, 3)
frame = cv2.resize(frame, (640,480))
if ret == True:
roi = binaryMask(frame, x0, y0, width, height, framecount, plot)
framecount = framecount + 1
end = time.time()
timediff = (end - start)
if( timediff >= 1):
#timediff = end - start
fps = 'FPS:%s' %(framecount)
start = time.time()
framecount = 0
cv2.putText(frame,fps,(10,20), font, 0.7,(0,255,0),2,1)
cv2.putText(frame,'push key c to create floder',(10,35), font, 0.7,(0,255,0),2,1)
cv2.putText(frame,'push key s to save samples',(10,55), font, 0.7,(0,255,0),2,1)
cv2.imshow('Original',frame)
cv2.imshow('ROI', roi)
key = cv2.waitKey(5) & 0xff
#Esc键退出
if key == 27:
cap.release()
cv2.destroyAllWindows()
elif key == ord('c'):
gestname = input("输入存放手势的文件夹名称: ")
sample_nums = int(input("输入存放手势图片数目: "))
try:
os.makedirs(gestname)
except OSError as e:
print(gestname+'文件夹已创建')
path = "./"+gestname+"/"
elif key == ord('s'):
if gestname=='':
print("请先输入一个存放文件夹的名字")
else:
saveimg = True
#手势识别框动态移动
elif key == ord('i'):
y0 = y0 - 5
elif key == ord('k'):
y0 = y0 + 5
elif key == ord('j'):
x0 = x0 - 5
elif key == ord('l'):
x0 = x0 + 5