GS123 commited on
Commit
72870c0
·
verified ·
1 Parent(s): 7a167a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -71
app.py CHANGED
@@ -1,72 +1,72 @@
1
- import tensorflow as tf
2
- import cv2
3
- import numpy as np
4
- import matplotlib.pyplot as plt
5
- import seaborn as sns
6
- import streamlit as st
7
- from PIL import Image
8
-
9
- st.image(r"face-mask.png", width = 150)
10
- st.write("# Mask detector App")
11
-
12
- # load model
13
- @st.cache_resource
14
- def cache_model(model_add):
15
- model = tf.keras.models.load_model(model_add)
16
- return model
17
-
18
- model = cache_model("mask_detector.keras")
19
-
20
- labels = ['WithMask', 'WithoutMask']
21
- def predict(img,model,labels = labels):
22
- # read_image
23
- # img = cv2.imread(img_add)
24
- # resize image to 224, 224
25
- img = cv2.resize(img, (224,224))
26
- # st.write("resize_cv2", img.shape)
27
- # convert to RGB
28
- img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
29
- img_expand = tf.expand_dims(img_rgb, axis = 0)
30
- y_pred_prob = model.predict(img_expand)
31
- y_pred_prob_round = np.round(y_pred_prob[0][0], 3)
32
- st.write(y_pred_prob[0][0])
33
- y_pred = int(np.round(y_pred_prob_round))
34
- label = labels[y_pred]
35
- return (label, y_pred_prob_round)
36
-
37
- def face_detect(img, model, labels = labels):
38
- # img = cv2.imread(img_add)
39
- face_detector=cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
40
- img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
41
- faces = face_detector.detectMultiScale(img_gray)
42
- if len(faces) > 0:
43
- for face in faces:
44
- x,y,w,h = face
45
- x_max = x + w
46
- y_max = y + h
47
- face_crp = img[y:y_max+1, x:x_max+1]
48
- label, prob = predict(face_crp,model,labels = labels)
49
- if label == labels[0]:
50
- cv2.rectangle(img, (x,y), (x_max, y_max), (0,255,0), 3)
51
- cv2.putText(img, f"{label} -: {1 - prob}%", (x,y-10), cv2.FONT_HERSHEY_SIMPLEX,
52
- 1, (0,255,0), 2)
53
- else:
54
- cv2.rectangle(img, (x,y), (x_max, y_max), (255,0,0), 3)
55
- cv2.putText(img, f"{label} -: {prob}%", (x,y-10), cv2.FONT_HERSHEY_SIMPLEX,
56
- 1, (255,0,0), 2)
57
- return img
58
-
59
-
60
- # File uploader
61
- uploaded_file = st.file_uploader("upload image", type=["jpg", "jpeg", "png"])
62
- submit_btn = st.button("Submit")
63
-
64
- if submit_btn:
65
- if uploaded_file:
66
- img_array = np.array(Image.open(uploaded_file))
67
- # st.write(img_array.shape)
68
- result_img = face_detect(img_array, model)
69
- # result_img_rgb = cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)
70
- st.image(result_img)
71
- else:
72
  st.write("Please upload an image")
 
1
+ import tensorflow as tf
2
+ import cv2
3
+ import numpy as np
4
+ # import matplotlib.pyplot as plt
5
+ # import seaborn as sns
6
+ import streamlit as st
7
+ from PIL import Image
8
+
9
+ st.image(r"face-mask.png", width = 150)
10
+ st.write("# Mask detector App")
11
+
12
+ # load model
13
+ @st.cache_resource
14
+ def cache_model(model_add):
15
+ model = tf.keras.models.load_model(model_add)
16
+ return model
17
+
18
+ model = cache_model("mask_detector.keras")
19
+
20
+ labels = ['WithMask', 'WithoutMask']
21
+ def predict(img,model,labels = labels):
22
+ # read_image
23
+ # img = cv2.imread(img_add)
24
+ # resize image to 224, 224
25
+ img = cv2.resize(img, (224,224))
26
+ # st.write("resize_cv2", img.shape)
27
+ # convert to RGB
28
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
29
+ img_expand = tf.expand_dims(img_rgb, axis = 0)
30
+ y_pred_prob = model.predict(img_expand)
31
+ y_pred_prob_round = np.round(y_pred_prob[0][0], 3)
32
+ st.write(y_pred_prob[0][0])
33
+ y_pred = int(np.round(y_pred_prob_round))
34
+ label = labels[y_pred]
35
+ return (label, y_pred_prob_round)
36
+
37
+ def face_detect(img, model, labels = labels):
38
+ # img = cv2.imread(img_add)
39
+ face_detector=cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
40
+ img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
41
+ faces = face_detector.detectMultiScale(img_gray)
42
+ if len(faces) > 0:
43
+ for face in faces:
44
+ x,y,w,h = face
45
+ x_max = x + w
46
+ y_max = y + h
47
+ face_crp = img[y:y_max+1, x:x_max+1]
48
+ label, prob = predict(face_crp,model,labels = labels)
49
+ if label == labels[0]:
50
+ cv2.rectangle(img, (x,y), (x_max, y_max), (0,255,0), 3)
51
+ cv2.putText(img, f"{label} -: {prob}%", (x,y-10), cv2.FONT_HERSHEY_SIMPLEX,
52
+ 1, (0,255,0), 2)
53
+ else:
54
+ cv2.rectangle(img, (x,y), (x_max, y_max), (255,0,0), 3)
55
+ cv2.putText(img, f"{label} -: {1 - prob}%", (x,y-10), cv2.FONT_HERSHEY_SIMPLEX,
56
+ 1, (255,0,0), 2)
57
+ return img
58
+
59
+
60
+ # File uploader
61
+ uploaded_file = st.file_uploader("upload image", type=["jpg", "jpeg", "png"])
62
+ submit_btn = st.button("Submit")
63
+
64
+ if submit_btn:
65
+ if uploaded_file:
66
+ img_array = np.array(Image.open(uploaded_file))
67
+ # st.write(img_array.shape)
68
+ result_img = face_detect(img_array, model)
69
+ # result_img_rgb = cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)
70
+ st.image(result_img)
71
+ else:
72
  st.write("Please upload an image")