neelsahu
commited on
Commit
•
51eb9ff
1
Parent(s):
be7165c
new examples and face detection algo
Browse files- __pycache__/app.cpython-38.pyc +0 -0
- app.py +54 -9
- example1.jpg +3 -0
- example5.jpg +0 -0
- example6.jpg +0 -0
- example7.jpg +0 -0
- example8.jpg +0 -0
- haarcascade_frontalface_default.xml +0 -0
__pycache__/app.cpython-38.pyc
ADDED
Binary file (1.72 kB). View file
|
|
app.py
CHANGED
@@ -1,21 +1,66 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import urllib
|
|
|
4 |
from tensorflow.keras.preprocessing import image
|
5 |
from tensorflow.keras.models import load_model
|
6 |
|
|
|
|
|
|
|
7 |
# Load the pre-trained model
|
8 |
model = load_model('my_model.h5')
|
9 |
def classify_image(img):
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
# Use the model to make a prediction
|
16 |
-
prediction = model.predict(img)[0]
|
17 |
-
# Map the predicted class to a label
|
18 |
-
dic = {'SFW': float(prediction[1]), 'NSFW': float(prediction[0])}
|
19 |
return dic
|
20 |
|
21 |
def classify_url(url):
|
@@ -27,7 +72,7 @@ def classify_url(url):
|
|
27 |
|
28 |
|
29 |
# Define the GRADIO output interface
|
30 |
-
examples = [f"example{i}.jpg" for i in range(
|
31 |
# Define the GRADIO app
|
32 |
app = gr.Interface(classify_image, gr.Image(shape=(224, 224)), outputs="label", allow_flagging="never", examples = examples,title="NSFW/SFW Classifier")
|
33 |
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import urllib
|
4 |
+
import cv2
|
5 |
from tensorflow.keras.preprocessing import image
|
6 |
from tensorflow.keras.models import load_model
|
7 |
|
8 |
+
# Load the pre-trained face detection model
|
9 |
+
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
|
10 |
+
|
11 |
# Load the pre-trained model
|
12 |
model = load_model('my_model.h5')
|
13 |
def classify_image(img):
|
14 |
+
#print("image : ",type(img))
|
15 |
+
#image_array = img_to_array(image)
|
16 |
+
#print("image shape: ",img.shape)
|
17 |
+
img_copy = img
|
18 |
+
height, width = img_copy.shape[0], img_copy.shape[1]
|
19 |
+
|
20 |
+
img_copy = cv2.resize(img_copy, (500, 500))
|
21 |
+
# Convert the image to grayscale
|
22 |
+
gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
|
23 |
+
|
24 |
+
# Detect faces in the image
|
25 |
+
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
26 |
+
# Check if any faces were detected
|
27 |
+
if len(faces) > 0:
|
28 |
+
#print("Human face detected in the image!")
|
29 |
+
face_area_list = []
|
30 |
+
# Draw rectangles around the detected faces
|
31 |
+
for (x, y, w, h) in faces:
|
32 |
+
cv2.rectangle(img_copy, (x, y), (x+w, y+h), (0, 255, 0), 2)
|
33 |
+
area = w * h
|
34 |
+
face_area_list.append(area)
|
35 |
+
#print(sorted(face_area_list))
|
36 |
+
big_face_area = sorted(face_area_list)[-1]
|
37 |
+
img_area = img_copy.shape[0] * img_copy.shape[1]
|
38 |
+
#print(big_face_area)
|
39 |
+
#print(img_area)
|
40 |
+
#print("% Area of the largest face in image:", (big_face_area/img_area)*100)
|
41 |
+
perc_area = (big_face_area/img_area)*100
|
42 |
+
if perc_area>7.5:
|
43 |
+
img = image.img_to_array(img)
|
44 |
+
img = np.expand_dims(img, axis=0)
|
45 |
+
img /= 255.0
|
46 |
+
|
47 |
+
# Use the model to make a prediction
|
48 |
+
prediction = model.predict(img)[0]
|
49 |
+
# Map the predicted class to a label
|
50 |
+
dic = {'SFW': float(prediction[1]), 'NSFW': float(prediction[0])}
|
51 |
+
else :
|
52 |
+
dic = {'SFW': float(0), 'NSFW': float(1)}
|
53 |
+
# Display the image with the detected faces
|
54 |
+
#cv2_imshow(img)
|
55 |
+
#cv2.imshow('Detected faces', img_copy)
|
56 |
+
#cv2.waitKey(0)
|
57 |
+
#cv2.destroyAllWindows()
|
58 |
+
# Preprocess the input image
|
59 |
+
|
60 |
+
else:
|
61 |
+
dic = {'SFW': float(0), 'NSFW': float(1)}
|
62 |
+
#print("No human face detected in the image.")
|
63 |
|
|
|
|
|
|
|
|
|
64 |
return dic
|
65 |
|
66 |
def classify_url(url):
|
|
|
72 |
|
73 |
|
74 |
# Define the GRADIO output interface
|
75 |
+
examples = [f"example{i}.jpg" for i in range(1,9)]
|
76 |
# Define the GRADIO app
|
77 |
app = gr.Interface(classify_image, gr.Image(shape=(224, 224)), outputs="label", allow_flagging="never", examples = examples,title="NSFW/SFW Classifier")
|
78 |
|
example1.jpg
ADDED
Git LFS Details
|
example5.jpg
ADDED
example6.jpg
ADDED
example7.jpg
ADDED
example8.jpg
ADDED
haarcascade_frontalface_default.xml
ADDED
The diff for this file is too large to render.
See raw diff
|
|