kim1688 commited on
Commit
b0b78da
·
verified ·
1 Parent(s): da73e4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -39
app.py CHANGED
@@ -1,40 +1,36 @@
1
- import numpy as np
2
- import pandas as pd
3
- import keras
4
- import gradio as gr
5
-
6
-
7
-
8
- import os
9
- os.environ["KERAS_BACKEND"] = "tensorflow"
10
-
11
- import keras
12
-
13
- model = keras.saving.load_model("hf://kim1688/casting_defect_resnet50")
14
-
15
-
16
-
17
-
18
- def upload_images(image_paths):
19
- df = pd.DataFrame(columns=["Index", "File", "Result"])
20
- for i in range(len(image_paths)):
21
- df.loc[i] = [str(i+1), image_paths[i].split("\\")[-1], predict(image_paths[i])]
22
- return df
23
-
24
- # Function to preprocess image and predict
25
- def predict(image_path):
26
- img = keras.utils.load_img(image_path, target_size=(300, 300))
27
- img_array = keras.utils.img_to_array(img)
28
- img_array = keras.ops.expand_dims(img_array, 0)
29
- prediction = model.predict(img_array)
30
- class_names = ["Defective", "Normal"] # Class 0: def, Class 1: ok
31
- predicted_class = class_names[1] if prediction > 0.5 else class_names[0]
32
- return predicted_class
33
-
34
- demo = gr.Interface(
35
- upload_images,
36
- gr.File(file_count="multiple", file_types=[".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff"]),
37
- gr.DataFrame(headers=["Index", "File", "Result"])
38
- )
39
-
40
  demo.launch(share=True, debug=True)
 
1
+ import os
2
+ import numpy as np
3
+ import pandas as pd
4
+ import keras
5
+ import gradio as gr
6
+
7
+ os.environ["KERAS_BACKEND"] = "tensorflow"
8
+ print("loading file")
9
+ model = keras.saving.load_model("hf://kim1688/casting_defect_resnet50")
10
+
11
+
12
+
13
+
14
+ def upload_images(image_paths):
15
+ df = pd.DataFrame(columns=["Index", "File", "Result"])
16
+ for i in range(len(image_paths)):
17
+ df.loc[i] = [str(i+1), image_paths[i].split("\\")[-1], predict(image_paths[i])]
18
+ return df
19
+
20
+ # Function to preprocess image and predict
21
+ def predict(image_path):
22
+ img = keras.utils.load_img(image_path, target_size=(300, 300))
23
+ img_array = keras.utils.img_to_array(img)
24
+ img_array = keras.ops.expand_dims(img_array, 0)
25
+ prediction = model.predict(img_array)
26
+ class_names = ["Defective", "Normal"] # Class 0: def, Class 1: ok
27
+ predicted_class = class_names[1] if prediction > 0.5 else class_names[0]
28
+ return predicted_class
29
+
30
+ demo = gr.Interface(
31
+ upload_images,
32
+ gr.File(file_count="multiple", file_types=[".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff"]),
33
+ gr.DataFrame(headers=["Index", "File", "Result"])
34
+ )
35
+
 
 
 
 
36
  demo.launch(share=True, debug=True)