Я хочу сделать распознавание жестов с помощью TensorFlow в Raspberry pi 3.
Я пробовал таким образом.
import tensorflow as tf
import numpy as np
import io, time
import cv2
cap = cv2.VideoCapture(-1)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 720)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
img_num = 0
while(True):
ret, frame = cap.read()
cv2.imshow('frame', frame)
if(cv2.waitKey(1) & 0xFF == ord('x')):
print("X\n")
break
elif(cv2.waitKey(1) & 0xFF == ord('c')):
cv2.imwrite(str(img_num) + ".jpg", frame)
# Import the TF graph
graph_def = tf.GraphDef()
with tf.gfile.FastGFile("model.pb", 'rb') as f:
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
# Create a list of labels.
labels = []
with open("labels.txt", 'rt') as lf:
for l in lf:
labels.append(l.strip())
def oprate(image):
def crop_center(img,cropx,cropy):
h, w = img.shape[:2]
startx = w//2-(cropx//2)
starty = h//2-(cropy//2)
return img[starty:starty+cropy, startx:startx+cropx]
def resize_down_to_1600_max_dim(image):
h, w = image.shape[:2]
if (h < 1600 and w < 1600):
return image
new_size = (1600 * w // h, 1600) if (h > w) else (1600, 1600 * h // w)
return cv2.resize(image, new_size, interpolation = cv2.INTER_LINEAR)
def resize_to_256_square(image):
h, w = image.shape[:2]
return cv2.resize(image, (227, 227), interpolation = cv2.INTER_LINEAR)
# If the image has either w or h greater than 1600 we resize it down respecting
# aspect ratio such that the largest dimension is 1600
image = resize_down_to_1600_max_dim(image)
# We next get the largest center square
h, w = image.shape[:2]
min_dim = min(w,h)
max_square_image = crop_center(image, min_dim, min_dim)
# Resize that square down to 256x256
augmented_image = resize_to_256_square(max_square_image)
# The compact models have a network size of 227x227, the model requires this size.
# Crop the center for the specified network_input_Size
augmented_image = crop_center(augmented_image, 227, 227)
augmented_image = resize_to_256_square(max_square_image)
# These names are part of the model and cannot be changed.
output_layer = 'loss:0'
input_node = 'Placeholder:0'
with tf.Session() as sess:
prob_tensor = sess.graph.get_tensor_by_name(output_layer)
predictions, = sess.run(prob_tensor, {input_node: [augmented_image] })
###View the results
# Print the highest probability label
# Or you can print out all of the results mapping labels to probabilities.
label_index = 0
list_img = ""
for p in predictions:
truncated_probablity = np.float64(round(p,8))
#list_img += labels[label_index] + ":%4f"%truncated_probablity+","
print (labels[label_index],":%4f "%truncated_probablity,end=" | ")
label_index += 1
#list_img = (list_img.split(','))
#print(list_img[0],list_img[4],list_img[9],list_img[15])
highest_probability_index = np.argmax(predictions)
print('Classified as:' + labels[highest_probability_index])
return labels[highest_probability_index]
### MAIN ###
if __name__ == '__main__':
list_img2 = ""
image = cv2.imread("%d.jpg"%img_num)
list_img2 = (oprate(image)).split('_',1)
print(list_img2[0]+"\n")
img_num += 1
cap.release()
cv2.destroyAllWindows()
Это приводит к ошибке:
ValueError: Невозможно передать значение формы (1, 227, 227, 3) для тензора 'Заполнитель: 0', который имеет форму '(?, 224, 224, 3)'
ИтакЯ изменил (227, 227) на (224, 224) в
return cv2.resize(image, (227, 227), interpolation = cv2.INTER_LINEAR)
Но при этом я получаю ошибку.