我正在使用cv2为网络摄像头拍照 我使用ASL数据集训练了一个模型,我将从webcamp中对图像进行分类。为此,我需要根据训练过的模型调整图像大小
它说我必须将网络摄像头图像转换为(1,64,64,3)的形状,但它给了我一个错误
import cv2
import numpy as np
from keras.models import load_model
from keras.preprocessing import image
import imutils
from numpy import array
model = load_model('trained_model2.h5')
print('model loaded!')
out_label=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y']
pre = []
s = ''
cchar = [0, 0]
c1 = ''
aWeight = 0.5
capture = cv2.VideoCapture(0)
top, right, bottom, left = 170, 150, 425, 450
num_frames = 0
flag = 0
flag1= 0
while capture.isOpened():
ret, frame = capture.read()
frame = imutils.resize(frame, width=700)
frame = cv2.flip(frame, 1)
clone = frame.copy()
(height, width) = frame.shape[:2]
roi = frame[top:bottom, right:left]
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)
img = gray
IMG_SIZE = 64
img = cv2.resize(img, (64, 64))
img = np.expand_dims(img, axis=0)
data = img.reshape(1, IMG_SIZE, IMG_SIZE, 3)
print("converted")
data = data / 255
model_out = model.predict([data][0])
pnb = np.argmax(model_out)
print(str(np.argmax(model_out)) + " " + str(out_label[pnb]))
pre.append(out_label[pnb])
cv2.putText(clone,
'%s ' % (str(out_label[pnb])),
(450, 150), cv2.FONT_HERSHEY_PLAIN, 5, (0, 255, 0))
cv2.rectangle(frame, (100, 100), (300, 300), (0, 255, 0), 0)
crop_image = frame[100:300, 100:300]
blur = cv2.GaussianBlur(crop_image, (3, 3), 0)
hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
mask2 = cv2.inRange(hsv, np.array([2, 0, 0]), np.array([20, 255, 255]))
kernel = np.ones((5, 5))
dilation = cv2.dilate(mask2, kernel, iterations=1)
erosion = cv2.erode(dilation, kernel, iterations=1)
filtered = cv2.GaussianBlur(erosion, (3, 3), 0)
ret, thresh = cv2.threshold(filtered, 127, 255, 0)
cv2.imshow("Gesture", frame)
cv2.imshow("Thresholded", thresh)
if cv2.waitKey(1) == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
您正在尝试将4096维图像重塑为形状为(64,64,3)的图像,这表示具有RGB颜色(或OpenCV中的BGR颜色)的图像。但是,正在读取的图像是灰度图像。这意味着您不应将其重新设置为(64,64,3),而应改为(64,64,1)
相关问题 更多 >
编程相关推荐