• 大小: 32.05MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-07-01
  • 语言: Python
  • 标签:

资源简介

一个用手势控制的计算器

资源截图

代码片段和文件信息

import numpy as np
import pickle
import cv2
import os
from keras import optimizers
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint
from keras import backend as K
K.set_image_dim_ordering(‘tf‘)

def get_image_size():
img = cv2.imread(‘gestures/0/100.jpg‘ 0)
return img.shape

def get_num_of_classes():
return len(os.listdir(‘gestures/‘))

image_x image_y = get_image_size()

def cnn_model():
num_of_classes = get_num_of_classes()
model = Sequential()
model.add(Conv2D(32 (55) input_shape=(image_x image_y 1) activation=‘relu‘))
model.add(MaxPooling2D(pool_size=(2 2) strides=(2 2) padding=‘same‘))
model.add(Conv2D(64 (55) activation=‘relu‘))
model.add(MaxPooling2D(pool_size=(5 5) strides=(5 5) padding=‘same‘))
model.add(Flatten())
model.add(Dense(1024 activation=‘relu‘))
model.add(Dropout(0.4))
model.add(Dense(num_of_classes activation=‘softmax‘))
sgd = optimizers.SGD(lr=1e-4)
model.compile(loss=‘categorical_crossentropy‘ optimizer=sgd metrics=[‘accuracy‘])
filepath=“cnn_model_keras2.h5“
checkpoint1 = ModelCheckpoint(filepath monitor=‘val_acc‘ verbose=1 save_best_only=True mode=‘max‘)
#checkpoint2 = ModelCheckpoint(filepath monitor=‘val_loss‘ verbose=1 save_best_only=True mode=‘min‘)
callbacks_list = [checkpoint1]
return model callbacks_list

def train():
with open(“train_images“ “rb“) as f:
train_images = np.array(pickle.load(f))
with open(“train_labels“ “rb“) as f:
train_labels = np.array(pickle.load(f) dtype=np.int32)

with open(“test_images“ “rb“) as f:
test_images = np.array(pickle.load(f))
with open(“test_labels“ “rb“) as f:
test_labels = np.array(pickle.load(f) dtype=np.int32)

train_images = np.reshape(train_images (train_images.shape[0] image_x image_y 1))
test_images = np.reshape(test_images (test_images.shape[0] image_x image_y 1))
train_labels = np_utils.to_categorical(train_labels)
test_labels = np_utils.to_categorical(test_labels)

model callbacks_list = cnn_model()
model.fit(train_images train_labels validation_data=(test_images test_labels) epochs=50 batch_size=100 callbacks=callbacks_list)
scores = model.evaluate(test_images test_labels verbose=0)
print(“CNN Error: %.2f%%“ % (100-scores[1]*100))
#model.save(‘cnn_model_keras2.h5‘)

train()

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-03-04 17:49  Simple-OpenCV-Calculator-master\
     文件        4125  2018-03-04 17:49  Simple-OpenCV-Calculator-master\README.md
     文件        2541  2018-03-04 17:49  Simple-OpenCV-Calculator-master\cnn_keras.py
     文件     8950256  2018-03-04 17:49  Simple-OpenCV-Calculator-master\cnn_model_keras2.h5
     文件        3716  2018-03-04 17:49  Simple-OpenCV-Calculator-master\create_gestures.py
     文件         900  2018-03-04 17:49  Simple-OpenCV-Calculator-master\display_all_gestures.py
     文件       16384  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gesture_db.db
     目录           0  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\
     目录           0  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\10.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\100.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1000.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1001.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1002.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1003.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1004.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1005.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1006.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1007.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1008.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1009.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\101.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1010.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1011.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1012.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1013.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1014.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1015.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1016.jpg
     文件         369  2018-03-04 17:49  Simple-OpenCV-Calculator-master\gestures\0\1017.jpg
............此处省略15594个文件信息

评论

共有 条评论