Rehman1603's picture
Update app.py
ad1bb3e
import numpy as np
import cv2
from keras.models import load_model
import gradio as gr
model_info=load_model("GenderPredict_Model.h5",compile=True)
def Gender_prediction(img,choice):
value=-1
if(choice=="Through_Id_Card"):
face_classifier = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
scale_percent = 60 # percent of original size
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
image = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)
if faces is ():
print("No faces found")
for (x, y, w, h) in faces:
x = x - 25 # Padding trick to take the whole face not just Haarcascades points
y = y - 40 # Same here...
cv2.rectangle(image, (x, y), (x + w + 50, y + h + 70), (27, 200, 10), 2)
for (x, y, width, height) in faces:
roi = image[y:y+height, x:x+width]
cv2.imwrite("face.jpg",roi)
img=cv2.resize(cv2.imread("face.jpg"),(224,224))
result=model_info.predict(img.reshape(1,224,224,3))
value=result.argmax()
elif(choice=="Through_Image"):
img=cv2.resize(img,(224,224))
result=model_info.predict(img.reshape(1,224,224,3))
value=result.argmax()
if(value==0):
return "You are Female"
elif(value==1):
return "You are male"
else:
return "No Predict please Choose any option"
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")],
outputs=[gr.components.Textbox(label="Your Result")],
examples=[
["Girl_Id.jpg"],
["Male_id.jpg"],
["Female.jpg"],
["Men.jpg"]
])
interface.launch(debug=True)