Spaces:
Runtime error
Runtime error
Commit
·
8aac212
1
Parent(s):
609a565
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import cv2
|
| 3 |
+
from keras.models import load_model
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
model_info=load_model("GenderPredict_Model.h5",compile=True)
|
| 7 |
+
|
| 8 |
+
def Gender_prediction(img,choice):
|
| 9 |
+
value=-1
|
| 10 |
+
if(choice=="Through_Id_Card"):
|
| 11 |
+
face_classifier = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 12 |
+
scale_percent = 60 # percent of original size
|
| 13 |
+
width = int(img.shape[1] * scale_percent / 100)
|
| 14 |
+
height = int(img.shape[0] * scale_percent / 100)
|
| 15 |
+
dim = (width, height)
|
| 16 |
+
image = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
|
| 17 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 18 |
+
faces = face_classifier.detectMultiScale(gray, 1.3, 5)
|
| 19 |
+
if faces is ():
|
| 20 |
+
print("No faces found")
|
| 21 |
+
for (x, y, w, h) in faces:
|
| 22 |
+
x = x - 25 # Padding trick to take the whole face not just Haarcascades points
|
| 23 |
+
y = y - 40 # Same here...
|
| 24 |
+
cv2.rectangle(image, (x, y), (x + w + 50, y + h + 70), (27, 200, 10), 2)
|
| 25 |
+
for (x, y, width, height) in faces:
|
| 26 |
+
roi = image[y:y+height, x:x+width]
|
| 27 |
+
cv2.imwrite("face.jpg",roi)
|
| 28 |
+
img=cv2.resize(cv2.imread("face.jpg"),(224,224))
|
| 29 |
+
result=model_info.predict(img.reshape(1,224,224,3))
|
| 30 |
+
value=result.argmax()
|
| 31 |
+
elif(choice=="Through_Image"):
|
| 32 |
+
img=cv2.resize(img,(224,224))
|
| 33 |
+
result=model_info.predict(img.reshape(1,224,224,3))
|
| 34 |
+
value=result.argmax()
|
| 35 |
+
if(value==0):
|
| 36 |
+
return "You are Female"
|
| 37 |
+
elif(value==1):
|
| 38 |
+
return "You are male"
|
| 39 |
+
else:
|
| 40 |
+
return "No Predict please Choose any option"
|
| 41 |
+
|
| 42 |
+
interface=gr.Interface(fn=Gender_prediction,inputs=[gr.components.Image(label="Choose Image",type="numpy"),gr.components.Radio(['Through_Id_Card','Through_Image'],type="value",label="Select any One")],
|
| 43 |
+
outputs=[gr.components.Textbox(label="Your Result")])
|
| 44 |
+
interface.launch(debug=True)
|