Arifin commited on
Commit
df2d21a
·
1 Parent(s): 6e9944b
Files changed (2) hide show
  1. app.py +103 -0
  2. requirement.txt +8 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from roboflow import Roboflow
3
+ import numpy as np
4
+ from PIL import Image
5
+ import requests
6
+ from io import BytesIO
7
+ import pandas as pd
8
+ import os
9
+ from openpyxl import Workbook
10
+
11
+ # Initialize Roboflow with your API key
12
+ rf = Roboflow(api_key="kKDoCn3ABT9AKeFQDCB4")
13
+
14
+ # Function to calculate the area of a polygon using the shoelace formula
15
+ def calculate_polygon_area(points):
16
+ n = len(points)
17
+ area = 0.0
18
+ for i in range(n):
19
+ x1, y1 = points[i]
20
+ x2, y2 = points[(i + 1) % n]
21
+ area += (x1 * y2 - x2 * y1)
22
+ return abs(area) / 2.0
23
+
24
+ # Function to process Roboflow prediction JSON and calculate corrosion areas
25
+ def calculate_corrosion_areas(json_data, unit="pixels", conversion_factor=1):
26
+ corrosion_areas = []
27
+ for prediction in json_data["predictions"]:
28
+ if prediction["class"] == "Corrosion":
29
+ points = [(point["x"], point["y"]) for point in prediction["points"]]
30
+ area = calculate_polygon_area(points)
31
+ if unit == "cm??":
32
+ area *= conversion_factor # Convert area from pixels to cm??
33
+ corrosion_areas.append(area)
34
+
35
+ total_corrosion_area = sum(corrosion_areas)
36
+
37
+ # Prepare output
38
+ result = {
39
+ "individual_areas": [f"{area} {unit}" for area in corrosion_areas],
40
+ "total_area": f"{total_corrosion_area} {unit}",
41
+ "recommendation": get_inspection_recommendation(total_corrosion_area)
42
+ }
43
+ return result
44
+
45
+ # Function to provide inspection recommendation based on total corrosion area
46
+ def get_inspection_recommendation(total_area):
47
+ if total_area < 1000:
48
+ return "No immediate inspection needed."
49
+ elif total_area < 5000:
50
+ return "Schedule an inspection in the next 6 months."
51
+ else:
52
+ return "Immediate inspection required."
53
+
54
+ # Define a Gradio interface to input a URL, run inference, and calculate corrosion areas
55
+ def url_infer_and_calculate(url, location, unit="pixels", conversion_factor=1):
56
+ try:
57
+ # Run inference using the Roboflow script
58
+ rf_project = rf.workspace().project("corrosion-instance-segmentation-sfcpc")
59
+ model = rf_project.version(3).model
60
+ prediction = model.predict(url)
61
+
62
+ # Ensure the response is properly formatted as JSON
63
+ prediction_json = prediction.json()
64
+
65
+ # Calculate corrosion areas from the Roboflow prediction
66
+ corrosion_areas = calculate_corrosion_areas(prediction_json, unit, float(conversion_factor))
67
+
68
+ # Download the image from the URL and convert it to a PIL Image
69
+ response = requests.get(url)
70
+ img = Image.open(BytesIO(response.content))
71
+
72
+ # Create a pandas DataFrame for reporting
73
+ df = pd.DataFrame([{'Number': index+1, 'URL': url, 'Location': location, 'corrosion_areas': corrosion_areas, 'Recommendation': corrosion_areas['recommendation']} for index in range(len(corrosion_areas))])
74
+
75
+ # Convert DataFrame to Excel and return it
76
+ excel_file = BytesIO()
77
+ with pd.ExcelWriter(excel_file, engine='openpyxl') as writer:
78
+ df.to_excel(writer, index=False)
79
+
80
+ excel_data = excel_file.getvalue()
81
+
82
+ return img, corrosion_areas, prediction_json, excel_data
83
+
84
+ except Exception as e:
85
+ return {"error": str(e)}
86
+
87
+ # Create a Gradio interface for URL input, inference, and corrosion area calculation
88
+ iface = gr.Interface(
89
+ fn=url_infer_and_calculate,
90
+ inputs=[
91
+ gr.inputs.Textbox(label="Enter the URL of an image"),
92
+ gr.inputs.Textbox(label="Enter the Location"),
93
+ gr.inputs.Dropdown(choices=["pixels", "cm??"], label="Area Unit"),
94
+ gr.inputs.Textbox(label="Conversion Factor")
95
+ ],
96
+ outputs=[gr.outputs.Image(type="pil"), "json", "json", gr.outputs.File(extension=".xlsx")],
97
+ title="Tim CCG",
98
+ description="Enter the URL of an image to perform rust detection and calculate corrosion areas.",
99
+ )
100
+
101
+ # Launch the Gradio interface
102
+ iface.launch(debug=False, share=False)
103
+
requirement.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ roboflow
3
+ numpy
4
+ pillow
5
+ requests
6
+ pandas
7
+ openpyxl
8
+