Spaces:
Runtime error
Runtime error
Commit
·
7bf2f0d
1
Parent(s):
4489ef8
Upload folder using huggingface_hub
Browse files- .gitattributes +1 -0
- README.md +2 -8
- app.py +122 -0
- corrosion1.png +0 -0
- corrosion2.jpeg +3 -0
- requirements.txt +1 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
corrosion2.jpeg filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
---
|
2 |
title: TimCCG
|
3 |
-
emoji: 🚀
|
4 |
-
colorFrom: yellow
|
5 |
-
colorTo: pink
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 3.44.4
|
8 |
app_file: app.py
|
9 |
-
|
|
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
title: TimCCG
|
|
|
|
|
|
|
|
|
|
|
3 |
app_file: app.py
|
4 |
+
sdk: gradio
|
5 |
+
sdk_version: 3.41.2
|
6 |
---
|
|
|
|
app.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Corrosion Excel.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1XTrJK3G8Har_yZt83wasZX1LYD6DG1Sa
|
8 |
+
"""
|
9 |
+
|
10 |
+
#install dependencies
|
11 |
+
!pip install roboflow
|
12 |
+
!pip install gradio
|
13 |
+
|
14 |
+
#import library
|
15 |
+
import gradio as gr
|
16 |
+
from roboflow import Roboflow
|
17 |
+
import numpy as np
|
18 |
+
from PIL import Image
|
19 |
+
import requests
|
20 |
+
from io import BytesIO
|
21 |
+
import pandas as pd
|
22 |
+
import os
|
23 |
+
|
24 |
+
# Initialize Roboflow with your API key
|
25 |
+
rf = Roboflow(api_key="kKDoCn3ABT9AKeFQDCB4")
|
26 |
+
|
27 |
+
# Function to calculate the area of a polygon using the shoelace formula
|
28 |
+
def calculate_polygon_area(points):
|
29 |
+
n = len(points)
|
30 |
+
area = 0.0
|
31 |
+
for i in range(n):
|
32 |
+
x1, y1 = points[i]
|
33 |
+
x2, y2 = points[(i + 1) % n]
|
34 |
+
area += (x1 * y2 - x2 * y1)
|
35 |
+
return abs(area) / 2.0
|
36 |
+
|
37 |
+
# Function to process Roboflow prediction JSON and calculate corrosion areas
|
38 |
+
def calculate_corrosion_areas(json_data, unit="pixels", conversion_factor=1):
|
39 |
+
corrosion_areas = []
|
40 |
+
for prediction in json_data["predictions"]:
|
41 |
+
if prediction["class"] == "Corrosion":
|
42 |
+
points = [(point["x"], point["y"]) for point in prediction["points"]]
|
43 |
+
area = calculate_polygon_area(points)
|
44 |
+
if unit == "cm²":
|
45 |
+
area *= conversion_factor # Convert area from pixels to cm²
|
46 |
+
corrosion_areas.append(area)
|
47 |
+
|
48 |
+
total_corrosion_area = sum(corrosion_areas)
|
49 |
+
|
50 |
+
# Prepare output
|
51 |
+
result = {
|
52 |
+
"individual_areas": [f"{area} {unit}" for area in corrosion_areas],
|
53 |
+
"total_area": f"{total_corrosion_area} {unit}",
|
54 |
+
"recommendation": get_inspection_recommendation(total_corrosion_area)
|
55 |
+
}
|
56 |
+
return result
|
57 |
+
|
58 |
+
# Function to provide inspection recommendation based on total corrosion area
|
59 |
+
def get_inspection_recommendation(total_area):
|
60 |
+
if total_area < 1000:
|
61 |
+
return "No immediate inspection needed."
|
62 |
+
elif total_area < 5000:
|
63 |
+
return "Schedule an inspection in the next 6 months."
|
64 |
+
else:
|
65 |
+
return "Immediate inspection required."
|
66 |
+
|
67 |
+
# Define a Gradio interface to input a URL, run inference, and calculate corrosion areas
|
68 |
+
def url_infer_and_calculate(url, location, unit="pixels", conversion_factor=1):
|
69 |
+
try:
|
70 |
+
# Run inference using the Roboflow script
|
71 |
+
rf_project = rf.workspace().project("corrosion-instance-segmentation-sfcpc")
|
72 |
+
model = rf_project.version(3).model
|
73 |
+
prediction = model.predict(url)
|
74 |
+
|
75 |
+
# Ensure the response is properly formatted as JSON
|
76 |
+
prediction_json = prediction.json()
|
77 |
+
|
78 |
+
# Calculate corrosion areas from the Roboflow prediction
|
79 |
+
corrosion_areas = calculate_corrosion_areas(prediction_json, unit, float(conversion_factor))
|
80 |
+
|
81 |
+
# Download the image from the URL and convert it to a PIL Image
|
82 |
+
response = requests.get(url)
|
83 |
+
img = Image.open(BytesIO(response.content))
|
84 |
+
|
85 |
+
# Create a pandas DataFrame for reporting
|
86 |
+
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))])
|
87 |
+
|
88 |
+
# Write DataFrame to local CSV file with index included immediately after creating it.
|
89 |
+
df.to_csv('Corrosion_Report.csv', index=False)
|
90 |
+
|
91 |
+
return img, corrosion_areas, prediction_json
|
92 |
+
|
93 |
+
except Exception as e:
|
94 |
+
return {"error": str(e)}
|
95 |
+
|
96 |
+
# Create a Gradio interface for URL input, inference, and corrosion area calculation
|
97 |
+
iface = gr.Interface(
|
98 |
+
fn=url_infer_and_calculate,
|
99 |
+
inputs=[
|
100 |
+
gr.inputs.Textbox(label="Enter the URL of an image"),
|
101 |
+
gr.inputs.Textbox(label="Enter the Location"),
|
102 |
+
gr.inputs.Dropdown(choices=["pixels", "cm²"], label="Area Unit"),
|
103 |
+
gr.inputs.Textbox(label="Conversion Factor")
|
104 |
+
],
|
105 |
+
outputs=[gr.outputs.Image(type="pil"), "json", "json"],
|
106 |
+
title="Tim CCG",
|
107 |
+
description="Enter the URL of an image to perform rust detection and calculate corrosion areas.",
|
108 |
+
)
|
109 |
+
|
110 |
+
# Launch the Gradio interface
|
111 |
+
iface.launch(debug=True, share=True)
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
|
corrosion1.png
ADDED
![]() |
corrosion2.jpeg
ADDED
![]() |
Git LFS Details
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
roboflow
|