M
M
maryaturova2021-07-01 15:15:41
Python
maryaturova, 2021-07-01 15:15:41

Keras: How to recognize a picture without saving to disk?

Dear! How can "tf.keras.preprocessing" produce a result without loading the image to disk, but directly? I have to process the image and save it to disk.

#определяем класс картинки
def model_ocr (obj, name):

    image = tf.keras.preprocessing.image.load_img(
        'image.png.png', target_size=(obj['size'], obj['size'])
    )
    input_arr = tf.keras.preprocessing.image.img_to_array(image)
    input_arr = np.array([input_arr])
    predictions = obj['main'].predict(input_arr)

    return obj['classes'][np.argmax(predictions[0])]
  
#обрабатываем и сохраняем	
new_img = cv2.copyMakeBorder(part_img, top, bottom, left, right, cv2.BORDER_CONSTANT)  
cv2.imwrite('image.png', new_img)

result = model_ocr (obj)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2021-07-01
@maryaturova

new_imgdo you have Numpy array?
You can directly pass it to the model:

predictions = obj['main'].predict([new_img]) # или predict(np.array([new_img]))

# и дальше
result = obj['classes'][np.argmax(predictions[0])]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question