def run_inference(self, inference_id): inference_obj = Inference.objects.get(id=inference_id) inference_obj.task_id = self.request.id inference_obj.save() self.update_state(state='image preprocessing', meta={'progress': 0}) img = image.load_img(inference_obj.image.file.filename, target_size=(150, 150)) img_array = image.image_to_array(img) img_array.shape = (1, 150, 150, 3) self.update_state(state='load the model', meta={'progress': 25}) with open('./model_architecture.json', 'r') as f: model = model_from_json(f.read()) model.load_weights('./model_weights.h5') self.update_state(state='predict the image', meta={'progress': 50}) prediction = model.predict(img_array, verbose=1) result = 'unknown' if prediction < 0.5: result = 'Normal' elif prediction > 0.5: result = 'Pneumonia' inference_obj.result = result inference_obj.save() self.update_state(state='Finished', meta={'progress': 100})
'haarcascade_frontalface_default.xml') cap=cv2.VideoCapture(0) while True: test_image=cap.read() coverted_image= cv2.cvtColor(test_image, cv2.COLOR_BGR2GRAY) faces_detected = face_haar_cascade.detectMultiScale(converted_image) for (x,y,w,h) in faces_detected: cv2.rectangle(test_image,(x,y), (x+w,y+h), (255,0,0)) roi_gray=gray_image[y:y+w,x:x+h] roi_gray=cv2.resize(roi_gray,(48,48)) image_pixels = image.image_to_array(roi_gray) image_pixels = np.expand_dims(image_pixels, axis = 0) image_pixels /= 255 predictions = model.predict(image_pixels) max_index = np.argmax(predictions[0]) emotion_detection = ('angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral') emotion_prediction = emotion_detection[max_index] cv2.putText(test_image, emotion_prediction, (int(x), int(y))) cv2.putText(frame,label,label_position,cv2.FONT_HERSHEY_SIMPLEX,2,(0,255,0),3)
target_size=(64, 64), batch_size=batch, class_mode='binary') test_set = test_datagen.flow_from_directory('../dataset/test_set', target_size=(64, 64), batch_size=batch, class_mode='binary') classifier.fit_generator(training_set, steps_per_epoch=(8000 / batch), epochs=25, validation_data=test_set, validation_steps=(2000 / batch)) elapsed = time.time() - start print("Elapsed: %f seconds", elapsed) from keras.models import load_model classifier.save('cat_or_dog_220batch.h5') from keras.preprocessing import image import numpy as np prediction_image = image.load_img( './dataset/single_prediction/cat_or_dog_3.jpg') prediction_image = image.image_to_array(prediction_image) prediction_image = np.expand_dims(prediction_image, axis=0) prediction_image = prediction_image.reshape(64, 64, 3) print(new_prediction)
import os import numpy as np import matplotlib.pyplot as plt import keras from keras.layers import * from keras.models import * from keras.preprocessing import image BASE_DIR = os.path.dirname(os.path.abspath(__file__)) model = load_model(os.path.join(BASE_DIR, 'model.h5')) img = image.load_image('image.jpeg', target_size=(224, 224)) img = image.image_to_array(img) img = np.expand_dims(img, axis=0) p = model.predict_classes(img) print(p)