vickyG commited on
Commit
a98e690
·
1 Parent(s): c96bbee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -4
app.py CHANGED
@@ -1,9 +1,16 @@
1
  import gradio as gr
 
 
2
  import tensorflow as tf
3
- import pickle
4
  from keras.models import model_from_json
5
 
6
- model = tf.keras.models.load_model('model')
 
 
 
 
 
 
7
 
8
  image_size = 250
9
 
@@ -13,7 +20,85 @@ def predict_image(img):
13
  print(prediction)
14
  return {"Victoria": float(prediction[i]) for i in range(1)}
15
 
16
- image = gr.inputs.Image(source="webcam", shape=(image_size,image_size))
17
  label = gr.outputs.Label(num_top_classes=1)
18
 
19
- gr.Interface(fn=predict_image, inputs=image, outputs=label,interpretation='default').launch(debug='True')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import os
4
  import tensorflow as tf
 
5
  from keras.models import model_from_json
6
 
7
+ def set_image(image):
8
+ return gr.Image.update(value=image[0][0])
9
+
10
+ def run_model(image: np.ndarray):
11
+ return gr.Image.update(value=detect_face(image))
12
+
13
+ model = tf.keras.models.load_model('model')
14
 
15
  image_size = 250
16
 
 
20
  print(prediction)
21
  return {"Victoria": float(prediction[i]) for i in range(1)}
22
 
23
+ image = gr.inputs.Image(shape=(image_size,image_size))
24
  label = gr.outputs.Label(num_top_classes=1)
25
 
26
+ sample_images = []
27
+ base_dir = './examples/'
28
+ for file in os.listdir(base_dir):
29
+ if file == ".git" or file == ".ipynb_checkpoints":
30
+ continue
31
+ sample_images.append(base_dir + '/' + file)
32
+
33
+ def interface() -> None:
34
+ # Create the blocks for the interface
35
+ with gr.Blocks() as app:
36
+ with gr.Group():
37
+ with gr.Tabs():
38
+ with gr.TabItem("Image input 🖼️"):
39
+ with gr.Row():
40
+ with gr.Column():
41
+ with gr.Row():
42
+ image_in = gr.Image(
43
+ label="Image input", interactive=True)
44
+ with gr.Row():
45
+ detect_image_button = gr.Button(
46
+ value="Detect face 👤")
47
+ # with gr.Row():
48
+ # paths = [["./examples/" + example]
49
+ # for example in os.listdir("examples")]
50
+ # example_images = gr.Dataset(components=([image_in]), label="Example images", samples=[[path]
51
+ # for path in paths])
52
+ # with gr.Column():
53
+ # with gr.Row():
54
+ # face_detected_image_out = gr.Image(
55
+ # label="Face detected", interactive=False)
56
+
57
+ # example_images.click(fn=set_image, inputs=[
58
+ # example_images], outputs=[image_in])
59
+ # detect_image_button.click(fn=predict_image, inputs=[
60
+ # image_in], outputs=face_detected_image_out)
61
+
62
+ with gr.TabItem("Webcam input 📷"):
63
+ with gr.Row():
64
+ with gr.Column():
65
+ with gr.Row():
66
+ webcam_image_in = gr.Webcam(
67
+ label="Webcam input")
68
+ with gr.Row():
69
+ detect_button = gr.Button(
70
+ value="Detect face 👤")
71
+ with gr.Row():
72
+ gr.Text(
73
+ label="⚠️ Reminder ", value="Do not forget to click the camera button to freeze and get the webcam image 📷!", interactive=False)
74
+ with gr.Column():
75
+ with gr.Row():
76
+ face_detected_webcam_out = gr.Image(
77
+ label="Face detected", interactive=False)
78
+ detect_button.click(fn=predict_image, inputs=webcam_image_in, outputs=face_detected_webcam_out)
79
+
80
+ app.launch()
81
+
82
+
83
+ if __name__ == '__main__':
84
+ interface() # Run the interface
85
+
86
+
87
+ # import gradio as gr
88
+ # import tensorflow as tf
89
+ # from keras.models import model_from_json
90
+
91
+ # model = tf.keras.models.load_model('model')
92
+
93
+ # image_size = 250
94
+
95
+ # def predict_image(img):
96
+ # img_4d=img.reshape(-1,image_size,image_size,3)
97
+ # prediction=model.predict(img_4d, verbose=0)[0]
98
+ # print(prediction)
99
+ # return {"Victoria": float(prediction[i]) for i in range(1)}
100
+
101
+ # image = gr.inputs.Image(source="webcam", shape=(image_size,image_size))
102
+ # label = gr.outputs.Label(num_top_classes=1)
103
+
104
+ # gr.Interface(fn=predict_image, inputs=image, outputs=label,interpretation='default').launch(debug='True')