IS361Group4 commited on
Commit
8c2cbb9
·
verified ·
1 Parent(s): ac7b0c6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # !pip install transformers==4.37.2 gradio==4.25.0
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+ import numpy as np
5
+ from PIL import Image
6
+ age_classifier = pipeline("image-classification", model="nateraw/vit-age-classifier")
7
+ emotion_classifier = pipeline("image-classification", model="jhoppanne/Emotion-Image-Classification-V2")
8
+ def pred_age_emotion(input_image):
9
+ if isinstance(input_image,np.ndarray):
10
+ img = Image.fromarray(input_image)
11
+ #age classifier
12
+ age_result = age_classifier(img)
13
+ age_score = age_result[0].get('score')
14
+ age_label = age_result[0].get('label')
15
+ txt1 =''
16
+ txt1 += f'The Model predict that the person in this image is around {age_label} years old.\n'
17
+ txt1 += f'with confident score : {age_score*100:.2f}%'
18
+ #emotion classifier
19
+ emotion_result = emotion_classifier(img)
20
+ emotion_score = emotion_result[0].get('score')
21
+ emotion_label = emotion_result[1].get('label')
22
+ txt2=''
23
+ txt2+= f'The Model predict that the emotion of person in this image is {emotion_label}.\n'
24
+ txt2+= f'with confident score : {emotion_score*100:.2f}% '
25
+ else:
26
+ txt1,txt2 = "sorry, unable to process the image"
27
+ return txt1, txt2
28
+ # return f"Data type of uploaded image: {type(img)}"
29
+ def pred_emotion(input_image):
30
+ return
31
+ iface = gr.Interface(fn=pred_age_emotion, inputs = gr.Image(), outputs = ["text", "text"])
32
+ iface.launch(share=True)