Spaces:
Runtime error
Runtime error
neelsahu
commited on
Commit
·
4d69047
1
Parent(s):
cc59a99
text box to tell percenatge of face area and image viewer to check the detected face added
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ 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 |
|
@@ -23,6 +24,7 @@ def classify_image(img):
|
|
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!")
|
@@ -35,9 +37,6 @@ def classify_image(img):
|
|
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:
|
43 |
img = image.img_to_array(img)
|
@@ -47,21 +46,17 @@ def classify_image(img):
|
|
47 |
# Use the model to make a prediction
|
48 |
prediction = model.predict(img)[0]
|
49 |
# Map the predicted class to a label
|
50 |
-
dic = {'NSFW': float(prediction[1]), '
|
51 |
else :
|
52 |
-
dic = {'SFW': float(0), 'NSFW': float(1)}
|
53 |
-
|
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):
|
67 |
# Load the image from the URL
|
@@ -73,8 +68,15 @@ def classify_url(url):
|
|
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=
|
78 |
|
79 |
# Start the GRADIO app
|
80 |
app.launch()
|
|
|
5 |
from tensorflow.keras.preprocessing import image
|
6 |
from tensorflow.keras.models import load_model
|
7 |
|
8 |
+
|
9 |
# Load the pre-trained face detection model
|
10 |
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
|
11 |
|
|
|
24 |
|
25 |
# Detect faces in the image
|
26 |
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
27 |
+
|
28 |
# Check if any faces were detected
|
29 |
if len(faces) > 0:
|
30 |
#print("Human face detected in the image!")
|
|
|
37 |
#print(sorted(face_area_list))
|
38 |
big_face_area = sorted(face_area_list)[-1]
|
39 |
img_area = img_copy.shape[0] * img_copy.shape[1]
|
|
|
|
|
|
|
40 |
perc_area = (big_face_area/img_area)*100
|
41 |
if perc_area>7:
|
42 |
img = image.img_to_array(img)
|
|
|
46 |
# Use the model to make a prediction
|
47 |
prediction = model.predict(img)[0]
|
48 |
# Map the predicted class to a label
|
49 |
+
dic = {'NSFW': float(prediction[1]), 'CART': float(prediction[0]),'SFW':float(prediction[2])}
|
50 |
else :
|
51 |
+
dic = {'CART': float(0),'SFW': float(0), 'NSFW': float(1)}
|
52 |
+
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
else:
|
55 |
+
dic = {'CART': float(0),'SFW': float(0), 'NSFW': float(1)}
|
56 |
+
perc_area = "could not detected face"
|
57 |
#print("No human face detected in the image.")
|
58 |
|
59 |
+
return [dic, perc_area, img_copy]
|
60 |
|
61 |
def classify_url(url):
|
62 |
# Load the image from the URL
|
|
|
68 |
|
69 |
# Define the GRADIO output interface
|
70 |
examples = [f"example{i}.jpg" for i in range(1,9)]
|
71 |
+
|
72 |
+
# Define the GRADIO output interfaces
|
73 |
+
output_interfaces = [
|
74 |
+
gr.outputs.Label(num_top_classes=3),
|
75 |
+
gr.outputs.Textbox(label="% Area of the largest face in image"),
|
76 |
+
gr.outputs.Image(type="pil", label="Detected Faces")
|
77 |
+
]
|
78 |
# Define the GRADIO app
|
79 |
+
app = gr.Interface(classify_image, gr.Image(shape=(224, 224)), outputs=output_interfaces, allow_flagging="never", examples = examples,title="NSFW/SFW Classifier")
|
80 |
|
81 |
# Start the GRADIO app
|
82 |
app.launch()
|