File size: 2,300 Bytes
78206ff
4ef8c53
e351372
 
2f11393
127612b
 
 
 
 
 
 
e351372
 
 
 
 
127612b
 
9532a82
 
 
 
 
 
127612b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9532a82
5a82e84
9532a82
 
5a82e84
127612b
 
5a82e84
78206ff
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from keras.models import load_model
import cv2
import numpy as np
import gradio as gr

# Load the pre-trained model
model = load_model('keras_model.h5')

# Load the class labels
with open('labels.txt', 'r') as f:
    class_names = [line.strip() for line in f.readlines()]

# Load the Haar cascade classifier for face detection
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

font = cv2.FONT_HERSHEY_SIMPLEX

# Define a function to detect faces in a given frame
def detect_faces(frame):
    # Detect faces in the frame
    faces = face_cascade.detectMultiScale(frame, scaleFactor=1.3, minNeighbors=5)

    # You can process the frame here if needed
    # e.g., apply filters, transformations, or object detection
    for (x, y, w, h) in faces:
        # Extract the face region
        face_img = frame[y:y+h, x:x+w]

        # Preprocess the face image
        face_img = cv2.resize(face_img, (224, 224))
        face_img = np.expand_dims(face_img, axis=0)
        face_img = face_img / 255.0

        # Predict the class probabilities
        pred_probs = model.predict(face_img)[0]
        class_idx = np.argmax(pred_probs)
        class_prob = pred_probs[class_idx]

        # Get the class name and display it on the image
        class_name = class_names[class_idx]
        if class_prob*100 < 70:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
            text = '{}: {:.2f}%'.format('Unknown', class_prob * 100)
            cv2.putText(frame, text, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
        else:
            cv2.putText(frame, class_name, (x, y - 10), font, 1, (0, 255, 0), 2)
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
            text = '{}: {:.2f}%'.format(class_name, class_prob * 100)
            cv2.putText(frame, text, (x, y + h + 30), font, 0.5, (0, 255, 0), 1)

    return frame[:, :, ::-1]

# Create a Gradio interface for face detection with webcam input
face_detection = gr.Interface(fn=detect_faces, 
                              inputs=gr.inputs.Video(type="webcam"), 
                              outputs="image",
                              title="Face Detection with OpenCV",
                              live=True)

# Launch the interface
face_detection.launch()