File size: 2,744 Bytes
6606691
 
 
51eb9ff
7d75000
 
6606691
4d69047
51eb9ff
 
 
6606691
 
 
51eb9ff
 
 
 
 
 
 
 
 
4d69047
51eb9ff
 
 
 
 
 
 
 
 
 
 
 
 
cc59a99
51eb9ff
 
 
 
 
 
4d69047
51eb9ff
4d69047
 
51eb9ff
 
4d69047
 
51eb9ff
6606691
4d69047
6606691
 
 
 
 
 
 
 
 
 
51eb9ff
4d69047
 
 
 
 
 
 
6606691
4d69047
6606691
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import gradio as gr
import numpy as np
import urllib
import cv2
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import load_model


# Load the pre-trained face detection model
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

# Load the pre-trained model
model = load_model('my_model.h5')
def classify_image(img):
    img_copy = img
    height, width = img_copy.shape[0], img_copy.shape[1]

    img_copy = cv2.resize(img_copy, (500, 500))
    # Convert the image to grayscale
    gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)

    # Detect faces in the image
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
    
    # Check if any faces were detected
    if len(faces) > 0:
        #print("Human face detected in the image!")
        face_area_list = []
        # Draw rectangles around the detected faces
        for (x, y, w, h) in faces:
            cv2.rectangle(img_copy, (x, y), (x+w, y+h), (0, 255, 0), 2)
            area = w * h
            face_area_list.append(area)
        #print(sorted(face_area_list))
        big_face_area = sorted(face_area_list)[-1]
        img_area = img_copy.shape[0] * img_copy.shape[1]
        perc_area = (big_face_area/img_area)*100
        if perc_area>7:
            img = image.img_to_array(img)
            img = np.expand_dims(img, axis=0)
            img /= 255.0
            # Use the model to make a prediction
            prediction = model.predict(img)[0]
            # Map the predicted class to a label
            dic = {'NSFW': float(prediction[1]), 'CART': float(prediction[0]),'SFW':float(prediction[2])}
        else :
            dic = {'CART': float(0),'SFW': float(0), 'NSFW': float(1)}
            
        
    else:
        dic = {'CART': float(0),'SFW': float(0), 'NSFW': float(1)}
        perc_area = "could not detected face"
        #print("No human face detected in the image.")
    
    return [dic, perc_area, img_copy]

def classify_url(url):
    # Load the image from the URL
    response = urllib.request.urlopen(url)
    img = image.load_img(response, target_size=(224, 224))
    
    return classify_image(img)


# Define the GRADIO output interface
examples = [f"example{i}.jpg" for i in range(1,9)]

# Define the GRADIO output interfaces
output_interfaces = [
    gr.outputs.Label(num_top_classes=3),
    gr.outputs.Textbox(label="% Area of the largest face in image"),
    gr.outputs.Image(type="pil", label="Detected Faces")
]
# Define the GRADIO app
app = gr.Interface(classify_image, gr.Image(shape=(224, 224)), outputs=output_interfaces, allow_flagging="never", examples = examples,title="NSFW/SFW Classifier")

# Start the GRADIO app
app.launch()