def image_classification(paths): classification_dict = {} model = MobileNet(input_shape=None, alpha=1.0, depth_multiplier=1, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000) for path in paths: image = mnetv2_loadimage(path) preds = model.predict(image) prediction = decode_predictions(preds, top=1)[0][0] img_class = prediction[1] img_confidence = prediction[2] if img_class in classification_dict: classification_dict[img_class].append((path, img_confidence)) else: classification_dict[img_class] = [(path, img_confidence)] return classification_dict
class MobileNetWrapper(object): def __init__(self): logger.info('Loading MobileNet') self.model = MobileNet(weights='imagenet') def predict(self, img): """ # Arguments img: a numpy array # Returns A dict containing predictions """ img = Image.fromarray(img) img = img.resize((224, 224)) x = keras_image.img_to_array(img)[:, :, :3] x = np.expand_dims(x, axis=0) x = preprocess_input_mobilenet(x) features = self.model.predict(x) predictions = decode_predictions_mobilenet(features)[0] clean_predictions = [{'score': str(k), 'class': j} for (i, j, k) in predictions] return json.dumps(clean_predictions)
# In[ ]: training_set_mobilenet_features = [] for i in list(range(1, 401)) + list(range(501, 901)): img_path = 'test8/%06d.png' % i # print(img_path) img = image.load_img(img_path, target_size=(224, 224)) # 读取图片 #plt.imshow(img) #plt.show()# 显示图片 x = image.img_to_array(img) # 图片转换为ndarray x = np.expand_dims(x, axis=0) x = preprocess_input(x) # 数组转换为mobilenet输入格式 training_set_mobilenet_features.append( model_mobilenet.predict(x).reshape((7 * 7 * 1024, ))) # 计算该张图片的特征 # In[ ]: training_set_mobilenet_features_ndarray = np.vstack( training_set_mobilenet_features) # 转换为ndarray # In[ ]: training_set_label = np.array([1.0 if i < 400 else 0.0 for i in range(800)]) # In[ ]: test_set_mobilenet_features = [] for i in list(range(401, 501)) + list(range(901, 1001)): img_path = 'test8/%06d.png' % i
for _ in camera.capture_continuous( stream, format='jpeg', use_video_port=True): #start to capture frames from the preview stream.seek(0) #begin from the first position of the buffer img_ov = Image.new("RGBA", (CAMERA_WIDTH,CAMERA_HEIGHT)) #create the overlay using the same resolution of the camera draw = ImageDraw.Draw(img_ov) #we are going to write on this overlay draw.font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSans.ttf", 20) draw.rectangle([5,5,CAMERA_WIDTH-5,CAMERA_HEIGHT-5], fill=None, outline="red") #a purely decorative choice img = Image.open(stream).convert('RGB').resize((224, 224)) #we modify the frame to give it as input to the model #we then give the frame to the model and calculate the inference time x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) start_time = time.monotonic() preds = model.predict(x) elapsed_ms = (time.monotonic() - start_time) * 1000 preds= decode_predictions(preds) # we write the first two output probabilities onto the overlay draw.multiline_text((8,40), str(preds[0][0][1])+"\n"+str('%.1f' % ((preds[0][0][2])*100))+ " % \n", fill=(255,0,0,255), font=None, anchor=None, spacing=5, align="left") draw.multiline_text((8,85), str(preds[0][1][1])+"\n"+str('%.1f' % ((preds[0][1][2])*100))+ " % \n", fill=(255,0,0,255), font=None, anchor=None, spacing=5, align="left") draw.text((8,8),str('%.1f ms' % (elapsed_ms)), fill="red", font=None, anchor=None) if not overlay_renderer: """ If overlay layer is not created yet, get a new one. Layer parameter must have 3 or higher number because the original preview layer has a # of 2 and a layer with smaller number will
camera = picamera.PiCamera() camera.resolution = (224, 224) model = MobileNet(weights='imagenet') devnull = open('os.devnull', 'w') i = 1 while True: camera.capture("img.jpg") img = image.load_img("img.jpg", target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) pred = model.predict(x) word = decode_predictions(pred)[0][0][1].replace('_', ' ') word2 = decode_predictions(pred)[0][1][1].replace('_', ' ') speak = "Okay I got it. I believe I'm looking at some kind of {} or {}".format( word, word2) print(speak) os.rename('img.jpg', 'outputs/mobilenet/{}-{}-{}.jpg'.format(i, word, word2)) subprocess.run([ 'pico2wave', '-w', 'outputs/mobilenet/{}-result.wav'.format(i), "{}".format(speak) ], stdout=devnull, stderr=subprocess.STDOUT) subprocess.call(['aplay', 'outputs/mobilenet/{}-result.wav'.format(i)]) print()
return result # load the MobileNet model model_features = MobileNet(input_shape=(IMG_WIDTH, IMG_HEIGHT, 3), include_top=False, weights='imagenet') img_path = 'test.png' img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # get the bottleneck features from the mobilenet bottleneck_features = model_features.predict(x) # load our classfier model model_classification = load_model(TOP_MODEL_PATH) # use the bottleneck features to get the final classification class_predicted = model_classification.predict(bottleneck_features) #print('Predicted:', decode_predictions(preds, top=3)[0]) preds = decode_lights_predictions(class_predicted) print(preds) """ orig = cv2.imread(img_path)
from picamera.array import PiRGBArray from picamera import PiCamera model = MobileNet(weights='imagenet') print "Load model successfully" camera = PiCamera() camera.resolution = (600, 600) camera.framerate = 32 cap = PiRGBArray(camera, size=(600, 600)) print "Start video stream" for i in camera.capture_continuous(cap, format='bgr', use_video_port=True): frame = i.array image = cv2.resize(frame, (224, 224)) img = image.img_to_array(img) img = np.expand_dims(img, axis=0) img = preprocess_input(img) preds = model.predict(img) cv2.putText(frame, 'Predicted: ' + str(decode_predictions(preds, top=1)[0]), (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF cap.truncate(0) if key == ord('q'): break
img_mobilenet = load_img(img_mobilenet_pth, target_size=( 224, 224)) # image size can be calibrated with target_size parameter img_mobilenet img_mobilenet = img_to_array(img_mobilenet) print(img_mobilenet.shape) img_mobilenet = np.expand_dims(img_mobilenet, axis=0) print(img_mobilenet.shape) img_mobilenet = preprocess_input( img_mobilenet) # preprocess image with preprocess_input function print(img_mobilenet.shape) ## prediction with preprocessing pred_mobilenet = model_mobilenet.predict(img_mobilenet) # print(pred_class) n = 10 top_n_mobilenet = decode_predictions(pred_mobilenet, top=n) for b in top_n_mobilenet[0]: print(b) ##4 #using VGG16 MODEL from keras.applications.vgg16 import VGG16 model_vgg16 = VGG16() #load image
class AppearanceFeaturesExtractor(object): """ """ def __init__(self, model_type="MobileNet", weights="imagenet", input_shape=(128, 128, 3)): """ """ model_types = ["MobileNet", "MobileNetV2", "VGG16", "ResNet50"] weights_types = ["imagenet", "random"] if weights not in weights_types: raise ValueError( "Invalid weights. Should be one of: {}".format(weights_types)) if model_type not in model_types: raise ValueError( "Invalid model type. Should be one of: {}".format(model_types)) assert input_shape[0] > 32 assert input_shape[1] > 32 self.input_shape = input_shape if model_type == "MobileNet": self.model = MobileNet(weights=weights, include_top=False, pooling='avg', input_shape=input_shape) if model_type == "MobileNetV2": self.model = MobileNetV2(weights=weights, include_top=False, pooling='avg', input_shape=input_shape) if model_type == "VGG16": self.model = VGG16(weights=weights, include_top=False, pooling='avg', input_shape=input_shape) if model_type == "ResNet50": self.model = ResNet50(weights=weights, include_top=False, pooling='avg', input_shape=input_shape) def extract(self, rgb_image, track=None): """ """ if track is not None: x = track.bbox.xmin y = track.bbox.ymin w = track.bbox.width() h = track.bbox.height() crop_image = rgb_image[y:y + h, x:x + w] else: crop_image = rgb_image image_resized = cv2.resize(crop_image, (self.input_shape[0], self.input_shape[1])) x = image.img_to_array(image_resized) x = np.expand_dims(x, axis=0) x = preprocess_input(x) features = self.model.predict(x) return np.array(features).flatten()
def mobile_net(pred_cats,image_path,pickle_path,query_path): model = MobileNet(weights='imagenet', include_top=False) #Reading Image Names from Predicted Categories image_names=[] for i in range(0,len(pred_cats)) : # Give the path of the semi trained dataset path = image_path +"/"+ pred_cats[i] for img_path in os.listdir(path): if('.DS_Store' not in os.path.join(path,img_path)): image_names.append(pred_cats[i]+"/"+img_path) start = time.time() #print (image_names) # Give the MobileNet pickle file path for i in range(3): pickled_db_path = pickle_path + pred_cats[i] + ".pck" with open(pickled_db_path, 'rb') as fp: temp = pickle.load(fp) fp.close() if i == 0: mnet_loaded = temp else: mnet_loaded = np.append(mnet_loaded,temp,axis = 0) print(mnet_loaded.shape) #Extract features of Query Image imgq = image.load_img(query_path, target_size=(224, 224)) img_dataq = image.img_to_array(imgq) img_dataq = np.expand_dims(img_dataq, axis=0) img_dataq = preprocess_input(img_dataq) mnet_feature_query = model.predict(img_dataq) mnet_feature_np_query = np.array(mnet_feature_query) mnet_feature_np_query = mnet_feature_np_query.flatten() #Calculate Cosine Similarity similarity = [] count = 0 for i in mnet_loaded: count = count+1 d = distance.cosine(i,mnet_feature_np_query) sim = 1-d similarity.append((sim,image_names[count-1])) # To display Image def show_img(path): img =imread(path, mode="RGB") plt.imshow(img) plt.show() # Picking top 6 image similarities des_similarity = sorted(similarity,reverse=True) des_similarity = des_similarity[:6] print(des_similarity) # Give the Semi_images path print ('Query image ==========================================') show_img(query_path) print ('Result images ========================================') for i in range(len(des_similarity)): print("similarity",des_similarity[i][0]) result_image_path = image_path+ "/"+des_similarity[i][1] if(os.path.isfile(result_image_path)): show_img(result_image_path) end = time.time() total_time_main = end - start print("total_time_main", total_time_main)
# capture frames from the camera for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): # grab the raw NumPy array representing the image, then initialize the timestamp # and occupied/unoccupied text image = frame.array # machine learning # resize to VGG16 size(224, 224) img = Image.fromarray(np.uint8(image)) img = img.resize((224, 224)) x = img pred_data = np.expand_dims(x, axis=0) preds = model.predict(preprocess_input(pred_data)) results = decode_predictions(preds, top=1)[0] for result in results: # print(result) label = result[1] accu = str(result[2]) #gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # show the frame new_label = label + accu image = cv2.putText(image, new_label, (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) cv2.imshow("Frame", image) key = cv2.waitKey(1) & 0xFF # ピカ処理
y_train = to_categorical(y_train) y_test = to_categorical(y_test) # In[11]: print(x_train.shape, y_train.shape) print(x_test.shape, y_test.shape) # In[12]: from keras.applications.mobilenet import MobileNet MobileNet_extractor = MobileNet(weights='imagenet', include_top=False) # In[13]: train_features = MobileNet_extractor.predict(x_train) test_features = MobileNet_extractor.predict(x_test) # In[14]: print(train_features.shape, y_train.shape) print(test_features.shape, y_test.shape) # In[15]: train_features = train_features.reshape(train_features.shape[0], -1) test_features = test_features.reshape(test_features.shape[0], -1) # In[16]: print(train_features.shape, y_train.shape)
""" name = os.listdir('/content/drive/My Drive/Colab Notebooks/Large_data/Test') test_images = [] for i in range(len(name)): img = cv2.imread('/content/drive/My Drive/Colab Notebooks/Large_data/Test/' + name[i],0) img = cv2.resize(img,(64,64)) test_images.append(img) #print('Test' + str(i)) test_images = np.array(test_images) test_images = test_images/255.0 test_images = np.expand_dims(test_images,3) final = model.predict(test_images) print(final) pred = [] for i in final: pred.append(np.argmax(i)) print(pred) """ Plot metrics """ print(example.history.keys()) plt.plot(example.history['acc']) plt.plot(example.history['val_acc'])
# In[ ]: training_set_vgg16_features = [] for i in list(range(1, 401)) + list(range(501, 901)): img_path = 'test8/%06d.png' % i # print(img_path) img = image.load_img(img_path, target_size=(224, 224)) # 读取图片 #plt.imshow(img) #plt.show()# 显示图片 x = image.img_to_array(img) # 图片转换为ndarray x = np.expand_dims(x, axis=0) x = preprocess_input(x) # 数组转换为vgg16输入格式 training_set_vgg16_features.append( model_vgg16.predict(x).reshape((7 * 7 * 512, ))) # 计算该张图片的特征 # In[ ]: training_set_vgg16_features_ndarray = np.vstack( training_set_vgg16_features) # 转换为ndarray # In[ ]: training_set_label = np.array([1.0 if i < 400 else 0.0 for i in range(800)]) # # 读取测试集图片计算特征 # In[ ]: test_set_vgg16_features = []
# loop over the frames from the video stream while True: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels frame = vs.read() frame = imutils.resize(frame, width=400) # prepare the image to be classified by our deep learning network image = cv2.resize(frame, (224, 224)) image = image.astype("float") / 255.0 image = img_to_array(image) image = np.expand_dims(image, axis=0) # classify the input image and initialize the label and # probability of the prediction preds = model.predict(preprocess_input(image)) results = decode_predictions(preds, top=1)[0] for result in results: # print(result) label = result[1] proba = str(result[2]) # otherwise, reset the total number of consecutive frames and the # santa alarm # build the label and draw it on the frame label = "{0}: {1}%".format(label, proba * 100) frame = cv2.putText(frame, label, (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) # show the output frame
openNames = [] files = os.listdir(test_file_dir) for x in files: if os.path.isfile(test_file_dir + x): openNames.append(test_file_dir + str(x)) image = load_img(openNames[len(openNames) - 1], target_size=(224, 224)) print("loaded image #", len(openNames) - 1) # convert the image pixels to a numpy array image = img_to_array(image) # reshape data for the model image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) # prepare the image for the VGG model image = preprocess_input(image) # predict the probability across all output classes yhat = model.predict(image) # convert the probabilities to class labels labelPredict = decode_predictions(yhat) # retrieve the most likely result, e.g. highest probability label = labelPredict[0][0] label2 = labelPredict[0][1] label3 = labelPredict[0][2] # print the classification print('%s (%.2f%%)' % (label[1], label[2] * 100)) print('%s (%.2f%%)' % (label2[1], label2[2] * 100)) print('%s (%.2f%%)' % (label3[1], label3[2] * 100)) openFile = open("C://Users//mitch//Desktop//ImgText//guess.txt", 'w') outString = str(label[1]) + "," + str(label[2]) print(outString) print(outString, file=openFile) openFile.close()
# Load our model # model = densenet169_model(img_rows=img_rows, img_cols=img_cols, color_type=channel, num_classes=num_classes) # load keras model model = MobileNet(weights=None, classes=10) sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy']) model.summary() # Start Fine-tuning model.fit( X_train, Y_train, batch_size=batch_size, epochs=nb_epoch, shuffle=True, verbose=1, validation_data=(X_valid, Y_valid), ) # Make predictions predictions_valid = model.predict(X_valid, batch_size=batch_size, verbose=1) # Cross-entropy loss score score = log_loss(Y_valid, predictions_valid)