2017-08-06 3 views
0

Ich möchte ein Netzwerk mit Bildern mit Keras füttern. Ich lade Bilder aus dem Internet herunter und speichere sie in einer Reihe. Wenn ich ein einzelnes Bild plotte, wird es korrekt angezeigt. Als nächsten Schritt erstelle ich ein neues numpy Array, in dem ich das einzelne Bild speichern werde. In diesem Schritt wird das Bild jedoch nur als schwarze Bilder angezeigt. Ich frage mich, warum das passiert?Image Transformation mit Keras und Numpy

Hier ist mein Code:

import numpy as np 
import urllib.request 
import cv2 
from matplotlib import pyplot as plt 
from keras import backend as K 
%matplotlib inline 

file = "http://farm2.static.flickr.com/1353/1230897342_2bd7c7569f.jpg" 

# Read web file 
images_or = np.ndarray((1,128, 128,3), dtype=np.uint8) 
req = urllib.request.urlopen(file) 
arr = np.asarray(bytearray(req.read()), dtype=np.uint8) 
img = cv2.imdecode(arr,-1) # 'load it as it is' 
images_or[0] = cv2.resize(img,(128,128)) 

# Display image 
plt.imshow(images_or[0]) 
plt.show() 

# Format image 
images_or = images_or.astype(K.floatx()) 
images_or *= 0.96/255 
images_or += 0.02 

# Display image 
plt.imshow(images_or[0]) 
plt.show() 

# Reshape image 
images_or = images_or.reshape(images_or.shape[0], 3, 128, 128) 

# Copy image in another np.array 
A_train_test = np.ndarray((1, 3, 128, 128), dtype=np.uint8) 
A_train_test[0] = images_or[0] 

# Format image 
A_train_test = A_train_test.astype(K.floatx()) 
A_train_test *= 0.96/255 
A_train_test += 0.02 

# Reshape image 
A_train_test = A_train_test.reshape(A_train_test.shape[0], 128, 128, 3) 
image_xxx = A_train_test[0] 

plt.imshow(image_xxx) 
plt.show() 

Vielen Dank im Voraus, Andi

Antwort

0

Ich brauchte die Reihenfolge der Befehle zu ändern:

# Copy image in another np.array 
A_train_test = np.ndarray((1, 3, 128, 128), dtype=np.uint8) 
A_train_test = A_train_test.astype(K.floatx()) 
A_train_test[0][:] = images_or[0][:] 

Probleme gelöst.

Verwandte Themen