Muzmmillcoste commited on
Commit
df65647
·
1 Parent(s): c23f697

create initail app

Browse files
Files changed (4) hide show
  1. app.py +50 -0
  2. haarcascade_frontalface_default.xml +0 -0
  3. masknet.h5 +3 -0
  4. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ from tensorflow.keras.models import load_model
5
+ from scipy.spatial import distance
6
+
7
+ # Load model and haarcascade once
8
+ model = load_model("D:\CV CEP\Mask_Detection\masknet.h5")
9
+ face_cascade = cv2.CascadeClassifier("D:\CV CEP\Mask_Detection\haarcascade_frontalface_default.xml")
10
+
11
+ mask_label = {0: 'MASK', 1: 'NO MASK'}
12
+ dist_label = {0: (0, 255, 0), 1: (255, 0, 0)}
13
+ MIN_DISTANCE = 75
14
+
15
+ def predict(image):
16
+ img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
17
+ orig_img = img.copy()
18
+
19
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
20
+ faces = face_cascade.detectMultiScale(gray, 1.1, 4)
21
+
22
+ label = [0 for _ in range(len(faces))]
23
+
24
+ if len(faces) >= 2:
25
+ centers = [(x + w // 2, y + h // 2) for (x, y, w, h) in faces]
26
+ for i in range(len(centers) - 1):
27
+ for j in range(i + 1, len(centers)):
28
+ dist = distance.euclidean(centers[i], centers[j])
29
+ if dist < MIN_DISTANCE:
30
+ label[i] = 1
31
+ label[j] = 1
32
+
33
+ for i in range(len(faces)):
34
+ (x, y, w, h) = faces[i]
35
+ face_crop = img[y:y + h, x:x + w]
36
+ face_crop = cv2.resize(face_crop, (128, 128))
37
+ face_crop = np.reshape(face_crop, [1, 128, 128, 3]) / 255.0
38
+
39
+ result = model.predict(face_crop)
40
+ class_id = np.argmax(result)
41
+
42
+ cv2.rectangle(orig_img, (x, y), (x + w, y + h), dist_label[label[i]], 2)
43
+ cv2.putText(orig_img, mask_label[class_id], (x, y - 10),
44
+ cv2.FONT_HERSHEY_SIMPLEX, 0.7, dist_label[label[i]], 2)
45
+
46
+ output = cv2.cvtColor(orig_img, cv2.COLOR_BGR2RGB)
47
+ return output
48
+
49
+ iface = gr.Interface(fn=predict, inputs=gr.Image(type="numpy"), outputs="image", title="Face Mask Detection")
50
+ iface.launch()
haarcascade_frontalface_default.xml ADDED
The diff for this file is too large to render. See raw diff
 
masknet.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:95c32b3bc83e191312b6f003bdf0d54f5cf94d44aed9a6fd4d45d88d2183595e
3
+ size 80362728
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ tensorflow
3
+ opencv-python-headless
4
+ numpy
5
+ scipy
6
+ matplotlib