-
Notifications
You must be signed in to change notification settings - Fork 0
/
pneumonia.py
44 lines (31 loc) · 1.25 KB
/
pneumonia.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from keras.models import load_model # TensorFlow is required for Keras to work
import cv2 # Install opencv-python
import numpy as np
import cvzone
# Disable scientific notation for clarity
np.set_printoptions(suppress=True)
# Load the model
model = load_model("keras_Model.h5", compile=False)
# Load the labels
class_names = ['pneumonia','normal']
img = cv2.imread('test/NORMAL/NORMAL2-IM-0173-0001-0002.jpeg')
# Resize the raw image into (224-height,224-width) pixels
image = cv2.resize(img, (224, 224), interpolation=cv2.INTER_AREA)
# Make the image a numpy array and reshape it to the models input shape.
image = np.asarray(image, dtype=np.float32).reshape(1, 224, 224, 3)
# Normalize the image array
image = (image / 127.5) - 1
# Predicts the model
prediction = model.predict(image)
index = np.argmax(prediction)
class_name = class_names[index]
confidence_score = prediction[0][index]
# Print prediction and confidence score
texto1 = f"Classe: {class_name}"
texto2 = f"Confianca: {str(np.round(confidence_score * 100))[:-2]} %"
print(texto1,texto2)
cvzone.putTextRect(img, texto1,(50,50),scale=4)
cvzone.putTextRect(img, texto2,(50,100),scale=4)
# Listen to the keyboard for presses.
cv2.imshow('IMG', img)
cv2.waitKey(0)