Spaces:
Runtime error
Runtime error
Create prediction.py
Browse files- prediction.py +41 -0
prediction.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
from keras.models import load_model
|
4 |
+
|
5 |
+
|
6 |
+
def getAge(distr):
|
7 |
+
distr = distr*4
|
8 |
+
if distr >= 0.65 and distr <= 1.4:
|
9 |
+
return "0-18"
|
10 |
+
if distr >= 1.65 and distr <= 2.4:
|
11 |
+
return "19-30"
|
12 |
+
if distr >= 2.65 and distr <= 3.4:
|
13 |
+
return "31-80"
|
14 |
+
if distr >= 3.65 and distr <= 4.4:
|
15 |
+
return "80 +"
|
16 |
+
return "Unknown"
|
17 |
+
|
18 |
+
|
19 |
+
def getGender(prob):
|
20 |
+
if prob < 0.5:
|
21 |
+
return "Male"
|
22 |
+
else:
|
23 |
+
return "Female"
|
24 |
+
|
25 |
+
|
26 |
+
def getAgeGender(image_path):
|
27 |
+
# Loading the uploaded Image:
|
28 |
+
image = cv2.imread(image_path,0)
|
29 |
+
image = cv2.resize(image,dsize=(64,64))
|
30 |
+
image = image.reshape((image.shape[0],image.shape[1],1))
|
31 |
+
|
32 |
+
# Loading the trained model:
|
33 |
+
model = load_model('data.h5')
|
34 |
+
# model.summary()
|
35 |
+
|
36 |
+
# Getting the predictions:
|
37 |
+
image = image/255
|
38 |
+
val = model.predict(np.array([image]))
|
39 |
+
age = getAge(val[0])
|
40 |
+
gender = getGender(val[1])
|
41 |
+
return age, gender
|