Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[1]:
|
5 |
+
|
6 |
+
|
7 |
+
get_ipython().system('pip install gradio python-docx --quiet')
|
8 |
+
|
9 |
+
|
10 |
+
# In[2]:
|
11 |
+
|
12 |
+
|
13 |
+
import gradio as gr
|
14 |
+
import pandas as pd
|
15 |
+
import keras
|
16 |
+
import numpy as np
|
17 |
+
from docx import Document
|
18 |
+
|
19 |
+
|
20 |
+
# In[3]:
|
21 |
+
|
22 |
+
|
23 |
+
docs = []
|
24 |
+
model = keras.saving.load_model("resnet50_best.keras")
|
25 |
+
|
26 |
+
|
27 |
+
# In[4]:
|
28 |
+
|
29 |
+
|
30 |
+
def upload_images(image_paths):
|
31 |
+
docs.clear()
|
32 |
+
df = pd.DataFrame(columns=["Index", "File", "Result"])
|
33 |
+
for i in range(len(image_paths)):
|
34 |
+
df.loc[i] = [str(i+1), image_paths[i].split("/")[-1], predict(image_paths[i])]
|
35 |
+
docs.append([str(i+1), image_paths[i].split("/")[-1], predict(image_paths[i])])
|
36 |
+
return [df, gr.Button(visible=True), gr.DownloadButton(label="Download report", visible=True)]
|
37 |
+
|
38 |
+
|
39 |
+
# In[5]:
|
40 |
+
|
41 |
+
|
42 |
+
# Function to preprocess image and predict
|
43 |
+
def predict(image_path):
|
44 |
+
img = keras.utils.load_img(image_path, target_size=(300, 300))
|
45 |
+
img_array = keras.utils.img_to_array(img)
|
46 |
+
img_array = keras.ops.expand_dims(img_array, 0)
|
47 |
+
prediction = model.predict(img_array)
|
48 |
+
class_names = ["Defective", "Ok"] # Class 0: def, Class 1: ok
|
49 |
+
predicted_class = class_names[1] if prediction > 0.5 else class_names[0]
|
50 |
+
return predicted_class
|
51 |
+
|
52 |
+
|
53 |
+
# In[6]:
|
54 |
+
|
55 |
+
|
56 |
+
def generate_docs():
|
57 |
+
document = Document()
|
58 |
+
document.add_heading("Casting Report", 0)
|
59 |
+
table = document.add_table(rows=1, cols=3)
|
60 |
+
hdr_cells = table.rows[0].cells
|
61 |
+
hdr_cells[0].text = "Index"
|
62 |
+
hdr_cells[1].text = "File"
|
63 |
+
hdr_cells[2].text = "Result"
|
64 |
+
for i in range(len(docs)):
|
65 |
+
row_cells = table.add_row().cells
|
66 |
+
row_cells[0].text = docs[i][0]
|
67 |
+
row_cells[1].text = docs[i][1]
|
68 |
+
row_cells[2].text = docs[i][2]
|
69 |
+
document.save("casting_report.docx")
|
70 |
+
return [gr.UploadButton(visible=True), gr.DownloadButton(visible=True)]
|
71 |
+
|
72 |
+
|
73 |
+
# In[7]:
|
74 |
+
|
75 |
+
|
76 |
+
with gr.Blocks() as demo:
|
77 |
+
with gr.Column():
|
78 |
+
f = gr.File(file_count="multiple", file_types=[".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff"])
|
79 |
+
u = gr.Button("Upload files", visible=True)
|
80 |
+
d = gr.DownloadButton("Download report", visible=True)
|
81 |
+
r = gr.DataFrame(headers=["Index", "File", "Result"])
|
82 |
+
|
83 |
+
u.click(upload_images, f, [r, u, d])
|
84 |
+
d.click(generate_docs, None, [u, d])
|
85 |
+
|
86 |
+
|
87 |
+
# In[8]:
|
88 |
+
|
89 |
+
|
90 |
+
demo.launch(share=True, debug=True)
|
91 |
+
|